Automatically generated tags and tag clouds with Jekyll (multiple word tags allowed)

Stamped: 2010-08-25 00:00:00 -0400 | Show comments

Jekyll is a great thing, and supports tags right out of the box, but in order to get urls like in mephisto, /tags/something here, you have to either add an extension (which do exist, though I haven't tried), or generate them with the Rakefile, or however really, but I chose the Rakefile. Luckily raimonds simanovskis had already done so, but his solution didn't support tags with spaces, which I had used in mephisto, and to not break compatibility when switching, I had to edit the relevant part of his Rakefile to support tags with spaces.

So an an example post looks like this:

--- 
layout: post
title: Cloning an ISO to a USB flash drive (or anywhere) ...
syntax-highlighting: yes
tags: 
- dd
- snow leopard
---
Oh, and Leopard too...

The original method (can be viewed in it's entire, original, unedited form here). To allow support for tags with space (which some people don't like but I do!), you have to change it so it looks like:

desc 'Generate tags pages'
task :tags  => :tag_cloud do
  puts "Generating tags..."
  require 'rubygems'
  require 'jekyll'
  include Jekyll::Filters

  options = Jekyll.configuration({})
  site = Jekyll::Site.new(options)
  site.read_posts('')

  # Remove tags directory before regenerating
  FileUtils.rm_rf("tags")

  site.tags.sort.each do |tag, posts|
    html = <<-HTML
---
layout: default
title: "tagged: #{tag}"
syntax-highlighting: yes
---
  <h1 class="title">#{tag}</h1>
    
    {% for post in site.posts %}
        {% for tag in post.tags %}
            {% if tag == '#[tag]'%}
                {% include post.html %}
            {% endif %}
        {% endfor %}
    {% endfor %}
HTML

    FileUtils.mkdir_p("tags/#{tag}")
    File.open("tags/#{tag}/index.html", 'w+') do |file|
      file.puts html
    end
  end
  puts 'Done.'
end

Note the line if tag == "#[tag]" should be if tag == "#{tag}" but liquid 2.0 doesn't support escaping, and my ghetto method doesn't work. So just pull the entire function from github.

View my entire Rakefile @github.

tags: jekyll, rakefile
recent entries
Rails — A faster way for next and previous links on a post, article, or any model
The awkward things Siri says
Node.js — Getting oAuth Up and Running Using Express.js and Mongoose
Node.js — Getting oAuth Up and Running Using Express.js, Railway.js and Mongoose
Migrating from Rails 3.1 RC4 to RC5 using Heroku's Cedar Stack (also compass, unicorn, and sendgrid)
Random Freeze Fix for GTX 460 in 10.6 (osx86)
Wasted on Steam - an analytic tool for the Steam platform
Rails 3.1 — SQL logging to STDOUT during testing (with rspec, test::unit, or cucumber)
Rails 3.1 — Using ERB/HAML/etc within a Coffeescript JS file
Rails 3.1 — 'load_missing_contant': Expected ... to define ... (LoadError)
View the entire archive of articles