#!/usr/bin/env sh defaults_file='default.json' template_start='' template_end='' rel_root='' [ -n "${1:+x}" ] && cd "${1}" && rel_root="${1%/}/" ! [ -e "$defaults_file" ] && echo "${0}: error: ${rel_root}${defaults_file} does not exist" && exit 1 defaults="$(jq -c '.' "$defaults_file")" index_template="$(jq -r '.["$index-template"]' <<<"$defaults")" ! [ -e "$index_template" ] && echo "${0}: error: ${rel_root}${index_template} does not exist" >&2 && exit 1 sed "/${template_start}/q" "${index_template}" > ./index.html # this... thing turns the template in the html into a json string with jq string interpolation, to be passed back into jq # thankfully this only has to be done once. cursedd template="$(sed "/\s*${template_start}/,/\s*${template_end}/!d;//d" "${index_template}" | \ jq -Rrs '"\"" + (gsub("(?^|}})(?

.+?)(?{{|$)"; "\(.s + (.p|@json|trimstr("\"")) + .e)"; "m") | rtrim | gsub("{{(?

.+?)}}"; "\\(\(.p))"; "m")) + "\""')" find . -name meta.json -print0 | sort -r -z | while read -r -d '' post; do post="${post%/*}" echo -n "${post} " slashesinpath="${post//[^\/]}" # delete everything that isnt a slash slashesinpath="${#slashesinpath}" # count whats left (count slashes in $post) backtoblogroot="$(for ((i=0; i < slashesinpath; i++)); do echo -n '../'; done)" backtoblogroot="${backtoblogroot%/}" # this thing is responsible for concatenating arrays together if for example both default and meta specify $pandoc-args # it also replaces every instance of {{root}} in $pandoc-args with backtoblogroot meta="$(jq -c '$meta[0] as $meta | . as $default | $meta | [paths(type == "array")] | reduce .[] as $p ($meta; setpath($p; ($default | getpath($p) // []) + getpath($p))) | $default * . | .["$pandoc-args"] = (.["$pandoc-args"] | map(gsub("{{root}}"; $root)))' \ --slurpfile meta "${post}/meta.json" --arg root "$backtoblogroot" <<<"$defaults")" jq '.title' <<<"$meta" # with help from domi https://donotsta.re/objects/b84298d7-582f-4b42-9bbd-bc5636a2b9fb # and https://www.shellcheck.net/wiki/SC2046 mapfile -d '' pandoc_args < <(jq --raw-output0 '.["$pandoc-args"][], .["$input"], "-o", .["$output"]' <<<"$meta") pushd "$post" >/dev/null pandoc_error=0 pandoc \ --variable="root=${backtoblogroot}" \ --variable="path=${post#./}" \ --metadata-file=<(printf %s "$meta") \ "${pandoc_args[@]}" || pandoc_error=$? popd >/dev/null [ "$pandoc_error" != 0 ] && echo "warning: pandoc exited with code $pandoc_error, skipping" >&2 && continue jq -r --arg path "${post}" "${template}" <<<"$meta" >> ./index.html done sed -n "/${template_end}/,\$p" "${index_template}" >> ./index.html