teapot/pkgs/brewTea.nix
2025-04-21 17:24:14 -07:00

91 lines
No EOL
2 KiB
Nix

{ pkgs, inputs }:
{
name,
version,
# TODO: do not require flakes
markdollParser ? inputs.markdoll.packages.${pkgs.system}.markdoll,
markdollPlugins ? [],
src,
layoutSrc,
# otherwise set by frontmatter
defaultLayout ? "default.html",
}: let
markdoll = "${markdollParser}/bin/markdoll";
in pkgs.stdenvNoCC.mkDerivation {
inherit name version src;
buildPhase = ''
mkdir dist
layout_inject() {
layoutFile=$1
placeholder=$2
content=$3
layout=$(cat $layoutFile)
echo ''${layout/"$placeholder"/"$content"}
}
get_frontmatter() {
key=$1
file=$2
keyLine=$(grep -e "$key: " $file || echo "")
sed "s/.*: //" <<<$keyLine | xargs
}
render_final() {
file=$1
content=$2
outFile=$3
### Frontmatter: Get Layout
layout="${defaultLayout}"
layoutFm=$(get_frontmatter layout $file)
if [ ! -z $layoutFm ]; then
layout="$layoutFm"
fi
layoutPath="${layoutSrc}/$layout"
if [ ! -f $layoutPath ]; then
echo "Layout file $layout not found"
exit 1
fi
### Frontmatter: Title
title=$(get_frontmatter title $file)
if [ -z $title ]; then
title=$(basename $file | sed -e 's/.doll$//')
fi
### Layout: Title + Content
mkdir -p $(dirname $outFile)
layout_inject $layoutPath %%TITLE%% "$title" > $outFile.tmp.1
layout_inject $outFile.tmp.1 %%CONTENT%% "$content" > $outFile.tmp.final
### Layout: Cleanup Tmpfiles
cp $outFile.tmp.final $outFile
rm $outFile.tmp*
}
echo ">>> DOLLFILES"
dollFiles=$(find . -name '*.doll')
echo $dollFiles
for file in $dollFiles; do
outFile=$(realpath -m "dist/$(echo $file | sed -e 's/.doll$/.html/')")
echo "reading $file; plopping $outFile"
### Markdoll: Convert
content=$(cat $file | ${markdoll} convert)
render_final $file $content $outFile
done
'';
installPhase = ''
cp -rv dist $out
'';
}