summaryrefslogtreecommitdiffhomepage
path: root/web/site-page.sh
blob: dc08c950d35338e0407183495ee1ee4ea54e5ceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/sh
# web/site-page.sh -- one page of the Hugo site from a document in the tree.
#
# Usage: sh web/site-page.sh README.md 40 README https://<repo>/blob/main
#
# Prints the page on standard output: a front-matter block, then the document
# with its logo block, author line, `Contents` list and its own `# ` heading
# removed -- the site has a logo in the header, a contents list in the sidebar
# and the title in the front matter.  Links are rewritten on the way: a link
# from one document into the other becomes a link to that other page, and a
# link to any other file in the tree becomes a link to that file on GitHub,
# which is where it can be read.
#
# This used to be an inline awk program in the Makefile, where make expanded
# the `$` in `$/` and `$0` as one-letter variables and handed awk a program
# with a syntax error, so the README page was never written and nothing said
# so.
set -eu

if [ $# -ne 4 ]; then
    echo "usage: $0 FILE WEIGHT TITLE REPOURL" >&2
    exit 2
fi

src=$1
weight=$2
title=$3
repo=$4

[ -f "$src" ] || { echo "$0: $src: no such file" >&2; exit 2; }
# The front matter names the document in the tree, not the path it was read by.
base=${src##*/}

printf '%s\n' '---' "title: $title" "weight: $weight" "source: $base" '---' ''

awk '
    /^<p align="center">Sean Kurtz<\/p>$/ { next }
    /^<p align="center">$/ { logo = 1; next }
    logo { if ($0 == "</p>") { logo = 0 }; next }
    /^## Contents$/ { toc = 1; next }
    toc && /^## / { toc = 0 }
    toc { next }
    !h1 && /^# / { h1 = 1; next }
    { print }
' "$src" |
    sed -e 's|](DESIGN.md#|](../design/#|g' \
        -e 's|](README.md#|](../readme/#|g' \
        -e 's|](DESIGN.md)|](../design/)|g' \
        -e 's|](README.md)|](../readme/)|g' \
        -e "s|](\([A-Za-z][A-Za-z0-9_/.-]*\.md\)#|]($repo/\1#|g" \
        -e "s|](\([A-Za-z][A-Za-z0-9_/.-]*\.md\))|]($repo/\1)|g" \
        -e "s|](LICENSE)|]($repo/LICENSE)|g"