#000
Using AWK to make my website
Last updated:
This blog has been empty for quite some time, and the reason for it is certainly not a lack of ideas or motivation, but rather the desire to have the perfect setup before actually using it. I’ve come to realize that I will never really achieve this, simply because I’m constantly learning and changing my mind. Here’s my attempt at sharing what I have so far.
One of the tools missing in my setup was one to generate the HTML files for the articles of my blog. Although there is no lack of static site generators out there, I always felt that I wanted something simpler and I wanted to write it myself. Enter mmw(1), a minimal template engine written in POSIX awk(1). From the man page:
mmw merges the content of file with template files named after template by using variable substitution and prints the result to standard output.
That’s essentially all mmw(1) does. It takes a file and merges it with three templates: a header, a body and a footer.
$ mmw template file
<header>
<body>
<footer>
The same can be done with multiple files in which case the body template is printed once for every file.
$ mmw template file1 file2 file3
<header>
<body1>
<body2>
<body3>
<footer>
By defining local variables in every file, one can use them within templates to choose exactly what should be merged and what not. Together with a few other features like command evaluation and piping variables into other commands, this allows for a large number of ways to generate the content of a website (see the EXAMPLES section in the man page).
Now to the big question: Why awk? For a long time I thought of awk(1) as a program for one-liners like sed(1) or cut(1), and while it is great for that too, awk(1) is a powerful scripting language well suited for larger programs. It has just enough features to make it versatile while still keeping a very simple and intuitive syntax. For example, a very useful feature of awk(1) is associative arrays. They are used extensively by mmw(1) and is what allows it to keep track of the variables in each file. However, the very best feature of awk(1) is that it’s portable1.
Even though Awk is nearly 50 years old, and in spite of the great changes in computing, it’s still widely used, a core Unix tool that’s available on any Unix, Linux, or macOS system, and usually on Windows as well. There’s nothing to download, no libraries or packages to import — just use it. It’s an easy language to learn and you can do a lot after only a few minutes of study.
— Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger
The AWK Programming Language - Second Edition
With mmw(1), I can pretty much forget about what system I’m operating in and just focus on the writing. I’m very happy with how mmw(1) turned out. It is simple, fast and portable, and it integrates well with other UNIX utilities in a terminal based workflow.
Credits: mmw(1) took inspiration from the saait and zodiac static site generators.
-
POSIX awk(1) ↩