Creating a Linked List Atom Feed in Pelican

Monday, April 22, 2013

When I decided to reboot this site, one of my goals was to have a “linked list” feed, where the main link of each linked list post points to the URL that the post is about, instead of pointing to the post on my site. Several sites I read do this (Daring Fireball being the most popular example), to the point where I’m genuinely confused when I click a link in my newsreader and end up at the same post I just read.

I also wanted to use Pelican, for reasons I may write about another time. Pelican has no built in concept of linked lists. From Gabe at Macdrifter, I learned how to make linked list posts on my pages. Having gotten that far, I figured there had to be some way to do the same in the feed.

My first thought was to write a Pelican plugin. But Pelican’s plugin system doesn’t yet have enough hooks to make this an option. I then considered editing Pelican itself, but then I came back to my senses. I’ve done that with third-party code before. Never edit the core.

It finally occurred to me that I should be able to create a feed as just another template in the theme. I even had an example to start from: The Atom feed template in Movable Type.

If you’re interested in this for your own Pelican site, here’s how to set it up:

Download this gist and save it as “atom.html” in your theme directory. Next, download this plugin and install it so it can be used by your Pelican project.

The plugin is necessary because Datetime objects in Python are not, by default, time zone aware. But timestamps in Atom must be expressed as UTC (or as an offset from UTC).

You may have to do some jiggering to install the plugin. Pelican 3.1.1 has some issues with user-installed plugins. This should be resolved in the upcoming 3.2. In the mean time, I have this in my config file:

PLUGIN_PATH = 'my_plugins'
import sys
PLUGINS = ['tzawaredate']

When you install 3.2, you should be able to remove those middle two lines.

If you want to limit the number of items in your Atom feed you’ll need to enable the jinja2 loop controls in your config file:

JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols']

Finally, add this template to your list of direct templates and give it a save as location:

DIRECT_TEMPLATES = ('index', 'tags', 'categories', 'archives', 'atom')
ATOM_SAVE_AS = 'feeds/atom.xml'

Regenerate your site and see what you’ve got. For me, it’s working great.