From 5e784f958649c61d6aa97e7df5916332f7c89014 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sun, 17 Mar 2024 18:42:01 -0500 Subject: Initial commit --- .gitignore | 1 + README.md | 4 +++ schwa.awk | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 schwa.awk diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c585e19 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +out \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6de1bf3 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# schwa +## Source Code Htmlizer Written in Awk + +Hey guess what loser, it's a source code htmlizer written in awk. diff --git a/schwa.awk b/schwa.awk new file mode 100755 index 0000000..a3f4f3c --- /dev/null +++ b/schwa.awk @@ -0,0 +1,92 @@ +#!/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(//, "\\>") + # 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 + if (readme) { + while (((RMFL " " readme) | getline) > 0) + print >> INDEX + } + print(template_replace(IFOOT)) >> INDEX + close(INDEX) +} -- cgit 1.4.1-21-gabe81