Slow Lane

Weekend project: a tiny static site generator in 300 lines

· Rowan

Every couple of years I write a static site generator. It is a good calibration exercise: the problem is well understood, the scope is bounded, and the failure mode of over-engineering is visible within a day.

This year's version is 300 lines of Python and does exactly four things: walk a directory of Markdown files, parse front matter, render a template, and write output. That is it. No plugin system, because a plugin system is a promise that you will maintain an API.

The only interesting design decision

I render pages into memory as strings before touching the filesystem. That sounds wasteful and it is, but it means a half-finished build never leaves a site in a broken state, and it makes incremental output trivially correct: write everything, or write nothing.

pages = [render(p) for p in load_posts(source)]
sitemap = render_sitemap(pages)
for path, text in [*pages, sitemap]:
    (out / path).parent.mkdir(parents=True, exist_ok=True)
    (out / path).write_text(text, encoding="utf-8")

The dependency list is one line long, and the build takes 0.4 seconds for eighty posts. I will probably rewrite it in two years. That is also fine.

← All posts