Drafts

2019-2-4

This site is built on a custom static site generator (basically Pandoc and some custom programming). I have a build-system set up so it’s pretty easy for me to tweak things and see the result, which worked great for the generator itself. I found myself wanting to iteratively work on posts without publishing them, but still be able to preview what the site would look like if the drafts were published.

An overview of how the site works is here.

There is an input folder content which contains markdown to be rendered, as well as an output folder which the built site is expected to end up in as well as a working folder for intermediate files during the build.

All of this is strung together with a make file.

Since I wanted to be able to customize the build the first thing I did was to make these folders overridable by the caller of the Makefile:

# At the top of a Makefile
site_source ?= content
site_output ?= out
site_working ?= working

These default to the standard spot, but a caller can use environment variables to tweak them:

# In a 'draft'-specific build-script
export site_source=tmp/content
export site_output=tmp/out
export site_working=tmp/working

I’d started putting all of my in-progress posts into a folder called drafts. As long as I made sure that folder mirrored the structure of content I could use rsync to merge the two together and then call in to my normal build:

# still in the build-script
mkdir -p $site_source
rsync -av --delete content/ drafts/ $site_source/

make all

Because rsync preserves modify-times across calls this allows the make to keep doing incremental builds (which I was pretty pumped about).

Adventures in build-on-save

I wanted to do a re-build on save from my editor-of-choice, so I found inotify-hookable, which can be set-up to watch folders and files and trigger a shell-command after changes. The below watch-script is a wrapper around the rsync+make script described above:

#!/usr/bin/env bash

# Start with a build
scripts/draft-build.sh

# Then re-build on changes
inotify-hookable \
  -w content \
  -w drafts \
  -w src \
  -w static \
  -w util/src \
  \
  -f Makefile \
  -f scripts/draft-build.sh \
  \
  -c scripts/draft-build.sh

(It watches more than just the content and draft folders in case I change any of the other content which goes in to building the site.)