1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
#!/bin/sh
{ trampoline=";" "exec" "awk" "-f" "$0" "$@"; } # -*- awk -*-
BEGIN {
if (template) TEMPLATE = slurp(template)
else TEMPLATE = \
"<!DOCTYPE html><title>{{FILENAME}}</title>" \
"<h1>{{FILENAME}}</h1>{{CONTENT}}"
if (out) OUTD = out
else OUTD = "out/"
if (readmefilter) RMFL = readmefilter
else RMFL = "cat"
if (clone) TMPLV["CLONE"] = "<span class=\"clone\">" clone "</span>"
else TMPLV["CLONE"] = ""
if (desc) TMPLV["DESCRIPTION"] = "<p class=\"desc\">" desc "</p>"
else TMPLV["DESCRIPTION"] = ""
if (root) TMPLV["ROOT"] = root
else TMPLV["ROOT"] = "/"
"pwd" | getline TMPLV["DIRECTORY"]; close("pwd")
TMPLV["DIRECTORY"] = outfn(TMPLV["DIRECTORY"], 3)
}
FNR == 1 {
if (NR > 1) finish()
OUTFILE = outfn(FILENAME)
TMPLV["FILENAME"] = FILENAME
TMPLV["OUTFILE"] = OUTFILE
FILES[f++] = FILENAME
printf("%s -> %s\n", FILENAME, OUTFILE)
if (system("mkdir -p " OUTD outfn(FILENAME, 2)))
die(1, "Can't make directory: " outfn(FILENAME, 2))
if (!system("cp " FILENAME " " outfn(FILENAME, 4) ".txt"))
TMPLV["RAWFILE"] = outfn(FILENAME, 3) ".txt"
OUTSTR = ""
}
END {
if (dead) exit dead
finish()
doindex()
copy_statics()
}
{
# Sanitize HTML
gsub(/&/, "\\&"); gsub(/</, "\\<"); gsub(/>/, "\\>")
# Wrap the line in a span with a line number link
OUTSTR = OUTSTR sprintf("<span class=\"ln\" id=\"l%d\">" \
"<a class=\"ll\" href=\"#l%d\">%d</a>" \
"%s</span>\n",
FNR, FNR, FNR, $0)
}
function slurp (file, o) {
if (!file) return 0
while ((getline < file) > 0)
o = o (o?"\n":"") $0
return o
}
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(/<!--\/NORMAL/, "", str)
gsub(/NORMAL-->/, "", str)
} else {
gsub(/<!--\/INDEX/, "", str)
gsub(/INDEX-->/, "", str)
}
for (ts in TMPLV) {
gsub("{{"ts"}}", TMPLV[ts], str)
}
gsub(/&/, "\\\\&", OUTSTR)
sub("{{CONTENT}}", OUTSTR, str)
return str
}
function finish () {
sub("\n$", "", OUTSTR)
if (OUTFILE == OUTD "index.html") {
TMPLV["FILENAME"] = TMPLV["DIRECTORY"]
OUTSTR = "<ul>" OUTSTR "</ul>"
if (readme) {
OUTSTR = OUTSTR "<section id=\"readme\">"
while (((RMFL " " readme) | getline) > 0)
OUTSTR = OUTSTR "\n" $0
OUTSTR = OUTSTR "</section>"
}
} else {
OUTSTR = "<pre><code>" OUTSTR "</pre></code>"
}
print(template_replace(TEMPLATE)) > OUTFILE
close(OUTFILE)
}
function doindex () {
OUTFILE = OUTD "index.html"
OUTSTR = ""
printf("building index: %s\n", OUTFILE)
for (f in FILES) {
OUTSTR = OUTSTR \
sprintf("<li><a href=\"%s\">%s</a></li>\n",
outfn(FILES[f], 1), FILES[f])
}
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] " " OUTD outfn(STATICS[s], 3)))
die(2, "Can't copy static file: " STATICS[s])
}
}
|