Mephisto Tag Cloud Plugin

Posted by hank Sunday, February 25, 2007 06:15:00

Because the French one disappeared off the face of the earth, I took it upon myself to make a tag cloud Liquid plugin. Here’s the scoop. I downloaded Rick’s MeasureMap Plugin and went to town converting it.

Add this to your sidebar in your liquid template of choice:


{% for tag in site.tags %}
  {{ tag | size_tag }}
{% endfor %}

And you should have some awesome tag_cloud action. You can specify an upper limit as well:


{{ tag | size_tag: 3 }}

That code allows for up to 4em font size. I don’t recommend this because it’s ludicrous. The default upper limit is 2em, which makes the font size range between 1em and 2em. Suggestions are welcome.

Get it here:


svn co https://modzer0.cs.uaf.edu/repos/hank/code/rails/mephisto/mephisto_tag_cloud

Or install it like this:


./script/plugin install https://modzer0.cs.uaf.edu/repos/hank/code/rails/mephisto/mephisto_tag_cloud

Works for me - let me know if you have any difficulties.

Update: French one re-appears!

Well, he’s back. It was down a while back. You can see his version here. I like mine better. :P

Update: Better SELECT Statement

Thanks to Todd for pointing out that the tags were applying to not only published articles, but drafts as well, which produced false tag counts. He also pointed out that they were applying to revisions, which was unacceptable. I think I fixed the problems using some straight up SQL:

    def size_tag(tag, largest = 2, smallest = 0.5)
      number = ActiveRecord::Base.count_by_sql(
        ["SELECT COUNT(*) FROM taggings, 
            tags, contents WHERE 
            tags.name = ? AND taggings.tag_id = tags.id
            AND contents.id = taggings.taggable_id
            AND contents.published_at IS NOT NULL;", tag])
      size = sprintf("%0.2f", number.to_f / 5)
      size = size.to_f > largest ? largest : size.to_f < smallest ? smallest : size

This is simple - I’m just adding some conditions that filter unpublished articles and revisions (which I don’t think actually appear in the contents table anyway). In a test, the linux tag returned 35 results the old way, 34 without the checking of published_id, and 33 as it is above. I think it’s filtering, but Todd will have to confirm this.

Comments

Leave a response

  1. photo of todd toddFebruary 26, 2007 @ 08:23 AM

    for some reason, i only get two sizes for tags. 1em and 2em.

    in my design, 2em is tremendously large, and i would instead prefer to use a series of classes, rather than setting the font-size directly.

    i can easily change tag_cloud.rb to diplay a class with:

      &#8221;<span class='fs#{size}'><a href='/tags/#{tag}'>#{tag}</a></span>&#8221;
    

    but i am stuck on why i only see two different sizes. how can i make the tagcloud display a total of 5 sizes?

    please note, also, that i only have a few posts, currently.

    any advice?

  2. photo of Hank HankFebruary 26, 2007 @ 02:28 PM

    Todd,

    Sorry - my mistake. I wasn’t doing a to_f on my size. Therefore, it was doing an integer division. I’ll update the repository with the change. To change out your plugin, delete vendor/plugins/mephisto_tag_cloud and then do:

    
    ./script/plugin install https://modzer0.cs.uaf.edu/repos/hank/code/rails/mephisto/mephisto_tag_cloud
    
  3. photo of todd toddMarch 01, 2007 @ 01:30 PM

    thanks for the fix, hank. i found that there were too many digits, in the generated font sizes, so i ended up rounding them off to one decimal place. also, i wanted the smallest size to start at 0, so below are my changes. they seemed to work for me.

    module MephistoPlugins module TagCloud def size_tag(tag, largest = 2) size = (Tag.findby_name(tag).taggings(true).length + 8).tof / 9 fsize = largest if size > largest "#{tag}" end
    end end

  4. photo of todd toddMarch 01, 2007 @ 01:44 PM

    i had a typo in my previous post, please use this one:

    module MephistoPlugins module TagCloud def size_tag(tag, largest = 2) size = (Tag.findby_name(tag).taggings(true).length + 8).tof / 9 size = largest if size > largest "#{tag}" end
    end end

  5. photo of Hank HankMarch 01, 2007 @ 03:31 PM

    Todd, Good suggestion! I have revamped the plugin, and the changes are in subversion.

  6. photo of todd toddMarch 01, 2007 @ 07:34 PM

    got sloppy and didn’t close the code tag and broke your site. let’s see if this fixes it!

  7. no avatar available for Hank HankMarch 01, 2007 @ 07:38 PM

    I really need to get this XML pasting under wraps. This stuff needs to NOT be interpretted by browsers.

  8. photo of todd toddMarch 02, 2007 @ 07:40 AM

    i think my tagcloud is displaying tags for articles that aren’t published yet.unfrotunately, if you click that tag, there are no live articles to display.

    also, the weight for the tags seems to be a little off. for example, i have 1 post tagged with car, and it gets the largest font. the only thing i can think of is that mephisto considers each revision as an article, and is included in the array.

    is there a way around both of these issues that comes to mind?

  9. no avatar available for Hank HankMarch 02, 2007 @ 12:02 PM

    Todd: Thats in interesting hypothesis, and you could be right. I’ll check the schema and make sure my SELECT is working right.

  10. no avatar available for todd toddMarch 04, 2007 @ 07:02 PM

    any news on the select?

  11. photo of Hank HankMarch 05, 2007 @ 01:52 AM

    Todd, See the update above. I think you’ll like the changes.

  12. no avatar available for todd toddMarch 05, 2007 @ 05:48 AM

    i love the idea of a lower limit for the size. for my purposes, i’ll employ 11 sizes, from 1em to 2em.

    unfortunately this update didn’t work for me. now, i only two sizes display, small and large, and the unpublished articles are still showing up in the list. weird.

  13. no avatar available for todd toddMarch 05, 2007 @ 05:57 AM

    i am borrowing some of the math from the previous iteration of the plug, and the sizes seems to be more accurate.

    however, it is still displaying unpublished tags. possibly the plugin needs another sql select to properly display published tags only?

    size = sprintf("%0.1f", (number + 17).to_f / 19)

  14. no avatar available for Hank HankMarch 05, 2007 @ 06:47 AM

    Weird - that means the published date of the non-published articles isn’t NULL. That seems inaccurate to me. I’ll try and figure it out at some point. Thanks for the help - it’s hard for me to test since I have so many articles and they’re all imports from Typo!

  15. photo of todd toddMarch 07, 2007 @ 11:26 AM

    any news? i’ve been working mostly with this pluging on a local development environment and have just enough entries to test the tagcloud. let me know if you’d like some help.

  16. no avatar available for Hank HankMarch 07, 2007 @ 11:50 AM

    Todd, If you’d like to work on it and submit me patches, feel free. I don’t currently have the time to work on this plugin as it works just fine for my purposes currently. Thanks for the help.

  17. no avatar available for Phil PhilMarch 08, 2007 @ 10:50 AM

    I ran into some trouble using your plugin. At my blog (http://blog.philip-heck.de) it writes only the tag name, but no link. I’ve insert

    
    {% for tag in site.tags %}
      {{ tag | size_tag }}
    {% endfor %}
    

    in my layout.liquid.

    greeting Phil

  18. no avatar available for Sean SeanMarch 21, 2007 @ 02:06 PM

    The plugin appears to be unavailable…

    Plugin not found: [“https://modzer0.cs.uaf.edu/repos/hank/code/rails/mephisto/mephistotagcloud”]

  19. no avatar available for Hank HankMarch 21, 2007 @ 05:54 PM

    @Sean: It seems to be up. Try again and let me know if its still broken. try http if https doesn’t work. Perhaps your subversion isn’t compiled with https support.

    @Phil Thanks for letting me know about this. That means that the plugin isn’t working since Liquid will just return the passed object itself (in this case, the tag name) if something goes wrong with the filter. It seems to be working for me just fine - what version of Mephisto are you running? I only guarantee this works with the current subversion trunk since that’s what I use. If it’s not working, you can check out some of the other plugins and change the code around to make it work. It’s just a matter of changing a couple class/module names around and maybe editing the init.rb a little bit to make it load right. Let me know if you do this and I can make a branch.

  20. no avatar available for elliott cable elliott cableMarch 30, 2007 @ 02:52 AM

    I’m having the same problem Phil had, I’m wondering if somebody could give me more specific instructions on how to fix it, as I’m a relative noob and not sure what to do…

  21. no avatar available for Phil PhilMarch 30, 2007 @ 03:50 AM

    @Hank Thanks for your response. Tag Cloud is now working in my mephisto installation. I did not restart my webserver after plugin installation, so mephisto wrote only the tag names without using the plugin. My webserver isr running production mode so rails didn’t recognize new plugins like it does in development mode.

  22. photo of Hank HankMarch 30, 2007 @ 11:08 AM

    Thanks for clearing this up Phil. I forgot to mention that since I’m so used to doing it after installing plugins.

    You Must Restart the webserver after installing the plugin.

  23. no avatar available for Jesse JesseApril 08, 2007 @ 10:54 PM

    Hi Hank,

    I was having the same problem described by Phil, but after looking - I found the script/plugin install wasn’t bringing down the files from your server and was leaving me with a blank mephistotagcloud directory.

    I manually downloaded the files into the directory, restarted and all works well - but the first time I ran plugin install i was told “Plugin not found: [“https://modzer0.cs.uaf.edu/repos/hank/code/rails/mephisto/mephistotagcloud/”]”

    The second time I ran it, It said “already installed: mephistotagcloud (https://modzer0.cs.uaf.edu/repos/hank/code/rails/mephisto/mephistotagcloud).” - and the directory in plugins was there but blank.

    I think the trailing slash was the problem with the initial request, but either way - double check that the files are being loaded, even if script/plugin says the plugin is installed.

  24. no avatar available for Joe Beda Joe BedaJune 19, 2007 @ 08:39 PM

    I think that the problem is that the cert is expired on the https page. I used http instead and was able to get script/plugin to work.

  25. no avatar available for James Urquhart James UrquhartJuly 02, 2007 @ 09:38 AM

    Nice code, although i noticed that Safari and Opera appear to enforce a minimum font size, which pretty much screwed things up.

    The solution of course is to choose a larger minimum size. It might also help if you make the size calculation dependant on the total number of tags.

  26. photo of Hank HankJuly 02, 2007 @ 10:44 AM

    Well, it looks just fine in Firefox, and that’s pretty much the only browser demographic I care about (sorry). Just goes to show that Opera and Safari should not enforce a minimum font size.

    I don’t really want to increase my font sizes on this site since I have so many tags that it would become ridiculously long (see Longcat (Might be NSFW)). Anyway, thanks for the heads up. Making the size dependent on the total number is fine too - might be something I do if I ever come back to this code. :)

  27. no avatar available for Dariush DariushSeptember 03, 2007 @ 03:44 PM

    Good work.

    How can I show the code on my site as you have done in this page with the color and all.

    thanks much.

  28. no avatar available for Hank HankSeptember 03, 2007 @ 04:23 PM

    Just use the filter:code stuff, like so:

    
    <filter:code>This is some code</filter:code>
    

    Which makes

    
    This is some code
    
  29. no avatar available for Dariush DariushSeptember 04, 2007 @ 09:28 PM

    Hey Hank, Thanks for the quick reply. I have tried the code coloring as you suggested, it prints the line but I can not get the alternate colors as you have in your block of code shown on the top of your page.

    Also on another note, I tried the tag cloud, installed it, made the changes but I still can not get the tags to show as you have here.

    You can have a look at my site, to see what I mean.

    Thanks a bunch. www.dariushazimi.com

  30. no avatar available for Hank HankSeptember 04, 2007 @ 09:37 PM

    OH! Then you do this:

    
    <filter:code lang="ruby">a = 54 * Class.method(arg)</filter:code>
    

    There’s lots of other highlighting options too - look into the syntax highlighting plugin in vendoor/plugins.

    Interesting about the tag cloud. Are you in production mode? If so, did you clear your cache through the Mephisto admin interface?

Comment

Comments allow Markdown.