#!/bin/sh { trampoline=";" "exec" "awk" "-f" "$0" "$@"; } # -*- awk -*- BEGIN { if (template) TEMPLATE = slurp(template) else TEMPLATE = \ "{{FILENAME}}" \ "

{{FILENAME}}

{{CONTENT}}" if (out) OUTD = out else OUTD = "out/" if (readmefilter) RMFL = readmefilter else RMFL = "cat" if (clone) TMPLV["CLONE"] = "" clone "" else TMPLV["CLONE"] = "" if (desc) TMPLV["DESCRIPTION"] = "

" 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(//, "\\>") # 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 = "" if (readme) { OUTSTR = OUTSTR "
" while (((RMFL " " readme) | getline) > 0) OUTSTR = OUTSTR "\n" $0 OUTSTR = OUTSTR "
" } } else { 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("
  • %s
  • \n", outfn(FILES[f], 1), FILES[f]) } finish() } function die(code, message) { print(message) > "/dev/stderr" dead = code exit code } function copy_statics () { split(static, STATICS, ":") for (s in STATICS) { printf("Copying %s\n", STATICS[s]) if (system("cp " STATICS[s] " " OUTD outfn(STATICS[s], 3))) die(2, "Can't copy static file: " STATICS[s]) } }