From c2a3fda0cf2aef901d1280bea792bf109c6d3d73 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Mon, 18 Mar 2024 00:23:16 -0500 Subject: Add templating, static files; add sample template+style --- schwa.awk | 105 +++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 74 insertions(+), 31 deletions(-) (limited to 'schwa.awk') diff --git a/schwa.awk b/schwa.awk index a3f4f3c..015b374 100755 --- a/schwa.awk +++ b/schwa.awk @@ -1,24 +1,22 @@ #!/bin/sh -{ trampoline=";" "exec" "awk" "-f" "$0" "$@"; } +{ trampoline=";" "exec" "awk" "-f" "$0" "$@"; } # -*- awk -*- BEGIN { - if (head) HEAD = slurp(head) - else HEAD = "
"
- if (foot) FOOT = slurp(foot)
- else FOOT = "
"
- if (ihead) IHEAD = slurp(ihead)
- else IHEAD = "" desc "
" + else TMPLV["DESCRIPTION"] = "" "pwd" | getline TMPLV["DIRECTORY"]; close("pwd") - sub(/.*\//, "", TMPLV["DIRECTORY"]) + TMPLV["DIRECTORY"] = outfn(TMPLV["DIRECTORY"], 3) + copy_statics() } FNR == 1 { @@ -28,10 +26,15 @@ FNR == 1 { TMPLV["OUTFILE"] = OUTFILE FILES[f++] = FILENAME printf("%s -> %s\n", FILENAME, OUTFILE) - OUTSTR = template_replace(HEAD) + if (system("mkdir -p " OUTD outfn(FILENAME, 2))) + die(1, "Can't make directory " outfn(FILENAME, 2)) + if (!system("cp " FILENAME " " outfn(FILENAME, 4))) + TMPLV["RAWFILE"] = outfn(FILENAME, 3) + OUTSTR = "" } END { + if (dead) exit dead finish() doindex() } @@ -53,40 +56,80 @@ function slurp (file, o) { return o } -function outfn (file, norepldir) { - if (!norepldir) sub(/^/, OUTD, file) - sub(/$/, ".html", file) +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) - OUTSTR = OUTSTR template_replace(FOOT) - print(OUTSTR) > OUTFILE + if (OUTFILE == OUTD "index.html") { + TMPLV["FILENAME"] = TMPLV["DIRECTORY"] + OUTSTR = "" OUTSTR "
"
+ }
+ print(template_replace(TEMPLATE)) > 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
+ 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] " " outfn(STATICS[s], 4)))
+ die(2, "Can't copy static file: " STATICS[s])
}
- print(template_replace(IFOOT)) >> INDEX
- close(INDEX)
}
--
cgit 1.4.1-21-gabe81