89 lines
1.9 KiB
Bash
89 lines
1.9 KiB
Bash
#!/bin/sh
|
|
set -euf
|
|
|
|
BASE="$PWD"
|
|
export RESDIR="${RESOURCEDIR:-"${BASE}/include"}"
|
|
export BUILDDIR="${BUILDDIR:-"${BASE}/build"}"
|
|
export SRCDIR="${SRCDIR:-"${BASE}/src"}"
|
|
|
|
cleanup() {
|
|
[ -f "${inter:-}" ] && rm "$inter"
|
|
[ -f "${tmp:-}" ] && rm "$tmp"
|
|
[ -f "${filetmp:-}" ] && rm "$filetmp"
|
|
true
|
|
}
|
|
|
|
|
|
# process an html formatted file
|
|
# if a second argument is given, that is the output file
|
|
# otherwise, the output file is $BUILDDIR/<filepath>
|
|
process_html() {
|
|
tmp="$(mktemp)"
|
|
|
|
# only one use of grep to be efficient
|
|
i="$(grep -Ece '<!--sh[[:space:]]+' "$1")"
|
|
|
|
while [ $i -gt 0 ]; do
|
|
awk -v FS='<!--sh[[:space:]]+' -v RS='[[:space:]]+-->' \
|
|
'NF > 1 {print $NF; exit}' "$1" \
|
|
| sh -s | \
|
|
awk '
|
|
BEGIN {
|
|
RS="\a"
|
|
getline var
|
|
RS="<!--sh[[:space:]]+"
|
|
FS="[[:space:]]+-->"
|
|
OFS=" -->"
|
|
ORS=""
|
|
}
|
|
|
|
FNR == 2 {
|
|
printf substr(var, 1, length(var)-1)
|
|
for (i = 2; i < NF; i++)
|
|
print $i " -->"
|
|
print $i
|
|
RS="\a"
|
|
}
|
|
|
|
FNR == 1
|
|
FNR > 2 { print "<!--sh " $0 }
|
|
' - "$1" > "$tmp"
|
|
|
|
mv -f "$tmp" "$1"
|
|
i=$((i-1))
|
|
done
|
|
}
|
|
|
|
# process a pure markdown file
|
|
# uses a special file in RESDIR, `markdown-template.sh` to create the html
|
|
# TODO: finish this
|
|
# for now, port the rest of unix.dog
|
|
process_md() {
|
|
tmpmd="$(mktemp)"
|
|
lowdown --html-no-skiphtml --html-no-escapehtml -o "$tmpmd" "$1"
|
|
mv "$tmpmd" "$1"
|
|
process_html "$1"
|
|
}
|
|
|
|
trap 'cleanup' INT HUP QUIT EXIT
|
|
|
|
# 99% of the time you probably want to be here
|
|
cd "$RESDIR"
|
|
find "$SRCDIR" -type d -exec sh -c 'for d; do mkdir -p "$BUILDDIR"/"${d#"$SRCDIR"}"; done' sh {} +
|
|
|
|
while read -r file; do
|
|
filetmp="$(mktemp)"
|
|
cp -p "$file" "$filetmp"
|
|
case "$file" in
|
|
*.[hH][tT][mM][lL]) process_html "$filetmp" ;;
|
|
*.[mM][dD]) process_md "$filetmp" ;;
|
|
*) ;;
|
|
esac
|
|
if ! cmp -s "$filetmp" "$BUILDDIR"/"${file#"$SRCDIR"}"; then
|
|
mv "$filetmp" "$BUILDDIR"/"${file#"$SRCDIR"}"
|
|
fi
|
|
cleanup
|
|
done <<-EOF
|
|
$(find "$SRCDIR" -type f)
|
|
EOF
|