C Preprocessor HTML generation
Posted: Sunday, 12 January 2025 @ 19:54 EST
It's not a novel technique in the slightest, but I'm currently using the C preprocessor to process the HTML files for https://standingstones.neocities.org/, which is pending a rename but is something I intend to work on further. The script I'm using looks like this:
Essentially this copies everything to the out directory, processes all HTML files with cpp, replacing their unprocessed copies there, and then exits. I then just run `neocities push out` and that's that.
It's certainly not something I'd do for anything more complex, but it works remarkably well for what I want, which is just putting in a uniform header and footer.
Code: Select all
#!/usr/bin/env bash
set -euxo pipefail
mkdir -p out/
shopt -s extglob
cp -r !(out) out/
shopt -u extglob
shopt -s globstar
for filename in **/*.html; do
if [[ ! $filename =~ "out/" ]]; then
mkdir -p "$(dirname $filename)"
cpp -traditional-cpp -P "$filename" > "out/$filename"
fi
done
shopt -u globstar
It's certainly not something I'd do for anything more complex, but it works remarkably well for what I want, which is just putting in a uniform header and footer.