Sunday, 2021 April 04

I've been meaning to add a feed to this blog for years but have never gotten around to it. But a few days ago someone actually requested it as a feature; this was unexpected, but gave me the motivation to actually do it.

This entire site is built with nanoc. The exact reasons I chose nanoc are lost to the depths of time - probably something to do with it being written in Ruby at a time when most site generators seemed to be in PHP or Python - but I've been generally unhappy with it for a variety of reasons. However, for the most part it's done its job and I've stuck with it, because switching to a different tool at this point seems overwhelming difficult.

A quick search revealed that there is support for feeds in a built-in module and it seemed like this would be a quick change. It was not. As with pretty much everything, the nanoc documentation around this functionality is terse to the point of uselessness. I found a few examples online, but they were also very terse. After much trial and error I did eventually get something resembling a feed working - but it was XML embedded in the standard HTML layout. As you might imagine, that does not really work (but much to my surprise Firefox actually did a decent job of attempting to render it).

I did finally did get it working, mostly through trial and error. The code changes were small, both in overall size and in comparison to the examples, but it was a frustrating process and served to indicate, once again, that I chose poorly when I chose nanoc.

First, I had to add the builder gem to my Gemfile. This gem is used to create XML.

Next, I had to enable the blogging helper. This involved creating a new file, lib/helpers.rb.

use_helper Nanoc::Helpers::Blogging

After this I created a template file (content/feed/blog.xml.erb) that the blogging helper would transform into the XML feed.

<%= atom_feed limit: 10, title: "Josh Hamacher's Blog" %>

Up to this point things were going smoothly. I then spent about an hour poking and prodding at the Rules file before I finally got something that properly applied the blogging helper. The key was, for some reason, not specifying any sort of file extension in the pattern; if I specified *.erb, it would not actually match the erb file I had just created. This was infuriating.

compile '/feed/*' do
  filter :erb
  write '/feed/blog.xml'

Once this was working I started getting helpful compilation errors from nanoc, all related to missing parameters in my configuration. I added them as necessary, and finally everything was working.

The last thing I did was add a link to the main layout, so there is now a feed icon next to the "ramblings" category.

Sunday, 2021 February 28 Tuesday, 2021 April 06