teapot/pkgs/brewTea.nix
2025-04-22 21:11:48 -07:00

127 lines
No EOL
3.5 KiB
Nix

{ pkgs, inputs }:
{
name,
version,
# TODO: do not require flakes
markdollParser ? inputs.markdoll.packages.${pkgs.system}.markdoll.override({
danger = true;
}),
markdollPlugins ? [],
src,
layoutSrc,
# otherwise set by frontmatter
defaultLayout ? "default.html",
allowFailedRenders ? true,
}: let
markdoll = "${markdollParser}/bin/markdoll";
in pkgs.stdenvNoCC.mkDerivation {
inherit name version src;
buildPhase = ''
export PATH="$PATH:${pkgs.lib.makeBinPath markdollPlugins}"
mkdir dist
echoerr() { cat <<< "$@" 1>&2; }
layout_inject() {
local layoutFile=$1
local placeholder=$2
local contentFile=$3
local layout=$(cat $layoutFile)
local content=$(cat $contentFile)
echo "''${layout/"$placeholder"/"$content"}"
}
get_frontmatter() {
key=$1
file=$2
keyLine=$(grep -e "^$key: " $file || echo "")
sed "s/.*: //" <<<$keyLine | xargs
}
render_final() {
local file=$1
local outFile=$2
local content=$3
### Frontmatter: Get Layout
local layout="${defaultLayout}"
local layoutFm=$(get_frontmatter layout $file)
if [ ! -z $layoutFm ]; then
layout="$layoutFm"
fi
local layoutPath="$layout"
if [ ! -f $layoutPath ]; then
layoutPath="${layoutSrc}/$layout"
if [ ! -f $layoutPath ]; then
echo "Layout file $layout not found"
exit 1
fi
fi
### Frontmatter: Title
local title=$(get_frontmatter title $file)
if [ -z $title ]; then
title=$(basename $file | sed -e 's/.doll$//')
fi
### Frontmatter: Meta Head Stuff
local head=""
local meta_description=$(get_frontmatter description $file)
if [ ! -z "$meta_description" ]; then
head="$head<meta name='description' content='$meta_description'>"
fi
local meta_tags=$(get_frontmatter tags $file)
if [ ! -z "$meta_tags" ]; then
head="$head<meta name='keywords' content='$meta_tags'>"
fi
local meta_author=$(get_frontmatter author $file)
if [ ! -z "$meta_author" ]; then
head="$head<meta name='author' content='$meta_author'>"
fi
### Layout: Title + Content
layout_inject $layoutPath %%TITLE%% <<<"$title" > $outFile.tmp.1
layout_inject $outFile.tmp.1 %%HEAD%% <<<"$head" > $outFile.tmp.2
layout_inject $outFile.tmp.2 %%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/')")
mkdir -p $(dirname $outFile)
echo "reading $file; plopping $outFile"
### Markdoll: Convert
local content=$(cat $file | ${markdoll} convert 2>$outFile.tmp.convert-log | tee $outFile.tmp.render)
if [ -z "$content" ]; then
echo "<pre style='padding: 1em; border: 1px solid red; width: fit-content;'>" >> $outFile.tmp.render
echo "<b style='color: red;'>[teapot]: render failed, check build logs.</b>" >> $outFile.tmp.render
echo "== build log ==" >> $outFile.tmp.render
cat $outFile.tmp.convert-log >> $outFile.tmp.render
echo "</pre>" >> $outFile.tmp.render
fi
render_final $file $outFile $outFile.tmp.render
done
'';
installPhase = ''
cp -rv dist $out
'';
}