icon fullempty.sh

A Pure Bash 4 Static Site Generator


I'm trying something with this site: a pure Bash 4 static site generator, available on github.

The syntax is simple, but a tailored specifically for this site. For example, the index page's file named content/index.post.sh has the following structure:

docvars[date]='2019-04-22 3:53 PM CDT'

The generator makes heavy use of associative arrays. Each file matched by the glob contents/*.post.sh is sourced within a loop to populate an array that is initialized with a default set of values (included in src/index.sh). Each key in the array is then expanded by searching for <% key %> and using sed to replace it with the value defined in the array.

Once this step is complete, the content value in the array is used as the body of the page, outputted to a file in build/, using filename's value in the array.


Any pages with type set to post are treated slightly differently. They are processed normally, but also added to a tab-delimited csv named posts.csv that is used to build a list of <li> elements for later inclusion as a chronologically reversed order of post links. I intend to also build an RSS file for these posts using this index.

Closing Thoughts

I focused on a shallow dependency tree (in fact, a non-existent one). While this ended up being largely hassle free, the data structures available to bash preclude a lot of advanced transforms. If bash supported multi-dimensional arrays, that may have simplified a little code. The simple solution was to iterate through the list of files twice, which is only a little slower.

I fought constant internal critiques on what I was doing, such as impulses to just trash the entire work and use a simple Python script with Jinja2. In the end I'm glad I stuck it out, though the lack of more advanced templating patterns is limiting, long term. I implemented variable expansion within other assignments in files, however, so it should be possible to simply create those kinds of control structures within bash scripts referenced by the posts themselves. My only other closing thought is the directory structure is entirely flat and will get messy in short order.

« Back
© 2022 Derek Arnold · mastodon · twitter · github
This page modified on Thu Apr 25 08:54:13 CDT 2019