Contains nuts. Takes may be hot.
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:
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:
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:
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 :)