diff options
| author | Johannes Herman <johannes.herman@gmail.com> | 2026-03-16 11:14:21 +0100 |
|---|---|---|
| committer | Johannes Herman <johannes.herman@gmail.com> | 2026-03-16 11:14:21 +0100 |
| commit | caa30dd45d3ae76405a7f4ecc9641521c1fb8895 (patch) | |
| tree | 5641956935e8ef6cb487d47456b884443e5118e8 /bin | |
initial
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/categories | 12 | ||||
| -rwxr-xr-x | bin/make | 48 | ||||
| -rwxr-xr-x | bin/makepage | 36 | ||||
| -rwxr-xr-x | bin/makewritindex | 53 | ||||
| -rwxr-xr-x | bin/markup | 198 |
5 files changed, 347 insertions, 0 deletions
diff --git a/bin/categories b/bin/categories new file mode 100755 index 0000000..49b9d22 --- /dev/null +++ b/bin/categories @@ -0,0 +1,12 @@ +#!/bin/awk -f + +FNR == 2 { + sub(/^;[ ]*/, "") + for (i = 1; i <= NF; i++) + seen[$i] = 1 +} + +END { + for (w in seen) + printf "%s\n", w +} diff --git a/bin/make b/bin/make new file mode 100755 index 0000000..3d9c6fd --- /dev/null +++ b/bin/make @@ -0,0 +1,48 @@ +#!/bin/sh -eu + +. ./config + +title() { + [ -z "$1" ] && echo "$TITLE" && return + echo "${1##*/} - $TITLE" +} + +mkdir -p "$DIR_BUILD" + +echo ">> Building content" + +find "$DIR_CONTENT" -type f -name "*.$EXT_WRITINGS" | while read -r file; do + base=${file#"$DIR_CONTENT"/} + name=${base%."$EXT_WRITINGS"} + + outdir="$DIR_BUILD/$name" + if [ "$name" = "index" ]; then + output=$DIR_BUILD/index.html + name="" + else + output="$outdir/index.html" + mkdir -p "$outdir" + fi + + case "$name" in + *"${DIR_WRITINGS##*/}"*) name="$(sed -n '1s/^; //p' "$file")" ;; + esac + echo " $output" + markup < "$file" | makepage "$(title "$name")" > "$output" + +done + +echo ">> Generating writings indexes" + +mkdir -p "$DIR_WRITINGS_DST" +echo " $DIR_WRITINGS_DST/index.html" +makewritindex | makepage "$(title writings)" > "$DIR_WRITINGS_DST/index.html" + +for c in $(categories "$DIR_WRITINGS"/*."$EXT_WRITINGS"); do + mkdir -p "$DIR_WRITINGS_DST/$c" + echo " $DIR_WRITINGS_DST/$c/index.html" + makewritindex "$c" | makepage "$(title "#$c")" > "$DIR_WRITINGS_DST/$c/index.html" +done + +echo ">> Copying static content" +cp -rv "$DIR_STATIC"/* "$DIR_BUILD" | sed -E "s/.*-> '(.*)'/ \1/" diff --git a/bin/makepage b/bin/makepage new file mode 100755 index 0000000..386f0de --- /dev/null +++ b/bin/makepage @@ -0,0 +1,36 @@ +#!/bin/sh -eu + +. ./config + +title="${1:-$TITLE}" +content=$(cat) + +awk \ + -v title="$title" \ + -v content="$content" \ + -v header="$TEMP_HEAD" \ + -v footer="$TEMP_FOOT" \ + ' +function replace(str, pat, rep, out, parts, n, i) { + n = split(str, parts, pat) + out = parts[1] + for (i = 2; i <= n; i++) + out = out rep parts[i] + return out +} + +BEGIN { + while ((getline line < header) > 0) hbuf = hbuf line "\n" + close(header) + while ((getline line < footer) > 0) fbuf = fbuf line "\n" + close(footer) +} + +{ + line = $0 + line = replace(line, "{{TITLE}}", title) + line = replace(line, "{{CONTENT}}", content) + line = replace(line, "{{HEADER}}", hbuf) + line = replace(line, "{{FOOTER}}", fbuf) + print line +}' "$TEMP_PAGE" diff --git a/bin/makewritindex b/bin/makewritindex new file mode 100755 index 0000000..5562c85 --- /dev/null +++ b/bin/makewritindex @@ -0,0 +1,53 @@ +#!/bin/sh -e + +. ./config + +writings_by_cat() { + for file in "$DIR_WRITINGS"/*."$EXT_WRITINGS"; do + case "$(sed -n '2p' "$file") " in + *" $1 "*) echo "$file" ;; + esac + done +} + +if [ -z "$1" ]; then + cats="$(categories "$DIR_WRITINGS"/*."$EXT_WRITINGS")" + writings="$DIR_WRITINGS/*.$EXT_WRITINGS" + all=1 +else + cats="$1" + writings="$(writings_by_cat "$1")" +fi + +writings=$(for f in $writings; do + date="$(date -d "$(sed -n '3s/^; //p' "$f")" "+%s")" + printf '%s %s\n' "$date" "$f" +done | sort -rn | cut -f2 -d' ') + +cat <<EOF +<nav class="categories"> +$([ -z "$all" ] && echo "#<a class=\"category\" href=\"/writings/\">all</a>") +$(for c in $cats; do echo "#<a class=\"category\" href=\"/writings/$c\">$c</a>"; done) +</nav> +<ul class="writ-index"> +EOF + +for f in $writings; do + title=$(sed -n '1s/^; //p' "$f") + cats=$(sed -n '2s/^; //p' "$f") + date="$(date -d "$(sed -n '3s/^; //p' "$f")" "+%d %b %Y")" + file="$(basename "$f" .md)" + + cat_links=$(echo "$cats" | tr ' ' '\n' | while read -r cat; do + echo "#<a class=\"category\" href=\"/writings/$cat\">$cat</a>" + done | tr '\n' ' ') + + cat <<EOF +<li class="writing"> +<span>$date <a href="/writings/$file">$title</a></span> +<span>$cat_links</span> +</li> +EOF +done + +echo "</ul>" diff --git a/bin/markup b/bin/markup new file mode 100755 index 0000000..5b2ca88 --- /dev/null +++ b/bin/markup @@ -0,0 +1,198 @@ +#!/bin/gawk -f + +BEGIN { + list = "" + quote = 0 + code = 0 +} + +function handle_meta( i, n, cats, link) +{ + if ($0 ~ /^; /) { + if (meta_count == 0) { + } else if (meta_count == 1) { + sub(/^; /, "") + n = split($0, cats, " ") + printf "<nav class=\"categories\">" + printf "#<a class=\"category\" href=\"/writings/\">all</a> " + for (i = 1; i <= n; i++) { + link = "/writings/" cats[i] + printf "<a class=\"category\" href=\"" link "\">#" cats[i] "</a> " + } + print "</nav>" + } else if (meta_count == 2) { + sub(/^; /, "") + print "<p class=\"date\">" $0 "</p>" + } + meta_count++ + next + } +} +function handle_list_item(condition, type) +{ + if ($0 ~ condition) { + if (list != type) { + if (list != "") print "</" list ">" + print "<" type ">" + list = type + } + sub(condition, "") + print "<li>" $0 "</li>" + next + } +} + +function handle_quote() +{ + if ($0 == "\"\"\"") { + if (quote == 0) { + print "<blockquote>" + quote = 1 + } else { + print "</blockquote>" + quote = 0 + } + next + } +} + +function handle_codeblock() +{ + if ($0 ~ /^'''/) { + if (code == 0) { + if (list != "") { + print "</" list ">" + list = "" + } + print "<pre><code>" + code = 1 + } else { + print "</code></pre>" + code = 0 + } + next + } + + if (code == 1) { + gsub(/&/, "\\&") + gsub(/</, "\\<") + gsub(/>/, "\\>") + print + next + } +} + +function inline(delim, tag, escaped, pattern) +{ + escaped = delim + gsub(/\*/, "\\*", escaped) + gsub(/\./, "\\.", escaped) + + pattern = escaped "([^" escaped "]+)" escaped + while (match($0, pattern)) { + $0 = substr($0, 1, RSTART-1) \ + "<" tag ">" substr($0, RSTART+length(delim), \ + RLENGTH-length(delim)*2) "</" tag ">" \ + substr($0, RSTART+RLENGTH) + } +} + +function inline_link(pattern, result, pre, arr) +{ + pattern = "\\[([^]]+)\\]\\(([^)]+)\\)" + result = "" + while (match($0, pattern, arr)) { + pre = substr($0, 1, RSTART-1) + result = result pre "<a href=\"" arr[2] "\">" arr[1] "</a>" + $0 = substr($0, RSTART+RLENGTH) + } + $0 = result $0 +} + +function inline_image(pattern, result, pre, arr) +{ + pattern = "!\\[([^]]+)\\]\\(([^)]+)\\)" + result = "" + while (match($0, pattern, arr)) { + pre = substr($0, 1, RSTART-1) + result = result pre "<a href=\"" arr[2] "\">" "<img alt=\"" \ + arr[1] "\" src=\"" arr[2] "\">" "</a>" + $0 = substr($0, RSTART+RLENGTH) + } + $0 = result $0 +} + +function inline_footnote_ref(pattern, result, pre, arr) +{ + pattern = ".\\[\\^([^]]+)\\]" + result = "" + while (match($0, pattern, arr)) { + pre = substr($0, 1, RSTART) + result = result pre "<sup id=\"fnref-" \ + arr[1] "\"><a href=\"#fn-" arr[1] "\">" arr[1] "</a></sup>" + $0 = substr($0, RSTART+RLENGTH) + } + $0 = result $0 +} + +function handle_header(i, level) +{ + if ($0 ~ /^#+[ ]/) { + level = 0 + while (substr($0, level+1, 1) == "#") level++ + sub(/^#+[ ]+/, "") + print "<h" level ">" $0 "</h" level ">" + next + } +} + +function handle_footnote(pattern, arr) +{ + pattern = "^\\[\\^([^]]+)\\]:[ ]*(.*)" + if (match($0, pattern, arr)) { + if (footnote != 1) { + print "<ol class=\"footnotes\">" + footnote = 1 + } + print "<li id=\"fn-" arr[1] "\">" arr[2] \ + "<a href=\"#fnref-" arr[1] "\">↩</a></li>" + list = "ol" + next + } +} + +{ + handle_meta() + handle_quote() + handle_codeblock() + + inline("**", "b") + inline("*", "em") + inline("--", "s") + inline("'''", "code") + + inline_image() + inline_link() + + inline_footnote_ref() + + handle_list_item("^- ", "ul") + handle_list_item("^[0-9]+\\.[ ]+", "ol") + handle_footnote() + + if (list != "") { + print "</" list ">" + list = "" + } + + handle_header() + + if ($0 != "") { + print "<p>" $0 "</p>" + } +} + +END { + if (list != "") + print "</" list ">" +} |
