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 +++++++++++++++++++++++++++++++++++++++++-----------------
style.css | 79 +++++++++++++++++++++++++++++++++++++++++++
template.html | 31 +++++++++++++++++
3 files changed, 184 insertions(+), 31 deletions(-)
create mode 100644 style.css
create mode 100644 template.html
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 = "
{{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 (template) TEMPLATE = slurp(template)
+ else TEMPLATE = \
+ "{{FILENAME}}" \
+ "{{FILENAME}}
{{CONTENT}}"
if (out) OUTD = out
else OUTD = "out/"
if (readmefilter) RMFL = readmefilter
else RMFL = "cat"
- if (system("mkdir -p " OUTD)) exit 1
+ if (clone) TMPLV["CLONE"] = "" clone ""
+ else TMPLV["CLONE"] = ""
+ if (desc) TMPLV["DESCRIPTION"] = "" 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 = ""
+ 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 () {
- INDEX = OUTD "index.html"
- printf("building index: %s\n", INDEX)
- print(template_replace(IHEAD)) > INDEX
- print("") >> INDEX
+ OUTFILE = OUTD "index.html"
+ OUTSTR = ""
+ printf("building index: %s\n", OUTFILE)
for (f in FILES) {
- printf("- %s
\n",
- outfn(FILES[f], 1), FILES[f]) >> INDEX
+ OUTSTR = OUTSTR \
+ sprintf("- %s
\n",
+ outfn(FILES[f], 1), FILES[f])
}
- 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)
}
diff --git a/style.css b/style.css
new file mode 100644
index 0000000..b73b2dc
--- /dev/null
+++ b/style.css
@@ -0,0 +1,79 @@
+/* schwa example style */
+
+* { box-sizing: border-box; }
+
+body {
+ width: fit-content;
+ margin: auto;
+ padding: 1em;
+ background: gray;
+}
+
+main {
+ min-width: 40em;
+ background: white;
+ padding: 1em;
+ margin: 0;
+ border: 2px outset black;
+ border-top: none;
+}
+
+.ll {
+ display: inline-block;
+ width: 3em;
+ text-align: right;
+ padding-right: 0.5em;
+ margin-right: 0.5em;
+}
+
+header {
+ width: 100%;
+ color: white;
+ background: teal;
+ padding: 1em;
+ margin: 0;
+ border: 2px outset black;
+ border-bottom: none;
+ display: flex;
+ align-items: baseline;
+ justify-content: space-between;
+}
+
+header h1 {
+ margin: 4px;
+}
+
+footer, footer p {
+ margin: 0; padding: 4px;
+ text-align: right;
+ color: white;
+}
+
+header a:link {
+ text-decoration: none;
+ background: lightgray;
+ color: black;
+ padding: 4px;
+ border: 2px outset gray;
+}
+header a:visited {
+ color: black;
+}
+header a:hover {
+ border: 2px inset gray;
+}
+header a:active {}
+
+nav ul {
+ padding: 0; margin: 0;
+}
+
+nav li {
+ display: inline-block;
+}
+
+.clone, .desc {
+ font-style: italic;
+ text-align: right;
+ margin: 0;
+}
diff --git a/template.html b/template.html
new file mode 100644
index 0000000..c67ae0d
--- /dev/null
+++ b/template.html
@@ -0,0 +1,31 @@
+
+
+
+
+ {{FILENAME}}
+
+
+
+
+ {{DIRECTORY}}/{{FILENAME}}
+
+ {{DESCRIPTION}}
+
+
+
+
+
+
+ {{CONTENT}}
+
+
+
+
--
cgit 1.4.1-21-gabe81