about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xvienna88
1 files changed, 48 insertions, 40 deletions
diff --git a/vienna b/vienna index f4c1e65..ca35c39 100755 --- a/vienna +++ b/vienna
@@ -44,9 +44,10 @@ EOF
44configure() { 44configure() {
45 ## Set up environment 45 ## Set up environment
46 DOMAIN="${VIENNA_DOMAIN:-https://www.example.com}" 46 DOMAIN="${VIENNA_DOMAIN:-https://www.example.com}"
47 TMPD="${VIENNA_TMPD:-/tmp/vienna}" 47 TEMPDIR="${VIENNA_TEMPDIR:-/tmp/vienna}"
48 WORKD="${VIENNA_WORKD:-$PWD}" 48 WORKDIR="${VIENNA_WORKDIR:-$PWD}"
49 OUTD="${VIENNA_OUTD:-out}" 49 OUTDIR="${VIENNA_OUTDIR:-out}"
50 PLUGINDIR="${VIENNA_PLUGINDIR:-.plugins}"
50 CONFIG="${VIENNA_CONFIG:-./.vienna.sh}" 51 CONFIG="${VIENNA_CONFIG:-./.vienna.sh}"
51 # Templates 52 # Templates
52 PAGE_TEMPLATE="${VIENNA_PAGE_TEMPLATE:-.page.tmpl.html}" 53 PAGE_TEMPLATE="${VIENNA_PAGE_TEMPLATE:-.page.tmpl.html}"
@@ -59,7 +60,7 @@ configure() {
59 ## Parse command line arguments 60 ## Parse command line arguments
60 while getopts C:c:d:ho:q opt; do 61 while getopts C:c:d:ho:q opt; do
61 case "$opt" in 62 case "$opt" in
62 C) WORKD="$OPTARG" ;; 63 C) WORKDIR="$OPTARG" ;;
63 c) 64 c)
64 CONFIG="$OPTARG" 65 CONFIG="$OPTARG"
65 # To error later if a config is specified on the command 66 # To error later if a config is specified on the command
@@ -68,7 +69,7 @@ configure() {
68 ;; 69 ;;
69 d) DOMAIN="$OPTARG" ;; 70 d) DOMAIN="$OPTARG" ;;
70 h) usage 0 ;; 71 h) usage 0 ;;
71 o) OUTD="$OPTARG" ;; 72 o) OUTDIR="$OPTARG" ;;
72 q) LOG=false ;; 73 q) LOG=false ;;
73 *) exit 1 ;; 74 *) exit 1 ;;
74 esac 75 esac
@@ -88,7 +89,10 @@ main() {
88 configure "$@" 89 configure "$@"
89 shift "$((OPTIND - 1))" 90 shift "$((OPTIND - 1))"
90 # Prepare 91 # Prepare
91 cd "$WORKD" || exit 2 92 cd "$WORKDIR" || exit 2
93 # Further argument processing --- pre-build
94 preprocess "$@" || shift
95 # Source config
92 if test -f "$CONFIG"; then 96 if test -f "$CONFIG"; then
93 # Source ./.vienna.sh, if it exists. 97 # Source ./.vienna.sh, if it exists.
94 . "$CONFIG" 98 . "$CONFIG"
@@ -104,31 +108,32 @@ main() {
104 preprocess "$@" || shift 108 preprocess "$@" || shift
105 ## Log configuration variables 109 ## Log configuration variables
106 log config domain: "$DOMAIN" 110 log config domain: "$DOMAIN"
107 log config workdir: "$WORKD" 111 log config workdir: "$WORKDIR"
108 log config output: "$OUTD" 112 log config output: "$OUTDIR"
109 log config page.tmpl: "$PAGE_TEMPLATE" 113 log template page: "$PAGE_TEMPLATE"
110 log config index.tmpl: "$INDEX_TEMPLATE" 114 log template index: "$INDEX_TEMPLATE"
111 log config feed.tmpl: "$FEED_TEMPLATE" 115 log template feed: "$FEED_TEMPLATE"
112 ## Plugins 116 # Plugins
113 # Plugins are *.sh files in $WORKD/.plugins/. They're sourced here. 117 for plugin in "$PLUGINDIR"/*.sh; do
114 for plugin in ./.*.sh; do
115 test -f "$plugin" || continue 118 test -f "$plugin" || continue
116 log plugin "$plugin" 119 log plugin "$plugin"
117 . "$plugin" 120 . "$plugin"
118 done 121 done
119 mkdir -p "$OUTD" || exit 2 122 # Prepare output directories
120 mkdir -p "$TMPD" || exit 2 123 mkdir -p "$OUTDIR" || exit 2
124 mkdir -p "$TEMPDIR" || exit 2
125 log vienna build
121 # Build pages 126 # Build pages
122 alias pagep=true 127 alias pagep=true
123 genpage *."$PAGE_RAW_EXT" || exit 2 128 genpage *."$PAGE_RAW_EXT" || exit 2
124 alias pagep=false 129 alias pagep=false
125 # Build index 130 # Build index
126 alias indexp=true 131 alias indexp=true
127 genlist index_item "$INDEX_TEMPLATE" *."$PAGE_RAW_EXT" >"$OUTD/index.html" || exit 2 132 genlist index_item "$INDEX_TEMPLATE" *."$PAGE_RAW_EXT" >"$OUTDIR/index.html" || exit 2
128 alias indexp=false 133 alias indexp=false
129 # Build feed 134 # Build feed
130 alias feedp=true 135 alias feedp=true
131 genlist feed_item "$FEED_TEMPLATE" *."$PAGE_RAW_EXT" >"$OUTD/feed.xml" || exit 2 136 genlist feed_item "$FEED_TEMPLATE" *."$PAGE_RAW_EXT" >"$OUTDIR/feed.xml" || exit 2
132 alias feedp=false 137 alias feedp=false
133 # Copy static files 138 # Copy static files
134 static * || exit 2 139 static * || exit 2
@@ -152,7 +157,7 @@ preprocess() {
152 ;; 157 ;;
153 clean) 158 clean)
154 log vienna clean 159 log vienna clean
155 rm -r "$OUTD" 160 rm -r "$OUTDIR"
156 cleanup 161 cleanup
157 if [ $# -eq 0 ]; then 162 if [ $# -eq 0 ]; then
158 exit # Quit when only cleaning 163 exit # Quit when only cleaning
@@ -168,11 +173,11 @@ postprocess() {
168 ok) ;; 173 ok) ;;
169 publish) 174 publish)
170 log vienna publish 175 log vienna publish
171 publish "$OUTD" 176 publish "$OUTDIR"
172 ;; 177 ;;
173 preview) 178 preview)
174 log vienna preview 179 log vienna preview
175 preview "$OUTD" 180 preview "$OUTDIR"
176 ;; 181 ;;
177 *) 182 *)
178 log error "Don't know command \`$1'." 183 log error "Don't know command \`$1'."
@@ -184,7 +189,7 @@ postprocess() {
184cleanup() { 189cleanup() {
185 test -z "$DEBUG" && 190 test -z "$DEBUG" &&
186 test -z "$NODEP_RM" && 191 test -z "$NODEP_RM" &&
187 rm -r "$TMPD" 192 rm -r "$TEMPDIR"
188} 193}
189 194
190_publish() { 195_publish() {
@@ -221,7 +226,8 @@ initialize() { # initialize
221 cat >"$CONFIG" <<EOF 226 cat >"$CONFIG" <<EOF
222# .vienna.sh 227# .vienna.sh
223DOMAIN="$DOMAIN" 228DOMAIN="$DOMAIN"
224OUTD="$OUTD" 229OUTDIR="$OUTDIR"
230PLUGINDIR="$PLUGINDIR"
225PAGE_TEMPLATE="$PAGE_TEMPLATE" 231PAGE_TEMPLATE="$PAGE_TEMPLATE"
226INDEX_TEMPLATE="$INDEX_TEMPLATE" 232INDEX_TEMPLATE="$INDEX_TEMPLATE"
227FEED_TEMPLATE="$FEED_TEMPLATE" 233FEED_TEMPLATE="$FEED_TEMPLATE"
@@ -233,12 +239,12 @@ preview() {
233 export _PYTHON="$(command -v python3 || command -v python)" 239 export _PYTHON="$(command -v python3 || command -v python)"
234 if [ -n "$_PYTHON" ]; then 240 if [ -n "$_PYTHON" ]; then
235 pkill -x "$_PYTHON" 241 pkill -x "$_PYTHON"
236 "$_PYTHON" -m http.server -d "$OUTD" & 242 "$_PYTHON" -m http.server -d "$OUTDIR" &
237 export _VIENNA_PID="$!" 243 export _VIENNA_PID="$!"
238 find . -type f -not -path "*/$OUTD/*" | 244 find . -type f -not -path "*/$OUTDIR/*" |
239 OUTD="$OUTD" entr -rp \ 245 OUTDIR="$OUTDIR" entr -rp \
240 sh -c 'kill "$_VIENNA_PID" >/dev/null 2>&1; 246 sh -c 'kill "$_VIENNA_PID" >/dev/null 2>&1;
241 vienna && "$_PYTHON" -m http.server -d "$OUTD"' 247 vienna && "$_PYTHON" -m http.server -d "$OUTDIR"'
242 else 248 else
243 log error "\`python' not found." 249 log error "\`python' not found."
244 _preview 250 _preview
@@ -247,16 +253,18 @@ preview() {
247 253
248publish() { 254publish() {
249 if [ -n "$WWW_ROOT" ]; then 255 if [ -n "$WWW_ROOT" ]; then
250 rsync -avzP --delete "$OUTD/" "$WWW_ROOT/" 256 rsync -avzP --delete "$OUTDIR/" "$WWW_ROOT/"
251 else 257 else
252 _preview 258 _preview
253 fi 259 fi
254} 260}
255EOF 261EOF
262 log init "$PAGE_TEMPLATE"
256 cat >"$PAGE_TEMPLATE" <<\EOF 263 cat >"$PAGE_TEMPLATE" <<\EOF
257<title>$$(title)</title> 264<title>$$(title)</title>
258$$(body) 265$$(body)
259EOF 266EOF
267 log init "$INDEX_TEMPLATE"
260 cat >"$INDEX_TEMPLATE" <<\EOF 268 cat >"$INDEX_TEMPLATE" <<\EOF
261<title>a home page!</title> 269<title>a home page!</title>
262<h1>hey! it's a home page of some sort!</h1> 270<h1>hey! it's a home page of some sort!</h1>
@@ -356,7 +364,7 @@ meta_init() { # meta_init FILE
356 364
357meta() { # meta FIELD [FILE] 365meta() { # meta FIELD [FILE]
358 ## Extract metadata FIELDS from INPUT. 366 ## Extract metadata FIELDS from INPUT.
359 # FILE gives the filename to save metadata to in the $WORKD. It 367 # FILE gives the filename to save metadata to in the $WORKDIR. It
360 # defaults to the current value for $FILE. 368 # defaults to the current value for $FILE.
361 # 369 #
362 # Metadata should exist as colon-separated data in an HTML comment at 370 # Metadata should exist as colon-separated data in an HTML comment at
@@ -375,16 +383,16 @@ filters() { # filters < INPUT
375### Site building 383### Site building
376 384
377genpage() { # genpage PAGE... 385genpage() { # genpage PAGE...
378 ## Compile PAGE(s) into $OUTD for publication. 386 ## Compile PAGE(s) into $OUTDIR for publication.
379 # Outputs a file of the format $OUTD/<PAGE>/index.html. 387 # Outputs a file of the format $OUTDIR/<PAGE>/index.html.
380 test -f "$PAGE_TEMPLATE" || return 1 388 test -f "$PAGE_TEMPLATE" || return 1
381 for FILE; do 389 for FILE; do
382 test -f "$FILE" || continue 390 test -f "$FILE" || continue
383 log genpage "$FILE" 391 log genpage "$FILE"
384 outd="$OUTD/${FILE%.$PAGE_RAW_EXT}" 392 outd="$OUTDIR/${FILE%.$PAGE_RAW_EXT}"
385 outf="$outd/index.html" 393 outf="$outd/index.html"
386 tmpf="$TMPD/$FILE.tmp" 394 tmpf="$TEMPDIR/$FILE.tmp"
387 META="$TMPD/$FILE.meta" 395 META="$TEMPDIR/$FILE.meta"
388 mkdir -p "$outd" 396 mkdir -p "$outd"
389 meta_init "$FILE" >"$META" 397 meta_init "$FILE" >"$META"
390 filters <"$FILE" >"$tmpf" 398 filters <"$FILE" >"$tmpf"
@@ -396,15 +404,15 @@ genlist() { # genlist PERITEM_FUNC TEMPLATE_FILE PAGE...
396 ## Generate a list. 404 ## Generate a list.
397 peritem_func="$1" 405 peritem_func="$1"
398 template_file="$2" 406 template_file="$2"
399 tmpf="$TMPD/$1" 407 tmpf="$TEMPDIR/$1"
400 shift 2 || return 2 408 shift 2 || return 2
401 test -f "$template_file" || return 1 409 test -f "$template_file" || return 1
402 printf '%s\n' "$@" | sort_items | 410 printf '%s\n' "$@" | sort_items |
403 while read -r FILE; do 411 while read -r FILE; do
404 test -f "$FILE" || continue 412 test -f "$FILE" || continue
405 log genlist "$peritem_func" "$FILE" 413 log genlist "$peritem_func:" "$FILE"
406 LINK="$DOMAIN${DOMAIN:+/}${FILE%.$PAGE_RAW_EXT}" 414 LINK="$DOMAIN${DOMAIN:+/}${FILE%.$PAGE_RAW_EXT}"
407 META="$TMPD/$FILE.meta" 415 META="$TEMPDIR/$FILE.meta"
408 "$peritem_func" "$FILE" 416 "$peritem_func" "$FILE"
409 done | expand "$template_file" 417 done | expand "$template_file"
410} 418}
@@ -432,7 +440,7 @@ feed_item() { # feed_item PAGE
432} 440}
433 441
434static() { # static FILE... 442static() { # static FILE...
435 ## Copy static FILE(s) to $OUTD as-is. 443 ## Copy static FILE(s) to $OUTDIR as-is.
436 # Performs a simple heuristic to determine whether to copy a file or 444 # Performs a simple heuristic to determine whether to copy a file or
437 # not. 445 # not.
438 for FILE; do 446 for FILE; do
@@ -440,8 +448,8 @@ static() { # static FILE...
440 case "$FILE" in 448 case "$FILE" in
441 .*) continue ;; 449 .*) continue ;;
442 *.htm) continue ;; 450 *.htm) continue ;;
443 "$OUTD") continue ;; 451 "$OUTDIR") continue ;;
444 *) cp -r "$FILE" "$OUTD/" ;; 452 *) cp -r "$FILE" "$OUTDIR/" ;;
445 esac 453 esac
446 done 454 done
447} 455}