Jack Dunne
rss
It’s three in the morning and I’m really meant to be asleep; I have an awful lot of collegework to be doing over the next few days. Instead, I’m blogging.
In speaking of blogs, I received a very friendly email a few days ago from the author of one I’ve kept coming back to here-and-then, requesting for me to set up an RSS feed. So I finished up on an assignment and thought to do something light before drifting off. I’ve chosen as such to set up an RSS feed, for the very rare occasion that I end up posting something, much less something interesting. >:)
Needs & wants
I don’t actually use RSS, although now that I’ve interacted with it I can see the uses. Of course I’ve read & heard about it from friends and from the occasional blog that I spend the evening reading, but I’d never considered setting one up for myself.
Implementation
While I do use Jekyll for generating static HTML from Markdown,1 I still prefer to stay away from web frameworks whenever I can. As a result, I thought instead to integrate it into my build script.
I’m not usually the biggest fan of Python, but since I use it to host my website locally prior to pushing changes, I thought to write something small:
import os, re, datetime as dt
URL, SRC, OUT = "https://jackinfurs.ie", "blog_src/_posts", "feed.xml"
items = []
for f in sorted([f for f in os.listdir(SRC) if f.endswith('.md')], reverse=True)[:15]:
slug = f.replace(".md", "")
with open(os.path.join(SRC, f)) as s:
c = s.read()
d = re.search(r'^date:\s*([\d-]+)', c, re.M).group(1)
t = "-".join(slug.split("-")[3:]).replace("-", " ")
date = dt.datetime.strptime(d, "%Y-%m-%d").strftime("%a, %d %b %Y 00:00:00 +0000")
link = f"{URL}/blog/{slug}/"
items.append(f"<item><title><![CDATA[{t}]]></title><link>{link}</link><guid>{link}</guid>
<pubDate>{date}</pubDate></item>")
with open(OUT, "w") as f:
f.write(f'<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
xmlns:atom="http://www.w3.org"><channel><title>jackinfurs/blog</title>
<link>{URL}/blog</link><description>The blog of the personal website of
Jack Dunne (jackinfurs); final year BSc. Computer Science at University
College Dublin.</description><atom:link href="{URL}/feed.xml" rel="self"
type="application/rss+xml" />{"".join(items)}</channel></rss>')
The caveats with this mostly regard Markdown frontmatter (I don’t record the exact second I hit post!) and inline file handling (which warrants the nightmare regex mishmash above). Besides that, it’s very straightforward for each post: get the title from the Markdown source, get the publication date RSS-compliant, add it to the feed.xml.
If you’re seeing this on RSS, wave hello!
-
…which is likely to change when I have the time; I’m sure there’s a more lightweight framework for this purpose. If not, it’s only an afternoon’s work to get it working with a website as simple as mine. ↩