diff options
author | Case Duckworth | 2024-03-18 00:23:16 -0500 |
---|---|---|
committer | Case Duckworth | 2024-03-18 00:23:16 -0500 |
commit | c2a3fda0cf2aef901d1280bea792bf109c6d3d73 (patch) | |
tree | 3576eb01b78742caf16e254afcd8ff0a1ea41f3d /schwa.awk | |
parent | Delete header (diff) | |
download | schwa-c2a3fda0cf2aef901d1280bea792bf109c6d3d73.tar.gz schwa-c2a3fda0cf2aef901d1280bea792bf109c6d3d73.zip |
Add templating, static files; add sample template+style
Diffstat (limited to 'schwa.awk')
-rwxr-xr-x | schwa.awk | 105 |
1 files changed, 74 insertions, 31 deletions
diff --git a/schwa.awk b/schwa.awk index a3f4f3c..015b374 100755 --- a/schwa.awk +++ b/schwa.awk | |||
@@ -1,24 +1,22 @@ | |||
1 | #!/bin/sh | 1 | #!/bin/sh |
2 | { trampoline=";" "exec" "awk" "-f" "$0" "$@"; } | 2 | { trampoline=";" "exec" "awk" "-f" "$0" "$@"; } # -*- awk -*- |
3 | 3 | ||
4 | BEGIN { | 4 | BEGIN { |
5 | if (head) HEAD = slurp(head) | 5 | if (template) TEMPLATE = slurp(template) |
6 | else HEAD = "<!DOCTYPE html><title>{{FILENAME}}</title>" \ | 6 | else TEMPLATE = \ |
7 | "<h1>{{FILENAME}}</h1><pre><code>" | 7 | "<!DOCTYPE html><title>{{FILENAME}}</title>" \ |
8 | if (foot) FOOT = slurp(foot) | 8 | "<h1>{{FILENAME}}</h1>{{CONTENT}}" |
9 | else FOOT = "</code></pre>" | ||
10 | if (ihead) IHEAD = slurp(ihead) | ||
11 | else IHEAD = "<!DOCTYPE html><title>{{DIRECTORY}}</title>" \ | ||
12 | "<h1>{{DIRECTORY}}</h1>" | ||
13 | if (ifoot) IFOOT = slurp(ifoot) | ||
14 | else IFOOT = "" | ||
15 | if (out) OUTD = out | 9 | if (out) OUTD = out |
16 | else OUTD = "out/" | 10 | else OUTD = "out/" |
17 | if (readmefilter) RMFL = readmefilter | 11 | if (readmefilter) RMFL = readmefilter |
18 | else RMFL = "cat" | 12 | else RMFL = "cat" |
19 | if (system("mkdir -p " OUTD)) exit 1 | 13 | if (clone) TMPLV["CLONE"] = "<span class=\"clone\">" clone "</span>" |
14 | else TMPLV["CLONE"] = "" | ||
15 | if (desc) TMPLV["DESCRIPTION"] = "<p class=\"desc\">" desc "</p>" | ||
16 | else TMPLV["DESCRIPTION"] = "" | ||
20 | "pwd" | getline TMPLV["DIRECTORY"]; close("pwd") | 17 | "pwd" | getline TMPLV["DIRECTORY"]; close("pwd") |
21 | sub(/.*\//, "", TMPLV["DIRECTORY"]) | 18 | TMPLV["DIRECTORY"] = outfn(TMPLV["DIRECTORY"], 3) |
19 | copy_statics() | ||
22 | } | 20 | } |
23 | 21 | ||
24 | FNR == 1 { | 22 | FNR == 1 { |
@@ -28,10 +26,15 @@ FNR == 1 { | |||
28 | TMPLV["OUTFILE"] = OUTFILE | 26 | TMPLV["OUTFILE"] = OUTFILE |
29 | FILES[f++] = FILENAME | 27 | FILES[f++] = FILENAME |
30 | printf("%s -> %s\n", FILENAME, OUTFILE) | 28 | printf("%s -> %s\n", FILENAME, OUTFILE) |
31 | OUTSTR = template_replace(HEAD) | 29 | if (system("mkdir -p " OUTD outfn(FILENAME, 2))) |
30 | die(1, "Can't make directory " outfn(FILENAME, 2)) | ||
31 | if (!system("cp " FILENAME " " outfn(FILENAME, 4))) | ||
32 | TMPLV["RAWFILE"] = outfn(FILENAME, 3) | ||
33 | OUTSTR = "" | ||
32 | } | 34 | } |
33 | 35 | ||
34 | END { | 36 | END { |
37 | if (dead) exit dead | ||
35 | finish() | 38 | finish() |
36 | doindex() | 39 | doindex() |
37 | } | 40 | } |
@@ -53,40 +56,80 @@ function slurp (file, o) { | |||
53 | return o | 56 | return o |
54 | } | 57 | } |
55 | 58 | ||
56 | function outfn (file, norepldir) { | 59 | function outfn (file, mod) { |
57 | if (!norepldir) sub(/^/, OUTD, file) | 60 | if (!mod) { # foo.txt => OUTD/foo.txt.html |
58 | sub(/$/, ".html", file) | 61 | sub(/^/, OUTD, file) |
62 | sub(/$/, ".html", file) | ||
63 | } else if (mod == 1) { # foo.txt => foo.txt.html | ||
64 | sub(/$/, ".html", file) | ||
65 | } else if (mod == 2) { # foo.txt => / ; foo/bar.txt => foo/ | ||
66 | if (!sub(/\/[^\/]*$/, "/", file)) | ||
67 | file = "/" | ||
68 | } else if (mod == 3) { # foo/bar.txt => bar.txt (basename) | ||
69 | sub(/.*\//, "", file) | ||
70 | } else if (mod == 4) { # foo/bar.txt => OUTD/foo/bar.txt | ||
71 | sub(/^/, OUTD, file) | ||
72 | } | ||
59 | return file | 73 | return file |
60 | } | 74 | } |
61 | 75 | ||
62 | function template_replace (str) { | 76 | function template_replace (str) { |
77 | if (OUTFILE == OUTD "index.html") { | ||
78 | gsub(/<!--\/NORMAL/, "", str) | ||
79 | gsub(/NORMAL-->/, "", str) | ||
80 | } else { | ||
81 | gsub(/<!--\/INDEX/, "", str) | ||
82 | gsub(/INDEX-->/, "", str) | ||
83 | } | ||
63 | for (ts in TMPLV) { | 84 | for (ts in TMPLV) { |
64 | gsub("{{"ts"}}", TMPLV[ts], str) | 85 | gsub("{{"ts"}}", TMPLV[ts], str) |
65 | } | 86 | } |
87 | gsub(/&/, "\\\\&", OUTSTR) | ||
88 | sub("{{CONTENT}}", OUTSTR, str) | ||
66 | return str | 89 | return str |
67 | } | 90 | } |
68 | 91 | ||
69 | function finish () { | 92 | function finish () { |
70 | sub("\n$", "", OUTSTR) | 93 | sub("\n$", "", OUTSTR) |
71 | OUTSTR = OUTSTR template_replace(FOOT) | 94 | if (OUTFILE == OUTD "index.html") { |
72 | print(OUTSTR) > OUTFILE | 95 | TMPLV["FILENAME"] = TMPLV["DIRECTORY"] |
96 | OUTSTR = "<ul>" OUTSTR "</ul>" | ||
97 | if (readme) { | ||
98 | OUTSTR = OUTSTR "<section id=\"readme\">" | ||
99 | while (((RMFL " " readme) | getline) > 0) | ||
100 | OUTSTR = OUTSTR "\n" $0 | ||
101 | OUTSTR = OUTSTR "</section>" | ||
102 | } | ||
103 | } else { | ||
104 | OUTSTR = "<pre><code>" OUTSTR "</pre></code>" | ||
105 | } | ||
106 | print(template_replace(TEMPLATE)) > OUTFILE | ||
73 | close(OUTFILE) | 107 | close(OUTFILE) |
74 | } | 108 | } |
75 | 109 | ||
76 | function doindex () { | 110 | function doindex () { |
77 | INDEX = OUTD "index.html" | 111 | OUTFILE = OUTD "index.html" |
78 | printf("building index: %s\n", INDEX) | 112 | OUTSTR = "" |
79 | print(template_replace(IHEAD)) > INDEX | 113 | printf("building index: %s\n", OUTFILE) |
80 | print("<ul>") >> INDEX | ||
81 | for (f in FILES) { | 114 | for (f in FILES) { |
82 | printf("<li><a href=\"%s\">%s</a></li>\n", | 115 | OUTSTR = OUTSTR \ |
83 | outfn(FILES[f], 1), FILES[f]) >> INDEX | 116 | sprintf("<li><a href=\"%s\">%s</a></li>\n", |
117 | outfn(FILES[f], 1), FILES[f]) | ||
84 | } | 118 | } |
85 | print("</ul>") >> INDEX | 119 | finish() |
86 | if (readme) { | 120 | } |
87 | while (((RMFL " " readme) | getline) > 0) | 121 | |
88 | print >> INDEX | 122 | function die(code, message) { |
123 | print(message) > "/dev/stderr" | ||
124 | dead = code | ||
125 | exit code | ||
126 | } | ||
127 | |||
128 | function copy_statics () { | ||
129 | split(static, STATICS, ":") | ||
130 | for (s in STATICS) { | ||
131 | printf("Copying %s\n", STATICS[s]) | ||
132 | if (system("cp " STATICS[s] " " outfn(STATICS[s], 4))) | ||
133 | die(2, "Can't copy static file: " STATICS[s]) | ||
89 | } | 134 | } |
90 | print(template_replace(IFOOT)) >> INDEX | ||
91 | close(INDEX) | ||
92 | } | 135 | } |