website/build.sh

75 lines
1.6 KiB
Bash

#!/bin/sh
set -euf
BASE="$PWD"
export _INCDIR="${_INCDIR:-"${BASE}/include"}"
export _BUILDDIR="${_BUILDDIR:-"${BASE}/build"}"
export _SRCDIR="${_SRCDIR:-"${BASE}/src"}"
cleanup() {
[ -f "${filetmp:-}" ] && rm "$filetmp"
true
}
# process an html formatted file and output to stdout
process_html() {
__SH_FLAG=""
# SUBSHELL START #
cat "${1:--}" | (
while read -r __LINE; do
if [ -z "$__SH_FLAG" ]; then
case "$__LINE" in
*"<?sh"*)
printf '%s' "${__LINE%%<\?sh*}"
__LINE="${__LINE#*<\?sh}"
__SH_FLAG=1
;;
*) echo "$__LINE" ;;
esac
fi
if [ -n "$__SH_FLAG" ]; then
eval "${__LINE%%\?>*}"
case "$__LINE" in
*"?>"*)
printf '%s' "${__LINE##*\?>}"
__SH_FLAG=""
;;
*) ;;
esac
fi
done
)
# SUBSHELL END #
}
# process a pure markdown file
# uses a special file in _INCDIR, `markdown-template.sh` to create the html
# TODO: finish this
# for now, port the rest of unix.dog
process_md() {
lowdown --html-no-skiphtml --html-no-escapehtml "${1:--}" | process_html
}
trap 'cleanup' INT HUP QUIT EXIT
filetmp="$(mktemp -u)"
# 99% of the time you probably want to be here
cd "$_INCDIR"
find "$_SRCDIR" -type d -exec sh -c 'for d; do mkdir -p "$_BUILDDIR"/"${d#"$_SRCDIR"}"; done' sh {} +
while read -r file; do
case "$file" in
*.[hH][tT][mM][lL]) process_html < "$file" > "$filetmp" ;;
*.[mM][dD]) process_md < "$file" > "$filetmp" ;;
*) cp "$file" "$filetmp" ;;
esac
if ! cmp -s "$filetmp" "$_BUILDDIR"/"${file#"$_SRCDIR"}"; then
mv "$filetmp" "$_BUILDDIR"/"${file#"$_SRCDIR"}"
fi
done <<-EOF
$(find "$_SRCDIR" -type f)
EOF