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 | |
parent | Delete header (diff) | |
download | schwa-c2a3fda0cf2aef901d1280bea792bf109c6d3d73.tar.gz schwa-c2a3fda0cf2aef901d1280bea792bf109c6d3d73.zip |
Add templating, static files; add sample template+style
-rwxr-xr-x | schwa.awk | 105 | ||||
-rw-r--r-- | style.css | 79 | ||||
-rw-r--r-- | template.html | 31 |
3 files changed, 184 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 | } |
diff --git a/style.css b/style.css new file mode 100644 index 0000000..b73b2dc --- /dev/null +++ b/style.css | |||
@@ -0,0 +1,79 @@ | |||
1 | /* schwa example style */ | ||
2 | |||
3 | * { box-sizing: border-box; } | ||
4 | |||
5 | body { | ||
6 | width: fit-content; | ||
7 | margin: auto; | ||
8 | padding: 1em; | ||
9 | background: gray; | ||
10 | } | ||
11 | |||
12 | main { | ||
13 | min-width: 40em; | ||
14 | background: white; | ||
15 | padding: 1em; | ||
16 | margin: 0; | ||
17 | border: 2px outset black; | ||
18 | border-top: none; | ||
19 | } | ||
20 | |||
21 | .ll { | ||
22 | display: inline-block; | ||
23 | width: 3em; | ||
24 | text-align: right; | ||
25 | padding-right: 0.5em; | ||
26 | margin-right: 0.5em; | ||
27 | } | ||
28 | |||
29 | header { | ||
30 | width: 100%; | ||
31 | color: white; | ||
32 | background: teal; | ||
33 | padding: 1em; | ||
34 | margin: 0; | ||
35 | border: 2px outset black; | ||
36 | border-bottom: none; | ||
37 | display: flex; | ||
38 | align-items: baseline; | ||
39 | justify-content: space-between; | ||
40 | } | ||
41 | |||
42 | header h1 { | ||
43 | margin: 4px; | ||
44 | } | ||
45 | |||
46 | footer, footer p { | ||
47 | margin: 0; padding: 4px; | ||
48 | text-align: right; | ||
49 | color: white; | ||
50 | } | ||
51 | |||
52 | header a:link { | ||
53 | text-decoration: none; | ||
54 | background: lightgray; | ||
55 | color: black; | ||
56 | padding: 4px; | ||
57 | border: 2px outset gray; | ||
58 | } | ||
59 | header a:visited { | ||
60 | color: black; | ||
61 | } | ||
62 | header a:hover { | ||
63 | border: 2px inset gray; | ||
64 | } | ||
65 | header a:active {} | ||
66 | |||
67 | nav ul { | ||
68 | padding: 0; margin: 0; | ||
69 | } | ||
70 | |||
71 | nav li { | ||
72 | display: inline-block; | ||
73 | } | ||
74 | |||
75 | .clone, .desc { | ||
76 | font-style: italic; | ||
77 | text-align: right; | ||
78 | margin: 0; | ||
79 | } | ||
diff --git a/template.html b/template.html new file mode 100644 index 0000000..c67ae0d --- /dev/null +++ b/template.html | |||
@@ -0,0 +1,31 @@ | |||
1 | <!DOCTYPE html> | ||
2 | <html> | ||
3 | <head> | ||
4 | <meta charset="utf-8"> | ||
5 | <title>{{FILENAME}}</title> | ||
6 | <link rel="stylesheet" type="text/css" href="style.css"> | ||
7 | </head> | ||
8 | <body> | ||
9 | <header> | ||
10 | <h1><!--NORMAL-->{{DIRECTORY}}/<!--/NORMAL-->{{FILENAME}}</h1> | ||
11 | <!--INDEX--> | ||
12 | {{DESCRIPTION}} | ||
13 | <!--/INDEX--> | ||
14 | <!--NORMAL--> | ||
15 | <nav> | ||
16 | <ul> | ||
17 | <li><a href="/">index</a></li> | ||
18 | <li><a href="{{RAWFILE}}">source</a></li> | ||
19 | </ul> | ||
20 | </nav> | ||
21 | <!--/NORMAL--> | ||
22 | </header> | ||
23 | <main> | ||
24 | {{CONTENT}} | ||
25 | </main> | ||
26 | <footer> | ||
27 | <p>(C) Case Duckworth. {{CLONE}} | ||
28 | </p> | ||
29 | </footer> | ||
30 | </body> | ||
31 | </html> | ||