Default Pages for a Static Site Generator
More work on bitty's1 documentation is under way. I'm using files and directories for all the data instead of putting it into a database. Pages are assembled by including files inside each other.
I use a directory structure like this:
The index.html file is what gets served. Its content is built by looping over the tests directory and combining the files from the directory for each test.
Each Like the Other
Every test directory has the same set of files with the following features:
-
The
name.htmlandscript.jsare different for each test. -
The
trigger.jsfile is the same for 90% of the tests. -
The
notes.htmlfile is usually empty.
Unsurprisingly, I wrote a script that watches for new test directories and stubs in an initial set of files.
The thing is, the trigger.js file has evolved over time. So, I needed a way to update it. The trick being to avoid overwriting any of the versions of the file I customized.
Scoring the Default
The solution I'm using is all about and underscore. It breaks down like this:
-
My script generates the default version of
trigger.jswith a leading underscore on the file name: -
I rename the file to remove the underscore if I need to customized the test.
-
I re-run my file generation script when the trigger format changes.
-
If the
trigger.jsfile exists, the script doesn't do anything for that test. -
If the
trigger.jsfile doesn't exist, the script creates or updates the_trigger.jsfile.
-
If the
-
When the site builds, the
trigger.jsfile gets included if it exists. Otherwise, the fallback_trigger.jsis used.
The Name is the Game
Removing the other files from the directory tree for a moment we get this:
Using the name of the file as a toggle to determine what should happen in both stubbing and output is nice. Removing a single character takes care of everything for me. It's also an instance read for which files are the default and which have been customized.
I'm all about reducing the friction it takes to make thing. This is a nice one that lets me focus on content rather than on how it's stored.
-a
Endnotes
The site is built with a custom static site generator built around MiniJinja (rust's version of Jinja). It's fantastic.
This is a cut down version of the directory structure to make the example clearer. The actual one has more levels and everything starts with the _ named stub file.
Footnotes
Version 8 is underway. I've got a good feeling about this one.