From e8e0dfa8a25c7c129c570e5f7e8fedaa7ce8105e Mon Sep 17 00:00:00 2001 From: chfour Date: Sun, 19 Oct 2025 00:20:52 +0000 Subject: [PATCH] buildblog: introduce default.json --- buildblog.sh | 49 ++++++++++++++++++++++++++++++++++--------------- src/blog/2025-09-14-test/meta.json | 1 - src/blog/2025-09-16-mastodon-stats/meta.json | 1 - src/blog/default.json | 11 +++++++++++ src/blog/template_index.html | 2 +- src/blog/template_page.html | 18 +++++++++--------- 6 file(s) changed, 55 insertion(s)(+), 27 deletion(s)(-) diff --git a/buildblog.sh b/buildblog.sh --- a/buildblog.sh +++ b/buildblog.sh @@ -1,13 +1,17 @@ #!/usr/bin/env sh -index_template='template_index.html' -page_template='template_page.html' +defaults_file='default.json' + template_start='' template_end='' -[ -n "${1}" ] && cd "${1}" -! [ -e "$index_template" ] && echo "${0}: error: ${1%/}/${index_template} does not exist" >&2 && exit 1 -! [ -e "$page_template" ] && echo "${0}: error: ${1%/}/${page_template} does not exist" >&2 && exit 1 +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 # generate the code syntax highlighting css # unfortunately pandoc did not like --template=<(echo ...) @@ -28,24 +32,39 @@ # 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 -Rs '"\"" + (gsub("(?^|}})(?

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

.+?)}}"; "\\(\(.p))"; "m")) + "\""' -r)" + jq -Rrs '"\"" + (gsub("(?^|}})(?

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

.+?)}}"; "\\(\(.p))"; "m")) + "\""')" -find . -name content.djot -print0 | sort -r -z | while read -r -d '' post; do +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%/}" - jq '.title' "${post}/meta.json" - jq -r --arg path "${post}" "${template}" "${post}/meta.json" >> ./index.html - pandoc -f djot -t html5 \ - --mathml \ - --template="${page_template}" \ - --variable="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 + pandoc_args=() + while read -r -d '' arg; do pandoc_args+=("$arg"); done < \ + <(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="${post}/meta.json" \ - "${post}/content.djot" -o "${post}/index.html" + --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 diff --git a/src/blog/2025-09-14-test/meta.json b/src/blog/2025-09-14-test/meta.json --- a/src/blog/2025-09-14-test/meta.json +++ b/src/blog/2025-09-14-test/meta.json @@ -1,6 +1,5 @@ { "title": "me and my stupid chud blog ssg child", - "lang": "en", "date": "2025-09-14", "tags": ["test", "test2"] } diff --git a/src/blog/2025-09-16-mastodon-stats/meta.json b/src/blog/2025-09-16-mastodon-stats/meta.json --- a/src/blog/2025-09-16-mastodon-stats/meta.json +++ b/src/blog/2025-09-16-mastodon-stats/meta.json @@ -1,6 +1,5 @@ { "title": "see how many times you've talked with someone on the fediverse", - "lang": "en", "date": "2025-09-16", "tags": ["software", "short"], "keywords": ["mastodon", "fediverse", "statistics"] diff --git a/src/blog/default.json b/src/blog/default.json new file mode 100644 --- /dev/null +++ b/src/blog/default.json @@ -0,0 +1,11 @@ +{ + "lang": "en", + "$pandoc-args": [ + "--from=djot", "--to=html5", + "--mathml", + "--template={{root}}/template_page.html" + ], + "$input": "content.djot", + "$output": "index.html", + "$index-template": "template_index.html" +} diff --git a/src/blog/template_index.html b/src/blog/template_index.html --- a/src/blog/template_index.html +++ b/src/blog/template_index.html @@ -25,7 +25,7 @@