about summary refs log tree commit diff stats
path: root/vienna
blob: 3d6de65c7b9485cabb27bce66eaae0521df20946 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/bin/sh
# vienna --- a tiny, tasty ssg
# by C. Duckworth <acdw@acdw.net>
# shellcheck disable=1090,2030,2031,2035

### Entry point

usage() {
	cat <<EOF
VIENNA: a tiny, tasty ssg
by C. Duckworth <acdw@acdw.net>

USAGE: vienna [-h]
       vienna [-q] [-c CONFIG] [-C DIR] [-o DIR] [-r URL] [COMMAND...]

FLAGS:
 -h         view this help
 -q         don't output any diagnostic information

OPTIONS:
 -c FILE    use FILE as configuration. [default: $VIENNA_CONFIG]
 -C DIR     operate in DIR, instead of \$PWD.
 -o DIR     output built site to DIR. [default: ./out]
 -r URL     use URL as basis for urls. [default: example.com]

COMMANDS:
 init	    initialize a vienna site with minimalist defaults and exit.
 clean      remove output directory before building.
 preview    preview the site locally after building.
            NOTE: this requires a .vienna.sh file in the site root.
            you'll have to define a \`preview' function there.
 publish    publish the site after building.
            NOTE: this requires a .vienna.sh file in the site root.
            you'll have to define a \`publish' function there.

you can redefine any variables or functions vienna uses in \$VIENNA_CONFIG,
which by default is ./.vienna.sh.  vienna uses heredoc-inspired templating, so
you can include shell snippets and variables by doubling the dollar signs.

EOF
	exit "${1:-0}"
}

configure() {
	## Set up environment
	URL_ROOT="${VIENNA_URL_ROOT:-https://www.example.com}"
	TEMPDIR="${VIENNA_TEMPDIR:-/tmp/vienna}"
	WORKDIR="${VIENNA_WORKDIR:-$PWD}"
	OUTDIR="${VIENNA_OUTDIR:-out}"
	PLUGINDIR="${VIENNA_PLUGINDIR:-.plugins}"
	CONFIG="${VIENNA_CONFIG:-./.vienna.sh}"
	# Templates
	PAGE_TEMPLATE="${VIENNA_PAGE_TEMPLATE:-.page.tmpl.html}"
	INDEX_TEMPLATE="${VIENNA_INDEX_TEMPLATE:-.index.tmpl.html}"
	FEED_TEMPLATE="${VIENNA_FEED_TEMPLATE:-.feed.tmpl.xml}"
	# Options
	PHTML_OPTIONS="${VIENNA_PHTML_OPTIONS:-expand entities}"
	# File extensions
	RAW_PAGE_EXTENSION="${VIENNA_RAW_PAGE_EXTENSION:-htm}"
	# Logging
	LOG=true
	## Parse command line arguments
	while getopts hqC:c:o:r: opt; do
		case "$opt" in
			h) usage 0 ;;
			q) LOG=false ;;
			C) WORKDIR="$OPTARG" ;;
			c)
				CONFIG="$OPTARG"
				# To error later if a config is specified on the command
				# line but doesn't exist.
				CONFIG_ARG=1
				;;
			o) OUTDIR="$OPTARG" ;;
			r) URL_ROOT="$OPTARG" ;;
			*) exit 1 ;;
		esac
	done
	## Initialize state
	FILE=
	## Cleanup after we're done
	trap cleanup INT QUIT
}

main() {
	# State predicates
	alias pagep=false indexp=false feedp=false
	# Convenience aliases
	alias body=cat title='meta title' pubdate='meta date'
	# Configure
	configure "$@"
	shift "$((OPTIND - 1))"
	# Prepare
	cd "$WORKDIR" || exit 2
	# Source config
	if test -f "$CONFIG"; then
		# Source ./.vienna.sh, if it exists.
		. "$CONFIG"
	elif test -n "$CONFIG_ARG"; then
		# If a -c option was passed on the command line but the file
		# doesn't exist, that's an error.  If we're just looking for the
		# default file, however, there is no error---the user might want
		# to use the default configuration.
		log error "Can't find configuration \`$CONFIG'."
		exit 2
	else
		print >&2 "I'm not sure this is a \`vienna' site directory."
		if yornp "Initialize? (y/N)"; then
			initialize
		else
			yornp "Continue building? (y/N)" || exit 2
		fi
	fi
	# Further argument processing --- pre-build
	preprocess "$@" || shift
	log vienna config
	# Log configuration variables
	log config 'base url': "$URL_ROOT"
	log config 'work dir': "$WORKDIR"
	log config output: "$OUTDIR"
	log template page: "$PAGE_TEMPLATE"
	log template index: "$INDEX_TEMPLATE"
	log template feed: "$FEED_TEMPLATE"
	# Plugins
	for plugin in "$PLUGINDIR"/*.sh; do
		test -f "$plugin" || continue
		log plugin "$plugin"
		. "$plugin"
	done
	# Prepare output directories
	mkdir -p "$OUTDIR" || exit 2
	mkdir -p "$TEMPDIR" || exit 2
	log vienna build
	# Build pages
	alias pagep=true
	genpage *."$RAW_PAGE_EXTENSION" || exit 2
	alias pagep=false
	# Build index
	alias indexp=true
	genlist index_item "$INDEX_TEMPLATE" *."$RAW_PAGE_EXTENSION" >"$OUTDIR/index.html" || exit 2
	alias indexp=false
	# Build feed
	alias feedp=true
	genlist feed_item "$FEED_TEMPLATE" *."$RAW_PAGE_EXTENSION" >"$OUTDIR/feed.xml" || exit 2
	alias feedp=false
	# Copy static files
	static * || exit 2
	# Further argument processing --- post-build
	postprocess "$@"
}

preprocess() {
	case "${1:-ok}" in
		ok) ;;
		init)
			shift
			initialize "$@" # exit
			;;
		clean)
			log vienna clean
			rm -r "$OUTDIR"
			cleanup
			if [ $# -eq 0 ]; then
				exit # Quit when only cleaning
			else
				return 1 # Otherwise, continue processing
			fi
			;;
	esac
}

postprocess() {
	case "${1:-ok}" in
		ok) ;;
		publish)
			log vienna publish
			publish "$OUTDIR"
			;;
		preview)
			log vienna preview
			preview "$OUTDIR"
			;;
		*)
			log error "Don't know command \`$1'."
			exit 1
			;;
	esac
}

cleanup() {
	test -z "$DEBUG" &&
		test -z "$NODEP_RM" &&
		rm -r "$TEMPDIR"
}

_publish() {
	cat <<EOF >&2

I want to publish your website but I don't know how.
$(if test -f "$CONFIG"; then
		echo "Edit the"
	else
		echo "Write a"
	fi) \`publish' function in the \`$CONFIG' file in this
directory that tells me what to do.

EOF
	exit 3
}
publish() { _publish; }

_preview() {
	cat <<EOF >&2

I want to show you a preview of your website but I don't
know how.  $(if test -f "$CONFIG"; then
		echo "Edit the"
	else
		echo "Write a"
	fi) \`preview' function in the \`$CONFIG' file
in this directory that tells me what to do.

EOF
	exit 3
}
preview() { _preview; }

initialize() { # initialize
	log init "$CONFIG"
	cat >"$CONFIG" <<EOF
# .vienna.sh

# Basic configuration variables
URL_ROOT="$URL_ROOT"
OUTDIR="$OUTDIR"
PLUGINDIR="$PLUGINDIR"
PAGE_TEMPLATE="$PAGE_TEMPLATE"
INDEX_TEMPLATE="$INDEX_TEMPLATE"
FEED_TEMPLATE="$FEED_TEMPLATE"
PHTML_OPTIONS="$PHTML_OPTIONS"

# Edit this with your web server's file path
SERVER_ROOT=

# The following functions are examples.  Make sure they fit your own needs
# before uncommenting them.

# preview() {
#     export _PYTHON="$(command -v python3 || command -v python)"
#     export URL_ROOT=http://localhost:8000
#     if [ -n "$_PYTHON" ]; then
# 	pkill -x "$_PYTHON"
# 	"$_PYTHON" -m http.server -d "$OUTDIR" &
# 	export _VIENNA_PID="$!"
# 	find . -type f -not -path "*/$OUTDIR/*" |
#             OUTDIR="$OUTDIR" entr -rp \
# 		sh -c 'kill "$_VIENNA_PID" >/dev/null 2>&1;
# 	    vienna && "$_PYTHON" -m http.server -d "$OUTDIR"'
#     else
# 	log error "\\\`python' not found."
# 	_preview
#     fi
# }

# publish() {
#     if [ -n "$SERVER_ROOT" ]; then
# 	rsync -avzP --delete "$OUTDIR/" "$SERVER_ROOT/"
#     else
# 	_publish
#     fi
# }
EOF
	log init "$PAGE_TEMPLATE"
	cat >"$PAGE_TEMPLATE" <<\EOF
<title>$$(title)</title>
$$(body)
EOF
	log init "$INDEX_TEMPLATE"
	cat >"$INDEX_TEMPLATE" <<\EOF
<title>a home page!</title>
<h1>hey! it's a home page of some sort!</h1>
<ul>
$$(body)
</ul>
EOF
	log init "$FEED_TEMPLATE"
	cat >"$FEED_TEMPLATE" <<\EOF
<rss version="2.0">
  <channel>
    <title>a feed!</title>
    <link>$$BASEURL</link>
    $$(body)
  </channel>
</rss>
EOF
	exit
}

### Utility

log() {
	if "$LOG"; then
		printf >&2 '[%s] ' "$1"
		shift
		printf >&2 "%s\t" "$@"
		echo >&2
	fi
}

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

yornp() { # yornp PROMPT
	printf >&2 '%s \n' "$@"
	read -r yn
	case "$yn" in
		[Nn]*) return 1 ;;
		[Yy]*) return 0 ;;
		*) return 2 ;;
	esac
}

### File processing

## Building block functions

shellfix() { # shellfix FILE...
	## Replace ` with \`, $ with \$, and $$ with $
	# shellcheck disable=2016
	sed -E \
		-e 's/`/\\`/g' \
		-e 's/\$\$\$/\\&/g' \
		-e 's/(^|[^\$])\$([^\$]|$)/\1\\$\2/g' \
		-e 's/\$\$/$/g' \
		"$@"
}

expand() { # expand TEMPLATE... < INPUT
	## Print TEMPLATE to stdout, expanding shell constructs.
	end="expand_:_${count:=0}_:_end"
	eval "$(
		echo "cat<<$end"
		shellfix "$@"
		echo
		echo "$end"
	)" && count=$((count + 1))
}

phtml() { # phtml < INPUT
	## Output HTML, pretty much.
	# Paragraphs unadorned with html tags will be wrapped in <p> tags, and
	# &, <, > will be escaped unless prepended with \.  Paragraphs where the
	# first character is < will be left as-is, excepting indentation on the
	# first line (an implementation detail).
	case "$PHTML_OPTIONS" in
		*entities*)
			_entities='s#([^\\])&#\1\&amp;#g;
	    s#([^\\])<#\1\&lt;#g;
	    s#([^\\])>#\1\&gt;#g;
	    s#\\([&<>])#\1#g;'
			;;
		*) _entities= ;;
	esac
	sed -E '
    /./ {H;$!d}; x
    s#^[ \n\t]+([^<].*)#\1#
    t par; b end
    :par
	'"$_entities"'
    	s#.*#<p>&</p>#
    :end
	s#^[ \n\t]+##
        $!a
'
}

meta_init() { # meta_init FILE
	## Extract metadata from FILE for later processing.
	# Metadata should exist as colon-separated data in HTML comments in the
	# input file.
	m=false
	t=false
	while read -r line; do
		case "$line" in
			'<!--') m=true ;;
			'-->') m=false ;;
			*title:*) t=true && print "$line" ;;
			*) "$m" && print "$line" ;;
		esac
	done <"$1"
	if ! "$t"; then
		title="${1##*/}"
		title="${title%.*}"
		print "title: ${title%.*}"
	fi
}

meta() { # meta FIELD [FILE]
	## Extract metadata FIELDS from INPUT.
	# FILE gives the filename to save metadata to in the $WORKDIR.  It
	# defaults to the current value for $FILE.
	#
	# Metadata should exist as colon-separated data in an HTML comment at
	# the beginning of an input file.
	sed -n "s/^[ \t]*$1:[ \t]*//p" <"${2:-$META}"
}

## Customizable bits

filters() { # filters < INPUT
	## The filters to run input through.
	# This is a good candidate for customization in .vienna.sh.
	phtml |
		case "$PHTML_OPTIONS" in
			*expand*) expand ;;
			*) cat ;;
		esac
}

### Site building

genpage() { # genpage PAGE...
	## Compile PAGE(s) into $OUTDIR for publication.
	# Outputs a file of the format $OUTDIR/<PAGE>/index.html.
	test -f "$PAGE_TEMPLATE" || return 1
	for FILE; do
		test -f "$FILE" || continue
		log genpage "$FILE"
		outd="$OUTDIR/${FILE%.$RAW_PAGE_EXTENSION}"
		outf="$outd/index.html"
		tmpf="$TEMPDIR/$FILE.tmp"
		META="$TEMPDIR/$FILE.meta"
		mkdir -p "$outd"
		meta_init "$FILE" >"$META"
		filters <"$FILE" >"$tmpf"
		expand "$PAGE_TEMPLATE" <"$tmpf" >"$outf"
	done
}

genlist() { # genlist PERITEM_FUNC TEMPLATE_FILE PAGE...
	## Generate a list.
	peritem_func="$1"
	template_file="$2"
	tmpf="$TEMPDIR/$1"
	shift 2 || return 2
	test -f "$template_file" || return 1
	printf '%s\n' "$@" | sort_items |
		while read -r FILE; do
			test -f "$FILE" || continue
			log genlist "$peritem_func:" "$FILE"
			LINK="$URL_ROOT${URL_ROOT:+/}${FILE%.$RAW_PAGE_EXTENSION}"
			META="$TEMPDIR/$FILE.meta"
			"$peritem_func" "$FILE"
		done | expand "$template_file"
}

sort_items() { # sort_items < ITEMS
	## Sort ITEMS separated by newlines.
	# This function assumes that no ITEM contains a newline.
	cat
}

index_item() { # index_item PAGE
	## Construct a single item in an index.html.
	print "<li><a href=\"$LINK\">$(meta title "$1")</a></li>"
}

feed_item() { # feed_item PAGE
	## Construct a single item in an RSS feed.
	date="$(meta date "$1")"
	cat <<EOF
<item>
  <title>$(meta title "$1")</title>
  <link>$LINK</link>
  <guid>$LINK</guid>
  $(test -n "$date" && print "<pubDate>$date</pubDate>")
  <description><![CDATA[$(filters <"$1")]]></description>
</item>
EOF
}

static() { # static FILE...
	## Copy static FILE(s) to $OUTDIR as-is.
	# Performs a simple heuristic to determine whether to copy a file or
	# not.
	for FILE; do
		test -f "$FILE" || continue
		case "$FILE" in
			.*) continue ;;
			*.htm) continue ;;
			"$OUTDIR") continue ;;
			*) cp -r "$FILE" "$OUTDIR/" ;;
		esac
	done
}

### Do the thing!

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