#!/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='