DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Rendering A Tag Cloud Using A Dynarex-tagcloud File

03.29.2010
| 2824 views |
  • submit to reddit
        Using a dynarex-tagcloud file used in conjunction with an S-Rscript RSF job a tag cloud can be rendered to HTML.

The following RSF job uses an XML file (dynarex-tagcloud) along with an XSL file to make the tag cloud:
<job id='to-html'>
  <script src='http://rscript.rorbuilder.info/packages/script/render.rb'/>
  <script>
    <![CDATA[

    require 'dynarex'
    require 'nokogiri'
    
    class TagCloud
      include Render

      def render(xml_url, xsl_url)
        dynarex = Dynarex.new xml_url

        biggest_tag_size = dynarex.records.max_by {|x| x[:size].to_i}[:size].to_i

        r = dynarex.records
        biggest_tag_size = r.max_by {|x| x[:size].to_i}[:size].to_i
        r.each {|x| x[:gauge] = (10 / (biggest_tag_size / x[:size].to_f)).round}

        doc = Document.new(dynarex.to_xml)
        element_xsl = Element.new('xsl_url').add_text(xsl_url)
        doc.root.elements['summary'].add_element element_xsl
        
        css_url = '/do/tagcloud/tagcloud-css'
        element_css = Element.new('css').add_text(css_url)
        doc.root.elements['summary'].add_element element_css          
        
        render_to_guide(:doc => doc)
      end
    end
    
    xml_url = params[:xml]
    xml_url ||= 'http://rscript.rorbuilder.info/tagcloud/programming.xml'
    
    xsl_url = params[:xsl]
    xsl_url ||= 'http://rscript.rorbuilder.info/tagcloud/tag_cloud.xsl'
    
    tagcloud = TagCloud.new
    tagcloud.render xml_url, xsl_url
    
    ]]>
  </script>
</job>

Tested: http://rscript.rorbuilder.info/do/tagcloud/to-html
Observed: see <a href="http://twitxr.com/image/322066/">screenshot of programming tag cloud</a> [twitxr.com]

Here's the XML file used
<pre>
<tagcloud>
  <summary/>
  <records>
    <tag><keyword>ruby</keyword><size>100</size></tag>
    <tag><keyword>php</keyword><size>55</size></tag>
    <tag><keyword>c++</keyword><size>70</size></tag>
    <tag><keyword>smalltalk</keyword><size>35</size></tag>
    <tag><keyword>python</keyword><size>50</size></tag>
    <tag><keyword>haskell</keyword><size>30</size></tag>
  </records>
</tagcloud>
</pre>

Note: The dynarex-tagcloud layout simplifies the process of creating the tag cloud.