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
# vienna --- a tiny, tasty ssg
# by C. Duckworth <acdw@acdw.net>
# shellcheck disable=1090,2035
### Entry point
usage() {
cat <<EOF
VIENNA: a tiny, tasty ssg
by C. Duckworth <acdw@acdw.net>
USAGE: vienna [-h]
vienna [-C DIR] [-d DOMAIN] [-o DIR] [COMMAND]
FLAGS:
-h view this help
OPTIONS:
-C DIR operate in DIR, instead of \$PWD.
-d DOMAIN use DOMAIN as basis for urls. default: example.com
-o DIR output built site to DIR. default: ./out
PARAMETERS:
clean remove output directory before building
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.
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.
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
DOMAIN="${VIENNA_DOMAIN:-https://www.example.com}"
TMPD="${VIENNA_TMPD:-/tmp/vienna}"
WORKD="${VIENNA_WORKD:-$PWD}"
OUTD="${VIENNA_OUTD:-out}"
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}"
# File extensions
PAGE_RAW_EXT="${VIENNA_PAGE_RAW_EXT:-htm}"
## Source ./.vienna.sh, if it exists.
# Sourcing it here lets it override any environment variables, and it to
# be overridden by command-line flags.
test -f "$CONFIG" && . "$CONFIG"
## Parse command line arguments
while getopts d:C:o:h opt; do
case "$opt" in
d) DOMAIN="$OPTARG" ;;
C) WORKD="$OPTARG" ;;
o) OUTD="$OPTARG" ;;
h) usage 0 ;;
*) exit 1 ;;
esac
done
## Log configuration variables
echo >&2 "[config] domain: $DOMAIN"
echo >&2 "[config] workdir: $WORKD"
echo >&2 "[config] output: $OUTD"
## 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))"
# Further argument processing --- pre-build
case "${1:-ok}" in
ok) ;;
clean)
echo >&2 "[vienna] clean"
rm -r "$OUTD"
exit
;;
esac
# Prepare
cd "$WORKD" || exit 2
mkdir -p "$OUTD" || exit 2
mkdir -p "$TMPD" || exit 2
# Build pages
alias pagep=true
build *."$PAGE_RAW_EXT" || exit 2
alias pagep=false
# Build index
alias indexp=true
index *."$PAGE_RAW_EXT" || exit 2
alias indexp=false
# Build feed
alias feedp=true
feed *."$PAGE_RAW_EXT" || exit 2
alias feedp=false
# Copy static files
static * || exit 2
# Further argument processing --- post-build
case "${1:-ok}" in
ok) ;;
publish)
echo >&2 "[vienna] publish"
publish "$OUTD"
;;
preview)
echo >&2 "[vienna] preview"
preview "$OUTD"
;;
*)
echo >&2 "[vienna] Don't know command \`$1'."
exit 1
;;
esac
}
cleanup() {
test -z "$DEBUG" &&
test -z "$NODEP_RM" &&
rm -r "$TMPD"
}
publish() {
cat <<EOF >&2
I want to publish your website but I don't know how.
Make a $CONFIG file in this directory and write a \`publish'
function telling me what to do. I recommend using \`rsync',
but you live your life."
EOF
exit 3
}
preview() {
cat <<EOF >&2
I want to show you a preview of your website but I don't
know how. Make a $CONFIG file in this directory and write
a \`preview' function telling me what to do. I recommend
using something like \`python -m http.server', but you live
your life."
EOF
exit 3
}
### File processing
## Building block functions
shellfix() { # shellfix FILE...
## Replace ` with \`, $ with \$, and $$ with $
# shellcheck disable=2016
sed -E \
-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).
sed -E \
'/./{H;1h;$!d;}; x;
s#^[ \n\t]+[^<].*#&#;
t par; b;
:par;
s#([^\\])&#\1\&#g; s#\\&#\&#g;
s#([^\\])<#\1\<#g; s#\\<#<#g;
s#([^\\])>#\1\>#g; s#\\>#>#g;'
}
meta() { # meta FIELD [FILE] < INPUT
## Extract metadata FIELDS from INPUT.
# FILE gives the filename to save metadata to in the $WORKD. 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.
field="$1"
file="${2:-$FILE}"
metafile="$TMPD/${file}.meta"
test -f "$metafile" ||
sed '/<!--/n;/-->/q' >"$metafile"
sed -n "s/^[ \t]*$field:[ \t]*//p" <"$metafile"
}
## Customizable bits
filters() { # filters < INPUT
## The filters to run input through.
# This is a good candidate for customization in .vienna.sh.
expand | phtml
}
### Site building
build() { # build PAGE...
## Compile PAGE(s) into $OUTD for publication.
# Outputs a file of the format $OUTD/<PAGE>/index.html.
test -f "$PAGE_TEMPLATE" || return 1
for FILE; do
echo >&2 "[build] $FILE"
outd="$OUTD/${FILE%.$PAGE_RAW_EXT}"
outf="$outd/index.html"
tmpf="$TMPD/$FILE.tmp"
mkdir -p "$outd"
filters <"$FILE" >"$tmpf"
expand "$PAGE_TEMPLATE" <"$tmpf" >"$outf"
done
}
index() { # index PAGE...
## Build a site index from all PAGE(s) passed to it.
# Wraps each PAGE in a <li><a> structure.
test -f "$INDEX_TEMPLATE" || return 1
for FILE; do
echo >&2 "[index] $FILE"
link="$DOMAIN${DOMAIN:+/}${FILE%.$PAGE_RAW_EXT}"
echo "<li><a href=\"$link\">$(meta title "$FILE")</a></li>"
done | expand "$INDEX_TEMPLATE" >"$OUTD/index.html"
}
feed() { # feed PAGE...
## Build an RSS 2.0 feed from PAGE(s).
test -f "$FEED_TEMPLATE" || return 1
for FILE; do
echo >&2 "[feed] $FILE"
link="$DOMAIN${DOMAIN:+/}${FILE%.$PAGE_RAW_EXT}"
date="$(meta pubdate "$FILE")"
echo "<item>"
echo "<title>$(meta title "$FILE")</title>"
echo "<link>$link</link>"
echo "<guid>$link</guid>"
test -n "$date" && echo "<pubDate>$date</pubDate>"
echo "</item>"
done | expand "$FEED_TEMPLATE" >"$OUTD/feed.xml"
}
static() { # static FILE...
## Copy static FILE(s) to $OUTD as-is.
# Performs a simple heuristic to determine whether to copy a file or
# not.
for FILE; do
case "$FILE" in
.*) continue ;;
*.htm) continue ;;
"$OUTD") continue ;;
*) cp -r "$FILE" "$OUTD/" ;;
esac
done
}
### Do the thing!
test -n "$DEBUG" && set -x
test -n "$SOURCE" || main "$@"
|