summary refs log tree commit diff stats
path: root/ht
blob: c8cef5c9f82315fa6d4e3d873e4de63e2a78b87f (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
#!/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/static.

PARAMETERS:
 FILE...	The list of files to include in the website.
EOF
	exit "${1:-0}"
}

main() {
	configure "$@"
	shift $((OPTIND - 1))
	prepare
	static_copy

	for input; do
		page_write "$PAGE_TEMPLATE" "$input"
	done

	if [ -n "$INDEXEACH" ]; then
		index_write "$INDEX_TEMPLATE" "$INDEXEACH" "$OUTD/$INDEXNAME"
	fi
	if [ -n "$FEEDEACH" ]; then
		index_write "$FEED_TEMPLATE" "$FEEDEACH" "$OUTD/$FEEDNAME"
	fi
	printf 'Done.  ' >&2
	if test -f "$WORKD/ok"; then
		eprint "No errors reported."
	else
		eprint "There were errors."
	fi
}

configure() {
	OUTD="${HT_OUT_DIR:-./out}"
	WORKD="${HT_WORKD:-/tmp/ht}"
	PROC="${HT_PROC:-./ht.awk}" # XXX: Needs better resolution
	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}/static}"

	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=\"$BASEURL/${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 hBo:w:p:i:I:f:F:u:s: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

	INDEX="$WORKD/index.txt"
}

prepare() {
	test -n "$WORKD" && rm -rf "$WORKD"
	mkdir -p "$OUTD" "$WORKD"
	test -x "$PROC" || {
		eprint "Can't find processor: $PROC"
		exit 2
	}
	touch "$WORKD/ok"
}

### Utilities

print() {
	printf '%s\n' "$*"
}

eprint() {
	print "$@" >&2
}

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...
	end="tmpl_$(date +%s)_${count:=0}"
	eval "$(
		print "cat <<$end"
		cat "$@"
		print
		print "$end"
	)"
	count=$((count + 1))
}

meta_save() { # meta_save [-c COMMENTCH] [-m METACH] META_FILE < INPUT
	COMMENTCH=';'
	METACH='@'
	while getopts c:m: opt; do
		case "$opt" in
		c) COMMENTCH="$OPTARG" ;;
		m) METACH="$OPTARG" ;;
		*) ;;
		esac
	done
	shift $((OPTIND - 1))
	sed -n "s/^${COMMENTCH}${COMMENTCH}${METACH}//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"

	fn="${file##*/}"
	fn="${fn%%.*}"
	out="${OUTD}/${fn}/index.html"
	mkdir -p "${out%/*}"
	meta="$WORKD/${fn}.meta"

	eval "$(meta_save "$meta" <"$file")"
	stat -c "%Y	${fn}	$meta" "$file" >>"$INDEX"

	uptodate "$out" "$template" "$file" && return

	eprint "Page:  $file -> $out"
	if
		! "$PROC" "$file" |
			tee "$WORKD/${fn}.body" |
			template_expand "$template" >"$out"
	then
		eprint "$file -> $out ... ERROR!"
		rm "$WORKD/ok"
		return
	fi
	meta_clear "$meta"
}

index_write() { # index_write TEMPLATE EACH OUTFILE
	template="$1"
	each="$2"
	out="$3"

	test -f "$INDEX" || return 1
	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"
			if ! item="$(eval print "\"$each\"")"; then
				eprint "ERROR: couldn't add '$file' to $out"
				rm "$WORKD/ok"
			fi
			print "$item"
			meta_clear "$meta"
		done |
		template_expand "$template" >"$out"
}

### Static files

static_copy() { # static_copy
	test -d "$STATICD" || return 1
	if command -v rsync 2>/dev/null; then
		eprint rsync -avz "$STATICD/" "$STATICOUT/"
		rsync -avz "$STATICD/" "$STATICOUT/"
	else
		eprint cp -r "$STATICD"/* "$STATICOUT/"
		cp -r "$STATICD"/* "$STATICOUT/"
	fi
}

### Do the thing

test "$DEBUG" && set -x
test "$SOURCE" || main "$@"