#!/bin/sh
{ trampoline=";" "exec" "awk" "-f" "$0" "$@"; }
BEGIN {
if (head) HEAD = slurp(head)
else HEAD = "
{{FILENAME}}" \
"{{FILENAME}}
"
if (foot) FOOT = slurp(foot)
else FOOT = "
"
if (ihead) IHEAD = slurp(ihead)
else IHEAD = "{{DIRECTORY}}" \
"{{DIRECTORY}}
"
if (ifoot) IFOOT = slurp(ifoot)
else IFOOT = ""
if (out) OUTD = out
else OUTD = "out/"
if (readmefilter) RMFL = readmefilter
else RMFL = "cat"
if (system("mkdir -p " OUTD)) exit 1
"pwd" | getline TMPLV["DIRECTORY"]; close("pwd")
sub(/.*\//, "", TMPLV["DIRECTORY"])
}
FNR == 1 {
if (NR > 1) finish()
OUTFILE = outfn(FILENAME)
TMPLV["FILENAME"] = FILENAME
TMPLV["OUTFILE"] = OUTFILE
FILES[f++] = FILENAME
printf("%s -> %s\n", FILENAME, OUTFILE)
OUTSTR = template_replace(HEAD)
}
END {
finish()
doindex()
}
{
# Sanitize HTML
gsub(/&/, "\\&"); gsub(/, "\\<"); gsub(/>/, "\\>")
# Wrap the line in a span with a line number link
OUTSTR = OUTSTR sprintf("" \
"%d" \
"%s\n",
FNR, FNR, FNR, $0)
}
function slurp (file, o) {
if (!file) return 0
while ((getline < file) > 0)
o = o (o?"\n":"") $0
return o
}
function outfn (file, norepldir) {
if (!norepldir) sub(/^/, OUTD, file)
sub(/$/, ".html", file)
return file
}
function template_replace (str) {
for (ts in TMPLV) {
gsub("{{"ts"}}", TMPLV[ts], str)
}
return str
}
function finish () {
sub("\n$", "", OUTSTR)
OUTSTR = OUTSTR template_replace(FOOT)
print(OUTSTR) > OUTFILE
close(OUTFILE)
}
function doindex () {
INDEX = OUTD "index.html"
printf("building index: %s\n", INDEX)
print(template_replace(IHEAD)) > INDEX
print("") >> INDEX
for (f in FILES) {
printf("- %s
\n",
outfn(FILES[f], 1), FILES[f]) >> INDEX
}
print("
") >> INDEX
if (readme) {
while (((RMFL " " readme) | getline) > 0)
print >> INDEX
}
print(template_replace(IFOOT)) >> INDEX
close(INDEX)
}