Subscribe:Atom Feed


Posted: 14 May, 2020

Why the hell would anyone, in the year 2020, write another blog/site generator? And why do it in C11?

The previous version of this site was built with Jekyll. Now, I'm not dunking on Jekyll, or its developers, despite the dunk-shaped text, below. Jekyll's a powerful generator that serverd me well, mostly. But my site is just a blog and a small one, at that. I've purposefully chosen to make it static because:

  1. There's no database with a static site. Meaning there's no need for me to schedule backups, or maintain a chunky bit of software.
  2. There's very little executable code on the site
  3. It's not Wordpress, so I'm not going to be constantly zero-day'd. (See point 2)
  4. I have my own server, so I can do whatever the hell I want...

So when the site generator starts requires more maintenance than a DB, well, point 4 starts to become an itch.

Now, there's a couple of tangents I could go down, here.

The first, about programming languages with large, package eco-systems. JS, Python and Ruby being the ones that I have experience of. In all instances, keeping a large piece of software working, with the correct package versions, through years of lifecycle upgrades of both the OS, interpreter and packages, is a monumental ball-ache. One that I can completely do without.

The other tangent is a Jon Blow-style rant about the state of modern programming, and the speed, and quality, of software in general. Because I'm starting to lean that way. So lets pretend I've said all that, and move on.

Last week I finally got to the point of wanting to scratch this itch. I fully planned on writing this in Rust, as a way of learning the language, but when push-came-to-shove the friction of doing so put me off. I wanted to do this quickly (I have other more pressing projects) and I wanted the code itself to be laser-focused. So I opted to do it in C11.

Crazy-talk, I can hear you thinking it, but actually, it proved to be super easy. The older I get, the more programming languages I use, the more I find that I really appreciate straight, simple C. It's fast. It's entirely memorable. And it's fun. There's an amazing eco-system of single header libraries available, and the good ones are really, exceptionally good. And I write C++ all day, so I've been feeling rusty. No pun intended...

Anyway, my code doesn't need to be as generalised as Jekyll, but it does need to:

  1. Generate the site from the markdown content that Jekyll was already using. I don't want to write a conversion script.
  2. Be configurable enough to generate my company site, and my personal site.

And because a blog is incredibly simple HTML, all the other heavy lifting can be done in the CSS (with a little sprinkling of JS).

To that end, I've used the following headers:

TinyDir handles reading and sorting directories on Linux and Windows. Ini.h allows me to put all config options in an ini file, so I can have one for each site and switch between them, and MD4C takes any markdown and parses it, Sax style. And as an example, it spits out HTML, which is exactly what I need. In fact, those 3 do 90% of the heavy lifting. All I've had to write is the glue around them, which amounts to mem allocation & string munging, oh, and the file output. Nothing hairy.

Klog itself (cos yeah, what else was I going to call it?) doesn't try to be generic. It's expecting to make a blog with a specific structure:

  1. A set of links that act as an archive.
  2. A Post Title.
  3. Post meta-data (date, in my case)
  4. A Post Body.
  5. A link to the previous post (if any)

This structure is wrapped in a header and footer, to make a complete page. The header and footer can be read in from files, whose location is specified in the .ini. The header is also where the customisation sits, as it's where the HTML defines which CSS and JS files are included.

Outside of Klog, I've ended up with several bash scripts: The first, concerns the markdown which feeds into Klog. Jekyll posts have a header with some meta-data in; Date, Title, Tags, etc. so my script spits this out, along with some default text, and then drops me into Emacs where I can write the actual post-body.

I have another script, for testing, that runs Klog and builds a local HTML root. It can also fire up a simple web-server, and launch me into a browser, pointing at the new page.

The thrid script can build the site, commit it to GitHub, and rsync the result up to my server. And I could probably take that to its logical conclusion by having a cronjob on the server, that pulls from GitHub, and builds the site automatically... shrug I'll probably leave it as an explicit, manual sync, for the time being.

All in, it probably took less than a day (8 hours) to write Klog and the bash-scripts. Plus a couple of nights to style the HTML output (and as you can see, I'm not finished with that yet).

The final executable size is ~320k (debug), and with verbose logging, it generates a site of 100posts in approx 310ms, compared to 1.6-1.8 seconds for Jekyll, with the same input. (I very much doubt that the logging is slowing it down in any meaningful way, nor the fact that it's a debug executable).

Klog v0.1 is currently tied to Linux (I wrote it using WSL on Win10, which I'll talk about later) but I do intend to make it compile and run on Windows, and once that's done, yeah, I'll make it public. But I'm not in a hurry with that, because, wow, WSL is really good! And besides, this won't be of any use to anyone but me.

So yeah. Programmer writes a program to do a thing, shock! It was a fun little deviation though :)