#!/bin/sh { trampoline=";" "exec" "awk" "-f" "$0" "$@"; } # -*- awk -*- BEGIN { if (template) TEMPLATE = slurp(template) else TEMPLATE = \ "
" desc "
" else TMPLV["DESCRIPTION"] = "" if (root) TMPLV["ROOT"] = root else TMPLV["ROOT"] = "/" "pwd" | getline TMPLV["DIRECTORY"]; close("pwd") TMPLV["DIRECTORY"] = outfn(TMPLV["DIRECTORY"], 3) } FNR == 1 { if (NR > 1) finish() OUTFILE = outfn(FILENAME) TMPLV["FILENAME"] = FILENAME TMPLV["OUTFILE"] = OUTFILE FILES[f++] = FILENAME printf("%s -> %s\n", FILENAME, OUTFILE) if (system("mkdir -p " OUTD outfn(FILENAME, 2))) die(1, "Can't make directory: " outfn(FILENAME, 2)) if (!system("cp " FILENAME " " outfn(FILENAME, 4) ".txt")) TMPLV["RAWFILE"] = outfn(FILENAME, 3) ".txt" OUTSTR = "" } END { if (dead) exit dead finish() doindex() copy_statics() } { # 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, mod) { if (!mod) { # foo.txt => OUTD/foo.txt.html sub(/^/, OUTD, file) sub(/$/, ".html", file) } else if (mod == 1) { # foo.txt => foo.txt.html sub(/$/, ".html", file) } else if (mod == 2) { # foo.txt => / ; foo/bar.txt => foo/ if (!sub(/\/[^\/]*$/, "/", file)) file = "/" } else if (mod == 3) { # foo/bar.txt => bar.txt (basename) sub(/.*\//, "", file) } else if (mod == 4) { # foo/bar.txt => OUTD/foo/bar.txt sub(/^/, OUTD, file) } return file } function template_replace (str) { if (OUTFILE == OUTD "index.html") { gsub(//, "", str) } else { gsub(//, "", str) } for (ts in TMPLV) { gsub("{{"ts"}}", TMPLV[ts], str) } gsub(/&/, "\\\\&", OUTSTR) sub("{{CONTENT}}", OUTSTR, str) return str } function finish () { sub("\n$", "", OUTSTR) if (OUTFILE == OUTD "index.html") { TMPLV["FILENAME"] = TMPLV["DIRECTORY"] OUTSTR = "" OUTSTR "
"
}
print(template_replace(TEMPLATE)) > OUTFILE
close(OUTFILE)
}
function doindex () {
OUTFILE = OUTD "index.html"
OUTSTR = ""
printf("building index: %s\n", OUTFILE)
for (f in FILES) {
OUTSTR = OUTSTR \
sprintf("