#!/bin/bash
main() {
opt_save=
opt_host=
while getopts 'sh:' opt; do
case "$opt" in
(s) opt_save=1;;
(h) opt_host=$OPTARG;;
esac
done
shift $((OPTIND-1))
opt_file=${1:?need file}
if [ -n "$opt_save" ]; then
dest=$opt_file
dest=${dest%.*}.html
exec > "$dest"
fi
if [ -n "$opt_host" ]; then
url=https://patchbay.pub/$opt_host
printf '%s\n' "$url"
exec > >(curl --data-binary @- "$url")
fi
title=$(sed -ne '/^\.title/ { s/^\.title //; p; q; }' "$opt_file")
printf "$header" "$title"
awk "$roff2html" "$opt_file"
}
header='
%s | mediocre at best
'
roff2html='
$0 == "." { next }
{ cmd = "" }
/^\./ {
cmd = substr($1, 2);
arg = "";
for (i=2; i<=NF; ++i) {
arg = arg " " $(i);
}
arg = substr(arg, 2);
}
{ gsub(/\\&/, "\\ ") }
BEGINFILE { inmacro = 0 }
cmd == "de" {
inmacro = 1;
next;
}
cmd == "." {
inmacro = 0;
next;
}
inmacro { next }
BEGINFILE { seentitle = 0 }
cmd == "title" {
if (seentitle) printf "%s
", arg;
else printf "%s
", arg;
seentitle = 1;
}
cmd == "scode" {
printf "";
}
cmd == "ecode" {
printf "
";
}
cmd == "cli" {
printf "%s
\n", arg;
}
cmd == "tech" {
printf "%s
\n", arg;
}
cmd == "filesystem" {
printf "%s
\n", arg;
}
cmd == "P" {
printf "
";
}
BEGINFILE { nref = 0 }
cmd == "ref" {
printf "[%d] %s
", nref++, arg, arg;
}
cmd == "" {
print;
}
'
main "$@"