Adding per-tag Atom feeds to Middleman

Jan Jones
The OOZOU Blog
Published in
1 min readDec 12, 2014

Today we decided to add per-tag feeds to our blog. Middleman proved to be flexible enough to make this pretty easy.

When we set out, we had two main questions:

  1. How to access the tags in config.rb, given that new posts can just arbitrarily add new ones?
  2. How to generate the file for each category?

It took us a bit to find the answer to the first question, but it turns out that the ready callback which is run after the rest of config.rb is exactly what we needed:

ready do
blog.tags.each do |tag, posts|
proxy "/tags/#{tag}/feed.xml",
"/tag.xml",
locals: { tag: tag, posts: posts },
ignore: true,
layout: false
end
end

The second point was much easier to solve: We’re using Middleman’s dynamic_pages to proxy the request to an Atom template (tag.xml.builder), passing in each tag and its associated posts as locals. The ignore: true option makes sure that Middleman won’t try to build the template itself.

Here’s the builder in all its glory:

xml.instruct!
xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do
site_url = "http://blog.oozou.com/"
xml.title "The Oozou Blog — #{tag.capitalize}"
xml.subtitle "We handcraft beautiful Web and Mobile apps"
xml.id URI.join(site_url, tag_path(tag))
xml.link "href" => URI.join(site_url, tag_path(tag))
xml.link "href" => URI.join(site_url, current_page.path), "rel" => "self"
xml.updated(blog.articles.first.date.to_time.iso8601) unless blog.articles.empty?
xml.author { xml.name "Oozou" }

posts.first(10).each do |article|
xml.entry do
xml.title article.title
xml.link "rel" => "alternate", "href" => URI.join(site_url, article.url)
xml.id URI.join(site_url, article.url)
xml.published article.date.to_time.iso8601
xml.updated File.mtime(article.source_file).iso8601
xml.author article.data.author
xml.avatar article.data.avatar
xml.content article.body, "type" => "html"
end
end
end

--

--

Jan Jones
The OOZOU Blog

Entrepreneur, tech geek, Apple fanboy, father. Jan is the founder of http://oozou.com and several other startups.