{ 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", }: 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" fi local meta_tags=$(get_frontmatter tags $file) if [ ! -z meta_tags ]; then head="$head" fi local meta_author=$(get_frontmatter author $file) if [ ! -z meta_author ]; then head="$head" 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 | tee $outFile.tmp.render) render_final $file $outFile $outFile.tmp.render done ''; installPhase = '' cp -rv dist $out ''; }