writing a static site generator in m4 and shell
Sun Mar 18 00:39:20 EDT 2018
In a fit of stupidity one day, I decided I would rewrite my personal site (this one) with a static site generator, written in posix-compliant m4 and shell. It didn't seem like too terrible of an idea, but it quickly went downhill.
The first thing that needed to happen---basic macros to handle things like my existing headers and footers, to flank pages as they existed. Then I'd move on to some other basic macros---say automagically generate the date in the footer, and if I felt fancy, a blog generator.
Macros and rewriting existing pages was fairly easy; I chose to use a shell script to loop over what files are in the directory, generate an M4 file with a list of links to other pages for the header, and then generate the pages. This was fairly straightforward, although an unreasonable volume of the m4 uses the syscmd() function with sed to reformat text entries.
The blog part was hard; since POSIX m4 doesn't support enumeration of the number of variables, or any kind of looping, I was forced to limit each page in the blog index to 5 posts. Of course, to make matters worse, I used shell scripts to generate info like the number of indeces. I also needed to sort blog entries roughly by date; while I had a `PAGE_DATE' macro defined in each blog entry, I couldn't just use it to sort, as sorting in bash is hard. Instead, I went ahead and just used sysv init-style sorting, but in reverse.