94 lines
2.1 KiB
Bash
94 lines
2.1 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() {
|
|
inter="$(mktemp)"
|
|
tmp="$(mktemp)"
|
|
|
|
[ -n "${2:-}" ] && outfile="$2" || outfile="$1"
|
|
|
|
while grep -q -E -e '<!--sh[[:space:]]*' "$1"; do
|
|
sh -s > "$inter" <<-EOF
|
|
$(awk -v FS='<!--sh[[:space:]]+' -v RS='[[:space:]]+-->' \
|
|
'NF > 1 {print $NF; exit}' "$1")
|
|
EOF
|
|
# can this be made smaller?
|
|
# the goal is to replace all text between
|
|
# <!--sh and --> with the contents of another file
|
|
# minus the newline at the end
|
|
awk -v repfile="$inter" '
|
|
BEGIN {
|
|
while (getline var < repfile) {
|
|
a[i++] = var
|
|
}
|
|
close(repfile)
|
|
FS="<!--sh[[:space:]]+"
|
|
RS="[[:space:]]+-->"
|
|
ORS=""
|
|
}
|
|
|
|
NF > 1 && NR == 1 {
|
|
print $1
|
|
for (n=0; n < (i - 1); n++) {
|
|
print a[n] "\n"
|
|
}
|
|
print a[n]
|
|
FS=" "
|
|
RS=ORS="\n"
|
|
}
|
|
|
|
NR > 1 { print $0 }' "$1" > "$tmp"
|
|
|
|
mv "$tmp" "$outfile"
|
|
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
|