diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | README.md | 4 | ||||
-rwxr-xr-x | schwa.awk | 92 |
3 files changed, 97 insertions, 0 deletions
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 @@ | |||
1 | # schwa | ||
2 | ## Source Code Htmlizer Written in Awk | ||
3 | |||
4 | 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 @@ | |||
1 | #!/bin/sh | ||
2 | { trampoline=";" "exec" "awk" "-f" "$0" "$@"; } | ||
3 | |||
4 | BEGIN { | ||
5 | if (head) HEAD = slurp(head) | ||
6 | else HEAD = "<!DOCTYPE html><title>{{FILENAME}}</title>" \ | ||
7 | "<h1>{{FILENAME}}</h1><pre><code>" | ||
8 | if (foot) FOOT = slurp(foot) | ||
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 | ||
16 | else OUTD = "out/" | ||
17 | if (readmefilter) RMFL = readmefilter | ||
18 | else RMFL = "cat" | ||
19 | if (system("mkdir -p " OUTD)) exit 1 | ||
20 | "pwd" | getline TMPLV["DIRECTORY"]; close("pwd") | ||
21 | sub(/.*\//, "", TMPLV["DIRECTORY"]) | ||
22 | } | ||
23 | |||
24 | FNR == 1 { | ||
25 | if (NR > 1) finish() | ||
26 | OUTFILE = outfn(FILENAME) | ||
27 | TMPLV["FILENAME"] = FILENAME | ||
28 | TMPLV["OUTFILE"] = OUTFILE | ||
29 | FILES[f++] = FILENAME | ||
30 | printf("%s -> %s\n", FILENAME, OUTFILE) | ||
31 | OUTSTR = template_replace(HEAD) | ||
32 | } | ||
33 | |||
34 | END { | ||
35 | finish() | ||
36 | doindex() | ||
37 | } | ||
38 | |||
39 | { | ||
40 | # Sanitize HTML | ||
41 | gsub(/&/, "\\&"); gsub(/</, "\\<"); gsub(/>/, "\\>") | ||
42 | # Wrap the line in a span with a line number link | ||
43 | OUTSTR = OUTSTR sprintf("<span class=\"ln\" id=\"l%d\">" \ | ||
44 | "<a class=\"ll\" href=\"#l%d\">%d</a>" \ | ||
45 | "%s</span>\n", | ||
46 | FNR, FNR, FNR, $0) | ||
47 | } | ||
48 | |||
49 | function slurp (file, o) { | ||
50 | if (!file) return 0 | ||
51 | while ((getline < file) > 0) | ||
52 | o = o (o?"\n":"") $0 | ||
53 | return o | ||
54 | } | ||
55 | |||
56 | function outfn (file, norepldir) { | ||
57 | if (!norepldir) sub(/^/, OUTD, file) | ||
58 | sub(/$/, ".html", file) | ||
59 | return file | ||
60 | } | ||
61 | |||
62 | function template_replace (str) { | ||
63 | for (ts in TMPLV) { | ||
64 | gsub("{{"ts"}}", TMPLV[ts], str) | ||
65 | } | ||
66 | return str | ||
67 | } | ||
68 | |||
69 | function finish () { | ||
70 | sub("\n$", "", OUTSTR) | ||
71 | OUTSTR = OUTSTR template_replace(FOOT) | ||
72 | print(OUTSTR) > OUTFILE | ||
73 | close(OUTFILE) | ||
74 | } | ||
75 | |||
76 | function doindex () { | ||
77 | INDEX = OUTD "index.html" | ||
78 | printf("building index: %s\n", INDEX) | ||
79 | print(template_replace(IHEAD)) > INDEX | ||
80 | print("<ul>") >> INDEX | ||
81 | for (f in FILES) { | ||
82 | printf("<li><a href=\"%s\">%s</a></li>\n", | ||
83 | outfn(FILES[f], 1), FILES[f]) >> INDEX | ||
84 | } | ||
85 | print("</ul>") >> INDEX | ||
86 | if (readme) { | ||
87 | while (((RMFL " " readme) | getline) > 0) | ||
88 | print >> INDEX | ||
89 | } | ||
90 | print(template_replace(IFOOT)) >> INDEX | ||
91 | close(INDEX) | ||
92 | } | ||