#!/bin/sh
# ht.sh
# *.ht -> *html
# config
header_file=header.htm
footer_file=footer.htm
meta_file=meta.sh
# state
HTDAT="$(date +%s)"
HT_TMPL_COUNT=0
print() {
printf '%s\n' "$*"
}
htt() { # htt FILE
# Like `cat`, but with templating.
ht_end="ht_main_${HTDAT}_${HT_TMPL_COUNT}" # be extra double sure
eval "$(
print "cat <<$ht_end"
cat "$@"
print
print "$ht_end"
)"
HT_TMPL_COUNT=$((HT_TMPL_COUNT + 1))
}
htmeta_clear() {
# Generate metadata-clearing commands from $meta_file.
while read -r line; do
case "$line" in
*'()'*) # function
unset -f "${line%()*}"
;;
*=*) # variable assignment
unset -v "${line%=*}"
;;
*) # other -- XXX: Don't know what to do
;;
esac
done <"$meta_file"
}
htmeta() { # htmeta FILE
# Collect metadata from FILE.
# Metadata looks like this: `;;@`
sed -n 's/^;;@//p' "$1" | tee "$meta_file"
}
main() {
# Make two passes over each input file, collecting metadata and content.
:
# Of course, this isn't safe, but you trust yourself, right?
for file; do
eval "$(htmeta_clear)"
eval "$(htmeta "$file")"
./ht.awk <"$file" | htt "$header_file" - "$footer_file" >"${file}ml"
done
}