1
2
3
4
5 basename() {
6 dir=${1%${1##*[!/]}}
7 dir=${dir##*/}
8 dir=${dir%"$2"}
9 printf '%s\n' "${dir:-/}"
10 }
11
12 dirname() {
13 dir=${1:-.}
14 dir=${dir%%"${dir##*[!/]}"}
15 [ "${dir##*/*}" ] && dir=.
16 dir=${dir%/*}
17 dir=${dir%%"${dir##*[!/]}"}
18 printf '%s\n' "${dir:-/}"
19 }
20
21
22
23
24
25 port=5000
26
27
28 pre=$(grep -Pzo '[^$]+(?=!CONTENT!)' template.html | tr -d '\0')
29 post=$(grep -Pzo '(?<=!CONTENT!)[^$]+' template.html | tr -d '\0')
30
31
32 process () {
33 out=${2/.${out#*.}/.html}
34 mkdir -p "$(dirname "$out")"
35 printf "\nBuilding $1 -> $out\n"
36
37 x=${out%%.html}
38 x=${x##out/}
39 header="${pre/!TITLE!/$x}"
40 printf '%s' "$header" > "$out"
41 $3 "$1" 1>> "$out"
42 printf '%s' "$post" >> "$out"
43 }
44
45
46 build () {
47
48 out=${1/src/out}
49
50 case "${out#*.}" in
51
52 "html")
53 cp -fv "$1" "$out"
54 return
55 ;;
56
57 "fmt.txt")
58 process "$1" "$out" 'env MARKDOWN_COMPAT=1; ENABLE_HEADERS=1; ENABLE_CODE_LINES=1; bash tool/pond.sh'
59 ;;
60
61 "md")
62 process "$1" "$out" 'env MARKDOWN_COMPAT=1 bash tool/pond.sh'
63 ;;
64 esac
65
66 }
67
68 case $1 in
69
70
71 live)
72 live-server --no-browser --port=$port --ignore=out --ignore=src &
73 printf "$(find src -name "*.fmt.txt")\n$(realpath ./template.html)\n$(find tool -name "*.sh")" | entr ./make
74 ;;
75
76
77 index)
78 while read -r file; do
79 filename=${file%%.html}
80 printf "<a href=\"$file\">${filename/out\//}</a>\n"
81 done < <(find out/ -type f -not -name "index.html")
82 ;;
83
84
85 serve)
86 python3 -m http.server $port &
87 ;;
88 clean)
89 rm -r out index.html
90 ;;
91
92
93 *)
94 mkdir -p src out
95 for file in ${2:-src/**/*.fmt.txt src/*.fmt.txt}; do
96 build "$file"
97 done
98 mv ./out/index.html .
99 printf "\nFinished!\n"
100 ;;
101 esac