my website
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

buildblog: introduce default.json

chfour e8e0dfa8 5fdb6d00

+55 -27
+34 -15
buildblog.sh
··· 1 1 #!/usr/bin/env sh 2 2 3 - index_template='template_index.html' 4 - page_template='template_page.html' 3 + defaults_file='default.json' 4 + 5 5 template_start='<!--BEGIN TEMPLATE-->' 6 6 template_end='<!--END TEMPLATE-->' 7 7 8 - [ -n "${1}" ] && cd "${1}" 9 - ! [ -e "$index_template" ] && echo "${0}: error: ${1%/}/${index_template} does not exist" >&2 && exit 1 10 - ! [ -e "$page_template" ] && echo "${0}: error: ${1%/}/${page_template} does not exist" >&2 && exit 1 8 + rel_root='' 9 + [ -n "${1:+x}" ] && cd "${1}" && rel_root="${1%/}/" 10 + ! [ -e "$defaults_file" ] && echo "${0}: error: ${rel_root}${defaults_file} does not exist" && exit 1 11 + defaults="$(jq -c '.' "$defaults_file")" 12 + 13 + index_template="$(jq -r '.["$index-template"]' <<<"$defaults")" 14 + ! [ -e "$index_template" ] && echo "${0}: error: ${rel_root}${index_template} does not exist" >&2 && exit 1 11 15 12 16 # generate the code syntax highlighting css 13 17 # unfortunately pandoc did not like --template=<(echo ...) ··· 28 32 # this... thing turns the template in the html into a json string with jq string interpolation, to be passed back into jq 29 33 # thankfully this only has to be done once. cursedd 30 34 template="$(sed "/\s*${template_start}/,/\s*${template_end}/!d;//d" "${index_template}" | \ 31 - jq -Rs '"\"" + (gsub("(?<s>^|}})(?<p>.+?)(?<e>{{|$)"; "\(.s + (.p|@json|trimstr("\"")) + .e)"; "m") | rtrim | gsub("{{(?<p>.+?)}}"; "\\(\(.p))"; "m")) + "\""' -r)" 35 + jq -Rrs '"\"" + (gsub("(?<s>^|}})(?<p>.+?)(?<e>{{|$)"; "\(.s + (.p|@json|trimstr("\"")) + .e)"; "m") | rtrim | gsub("{{(?<p>.+?)}}"; "\\(\(.p))"; "m")) + "\""')" 32 36 33 - find . -name content.djot -print0 | sort -r -z | while read -r -d '' post; do 37 + find . -name meta.json -print0 | sort -r -z | while read -r -d '' post; do 34 38 post="${post%/*}" 35 39 echo -n "${post} " 36 40 slashesinpath="${post//[^\/]}" # delete everything that isnt a slash 37 41 slashesinpath="${#slashesinpath}" # count whats left (count slashes in $post) 38 42 backtoblogroot="$(for ((i=0; i < slashesinpath; i++)); do echo -n '../'; done)" 39 43 backtoblogroot="${backtoblogroot%/}" 40 - jq '.title' "${post}/meta.json" 41 - jq -r --arg path "${post}" "${template}" "${post}/meta.json" >> ./index.html 42 - pandoc -f djot -t html5 \ 43 - --mathml \ 44 - --template="${page_template}" \ 45 - --variable="backtoblogroot=${backtoblogroot}" \ 44 + 45 + # this thing is responsible for concatenating arrays together if for example both default and meta specify $pandoc-args 46 + # it also replaces every instance of {{root}} in $pandoc-args with backtoblogroot 47 + 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)))' \ 48 + --slurpfile meta "${post}/meta.json" --arg root "$backtoblogroot" <<<"$defaults")" 49 + 50 + jq '.title' <<<"$meta" 51 + 52 + # with help from domi https://donotsta.re/objects/b84298d7-582f-4b42-9bbd-bc5636a2b9fb 53 + pandoc_args=() 54 + while read -r -d '' arg; do pandoc_args+=("$arg"); done < \ 55 + <(jq --raw-output0 '.["$pandoc-args"][], .["$input"], "-o", .["$output"]' <<<"$meta") 56 + 57 + pushd "$post" >/dev/null 58 + pandoc_error=0 59 + pandoc \ 60 + --variable="root=${backtoblogroot}" \ 46 61 --variable="path=${post#./}" \ 47 - --metadata-file="${post}/meta.json" \ 48 - "${post}/content.djot" -o "${post}/index.html" 62 + --metadata-file=<(printf %s "$meta") \ 63 + "${pandoc_args[@]}" || pandoc_error=$? 64 + popd >/dev/null 65 + [ "$pandoc_error" != 0 ] && echo "warning: pandoc exited with code $pandoc_error, skipping" >&2 && continue 66 + 67 + jq -r --arg path "${post}" "${template}" <<<"$meta" >> ./index.html 49 68 done 50 69 51 70 sed -n "/${template_end}/,\$p" "${index_template}" >> ./index.html
-1
src/blog/2025-09-14-test/meta.json
··· 1 1 { 2 2 "title": "me and my stupid chud blog ssg child", 3 - "lang": "en", 4 3 "date": "2025-09-14", 5 4 "tags": ["test", "test2"] 6 5 }
-1
src/blog/2025-09-16-mastodon-stats/meta.json
··· 1 1 { 2 2 "title": "see how many times you've talked with someone on the fediverse", 3 - "lang": "en", 4 3 "date": "2025-09-16", 5 4 "tags": ["software", "short"], 6 5 "keywords": ["mastodon", "fediverse", "statistics"]
+11
src/blog/default.json
··· 1 + { 2 + "lang": "en", 3 + "$pandoc-args": [ 4 + "--from=djot", "--to=html5", 5 + "--mathml", 6 + "--template={{root}}/template_page.html" 7 + ], 8 + "$input": "content.djot", 9 + "$output": "index.html", 10 + "$index-template": "template_index.html" 11 + }
+1 -1
src/blog/template_index.html
··· 25 25 <ul> 26 26 <!--BEGIN TEMPLATE--> 27 27 <li><article> 28 - <a href="{{$path}}/">{{.title | @html}}</a> 28 + <a href="{{$path}}/{{if .["$output"] == "index.html" then "" else .["$output"] end}}">{{.title | @html}}</a> 29 29 (<time datetime="{{.date}}">{{.date}}</time>) 30 30 <span class="tags">{{.tags | map("<span class=\"tag\" data-tag=\"\(.)\">\(.)</span>") | join(" ")}}</span> 31 31 </article></li>
+9 -9
src/blog/template_page.html
··· 5 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 6 <meta name="generator" content="pandoc and a shell script"> 7 7 <title>$if(title-prefix)$$title-prefix$ &ndash; $endif$$pagetitle$</title> 8 - <link rel="stylesheet" href="$backtoblogroot$/../style.css"> 8 + <link rel="stylesheet" href="$root$/../style.css"> 9 9 <style> 10 10 #post-body { 11 11 max-width: 50rem; ··· 13 13 } 14 14 </style> 15 15 $if(highlighting-css)$ 16 - <link rel="stylesheet" href="$backtoblogroot$/highlighting.css"> 16 + <link rel="stylesheet" href="$root$/highlighting.css"> 17 17 $endif$ 18 18 $for(author-meta)$ 19 - <meta name="author" content="$author-meta$" /> 19 + <meta name="author" content="$author-meta$"> 20 20 $endfor$ 21 21 $if(keywords)$ 22 - <meta name="keywords" content="$for(keywords)$$keywords$$sep$, $endfor$" /> 22 + <meta name="keywords" content="$for(keywords)$$keywords$$sep$, $endfor$"> 23 23 $endif$ 24 24 $if(description-meta)$ 25 - <meta name="description" content="$description-meta$" /> 25 + <meta name="description" content="$description-meta$"> 26 26 $endif$ 27 27 $for(css)$ 28 - <link rel="stylesheet" href="$css$" /> 28 + <link rel="stylesheet" href="$css$"> 29 29 $endfor$ 30 30 $for(header-includes)$ 31 31 $header-includes$ ··· 33 33 </head> 34 34 <body> 35 35 <nav> 36 - <img src="$backtoblogroot$/../platinum_fop_45_o.svg" alt="" width="64" height="64" id="fops"> 36 + <img src="$root$/../platinum_fop_45_o.svg" alt="" width="64" height="64" id="fops"> 37 37 <ul class="inline-list slashed"> 38 - <li><a href="$backtoblogroot$/../">home</a></li> 39 - <li><a href="$backtoblogroot$/">blog</a></li> 38 + <li><a href="$root$/../">home</a></li> 39 + <li><a href="$root$/">blog</a></li> 40 40 </ul> 41 41 </nav> 42 42 <main>