blob: 50e4d9964e5b1e26086726925ef075de0c22060f (
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
#!/bin/sh
### Code:
usage() {
cat <<\EOF
ht: Build a website
Usage: ht -h
ht [OPTIONS...] FILE...
FLAGS:
-h Show this help and exit.
-B Treat all targets as "new" (like in `make').
OPTIONS:
-u BASEURL The base URL of the built site. Default: https://example.com
-o OUTD Output built files to OUTD. Default: ./out/
-w WORKD Use WORKD as the working directory. Default: /tmp/ht/
-p PROC Use PROC to process each input file. Default: ./ht.awk
-P TEMPLATE Use TEMPLATE as the page template. Default: ./page.tmpl.htm
-i ITEMFMT Format individual items with ITEMFMT in the site index.
-I TEMPLATE Use TEMPLATE as the index template. Default: ./index.tmpl.htm
-f ITEMFMT Format individual items with ITEMFMT in the site feed.
-F TEMPLATE Use TEMPLATE as the feed template. Default: ./feed.tmpl.xml
-s DIRECTORY The DIRECTORY holding static files (images, css, etc.).
Default: ./static/
-S DIRECTORY The DIRECTORY to copy static files to. Default: ./out
PARAMETERS:
FILE... The list of files to include in the website.
EOF
exit "${1:-0}"
}
main() {
configure "$@"
shift $((OPTIND - 1))
test $# -eq 0 && usage 1
prepare
static_copy
for input; do
case "$input" in
_* | */_*) continue ;;
*) page_write "$PAGE_TEMPLATE" "$input" ;;
esac
done
if okp && [ -n "$INDEXEACH" ]; then
index_write "$INDEX_TEMPLATE" "$INDEXEACH" "$OUTD/$INDEXNAME"
fi
if okp && [ -n "$FEEDEACH" ]; then
index_write "$FEED_TEMPLATE" "$FEEDEACH" "$OUTD/$FEEDNAME"
fi
printf 'Done. ' >&2
if okp; then
eprint "No errors reported."
else
eprint "There were errors."
fi
}
htawk_resolve() {
if ! command -v ht.awk 2>/dev/null; then
print ./ht.awk
fi
}
configure() {
OUTD="${HT_OUT_DIR:-./out}"
WORKD="${HT_WORKD:-/tmp/ht}"
PROC="${HT_PROC:-$(htawk_resolve)}"
BASEURL="${HT_BASEURL:-https://example.com}"
PAGE_TEMPLATE="${HT_PAGE_TEMPLATE:-./page.tmpl.htm}"
INDEX_TEMPLATE="${HT_INDEX_TEMPLATE:-./index.tmpl.htm}"
FEED_TEMPLATE="${HT_FEED_TEMPLATE:-./feed.tmpl.xml}"
ALLNEW=false
STATICD="${HT_STATIC_DIR:-./static}"
STATICOUT="${HT_STATIC_OUTPUT_DIR:-${OUTD}}"
INDEXNAME="${HT_INDEX_NAME:-index.html}"
if [ -n "$HT_INDEX_ITEM_FORMAT" ]; then
INDEXEACH="$HT_INDEX_ITEM_FORMAT"
else
# shellcheck disable=2016
INDEXEACH='<li><a href=\"/${file}/\">${title}</a></li>'
fi
FEEDNAME="${HT_FEED_NAME:-feed.xml}"
if [ -n "$HT_FEED_ITEM_FORMAT" ]; then
FEEDEACH="$HT_FEED_ITEM_FORMAT"
else
# shellcheck disable=2016
FEEDEACH='<entry>
<id>$BASEURL/$file</id>
<link rel=\"alternate\" href=\"$BASEURL/$file/\" />
<title>${title}</title>
<updated>$(date -u -d "@${time}" -R)</updated>
<author>$AUTHOR</author>
<content type=\"text/html\">
<![CDATA[$(cat "$WORKD/${file}.body")]]>
</content>
</entry>'
fi
# shellcheck disable=2034 # BASEURL is used in templates
while getopts ho:w:p:P:i:I:f:F:u:Bs:S: opt; do
case "$opt" in
h) usage ;;
o) OUTD="$OPTARG" ;;
w) WORKD="$OPTARG" ;;
p) PROC="$OPTARG" ;;
P) PAGE_TEMPLATE="$OPTARG" ;;
i) INDEXEACH="$OPTARG" ;;
I) INDEX_TEMPLATE="$OPTARG" ;;
f) FEEDEACH="$OPTARG" ;;
F) FEED_TEMPLATE="$OPTARG" ;;
u) BASEURL="$OPTARG" ;;
B) ALLNEW=true ;;
s) STATICD="$OPTARG" ;;
S) STATICOUT="$OPTARG" ;;
*) usage 1 ;;
esac
done
STATICOUT="${HT_STATIC_OUTPUT_DIR:-${OUTD}}"
INDEX="$WORKD/index.txt"
}
prepare() {
test -n "$WORKD" && rm -rf "$WORKD"
mkdir -p "$OUTD" "$WORKD"
if ! test -x "$PROC"; then
eprint "Can't find processor: $PROC"
exit 2
fi
touch "$WORKD/ok"
}
### Utilities
print() {
printf '%s\n' "$*"
}
eprint() {
print "$@" >&2
}
okp() {
test -f "$WORKD/ok"
}
notok() {
rm "$WORKD/ok" 2>/dev/null
}
olderp() { # olderp REF OTHER
# Is REF older than OTHER ? Necessary b/c test -ot is not POSIX.
a="$1"
b="$2"
if a_age="$(stat -c%Y "$a" 2>/dev/null)"; then
a_exist=true
else
a_exist=false
fi
if b_age="$(stat -c%Y "$b" 2>/dev/null)"; then
b_exist=true
else
b_exist=false
fi
if $a_exist && ! $b_exist; then
# B doesn't exist -- so B is newer
# eprint "$a is older than $b (doesn't exist)"
return 0
elif ! $a_exist && $b_exist; then
# A doesn't exist -- so A is newer
# eprint "$a (doesn't exist) is newer than $b"
return 1
else
# A's age > B's age ?
# eprint "$a ($a_age) <= $b ($b_age)?"
test "$a_age" -le "$b_age"
fi
}
uptodate() { # uptodate FILE DEPENDENCIES... # && return
# Check if FILE is up-to-date with DEPENDENCIES.
$ALLNEW && return 1
uptodate=1
file="$1"
shift
for dep in "$@"; do
olderp "$dep" "$file" && uptodate=0
done
return $uptodate
}
### File conversion
template_expand() { # template_expand TEMPLATES...
if test "$noproc"; then
cat -
else
end="tmpl_$(date +%s)_${count:=0}"
eval "$(
print "cat <<$end"
cat "$@"
print
print "$end"
)" && count=$((count + 1))
fi
}
meta_save() { # meta_save META_FILE < INPUT
sed -n "s/^;;@//p" 2>/dev/null | tee "$1"
}
meta_clear() { # meta_clear META_FILE
while read -r line; do
case "$line" in
*'()'*) unset -f "${line%()*}" ;;
*=*) unset -v "${line%=*}" ;;
*) ;;
esac
done <"$1" 2>/dev/null
}
page_write() { # html_write TEMPLATE INPUT
template="$1"
file="$2"
noproc= # File can set `noproc' in their metadata to not be processed.
if ! test -f "$file"; then
eprint "ERROR: File doesn't exist: $file"
notok
return 1
fi
fn="${file##*/}"
fn="${fn%%.*}"
out="${OUTD}/${fn}/index.html"
mkdir -p "${out%/*}"
meta="$WORKD/${fn}.meta"
body="$WORKD/${fn}.body"
eval "$(meta_save "$meta" <"$file")"
stat -c "%Y ${fn} $meta" "$file" >>"$INDEX"
# uptodate "$out" "$template" "$file" && return
eprint "Page: $file -> $out"
test "$noproc" || "$PROC" "$file" >"$body"
if ! [ -f "$body" ]; then
eprint "ERROR: Conversion: $file"
notok
return 2
fi
test "$noproc" || body="$(template_expand "${body}")"
if ! template_expand "$template" >"$out"; then
eprint "ERROR: Expansion: $file"
notok
return 3
fi
meta_clear "$meta"
}
index_write() { # index_write TEMPLATE EACH OUTFILE
template="$1"
each="$2"
out="$3"
if ! test -f "$INDEX"; then
: >"$INDEX"
fi
# uptodate "$out" "$template" "$INDEX" && return
eprint "Index: $out"
# shellcheck disable=2034 # file and time can be used in `each'
sort -k1,1 -nr "$INDEX" |
while IFS=' ' read -r time file meta; do
# shellcheck disable=1090
. "$meta"
# shellcheck disable=2154 # noindex is a file var
[ "$noindex" ] && continue
if ! item="$(eval print "\"$each\"")"; then
eprint "ERROR: couldn't add '$file' to $out"
notok
fi
print "$item"
meta_clear "$meta"
done |
template_expand "$template" >"$out"
}
### Static files
static_copy() { # static_copy
if ! test -d "$STATICD"; then
return 1
fi
mkdir -p "$STATICOUT"
for f in "$STATICD"/*; do
if [ -d "$f" ]; then
cp -r "$f" "$STATICOUT/"
else
cp "$f" "$STATICOUT/"
fi
done
}
### Do the thing
test "$DEBUG" && set -x
test "$SOURCE" || main "$@"
|