about summary refs log tree commit diff stats
path: root/schwa.awk
blob: a3f4f3c0472cd1fd8e5131ec772d7a05aa763648 (plain)
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
#!/bin/sh
{ trampoline=";" "exec" "awk" "-f" "$0" "$@"; }

BEGIN {
	if (head) HEAD = slurp(head)
	else HEAD = "<!DOCTYPE html><title>{{FILENAME}}</title>" \
		     "<h1>{{FILENAME}}</h1><pre><code>"
	if (foot) FOOT = slurp(foot)
	else FOOT = "</code></pre>"
	if (ihead) IHEAD = slurp(ihead)
	else IHEAD = "<!DOCTYPE html><title>{{DIRECTORY}}</title>" \
		     "<h1>{{DIRECTORY}}</h1>"
	if (ifoot) IFOOT = slurp(ifoot)
	else IFOOT = ""
	if (out) OUTD = out
	else OUTD = "out/"
	if (readmefilter) RMFL = readmefilter
	else RMFL = "cat"
	if (system("mkdir -p " OUTD)) exit 1
	"pwd" | getline TMPLV["DIRECTORY"]; close("pwd")
	sub(/.*\//, "", TMPLV["DIRECTORY"])
}

FNR == 1 {
	if (NR > 1) finish()
	OUTFILE = outfn(FILENAME)
	TMPLV["FILENAME"] = FILENAME
	TMPLV["OUTFILE"] = OUTFILE
	FILES[f++] = FILENAME
	printf("%s -> %s\n", FILENAME, OUTFILE)
	OUTSTR = template_replace(HEAD)
}

END {
	finish()
	doindex()
}

{
	# Sanitize HTML
	gsub(/&/, "\\&amp;"); gsub(/</, "\\&lt;"); gsub(/>/, "\\&gt;")
	# 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, norepldir) {
	if (!norepldir) sub(/^/, OUTD, file)
	sub(/$/, ".html", file)
	return file
}

function template_replace (str) {
	for (ts in TMPLV) {
		gsub("{{"ts"}}", TMPLV[ts], str)
	}
	return str
}

function finish () {
	sub("\n$", "", OUTSTR)
	OUTSTR = OUTSTR template_replace(FOOT)
	print(OUTSTR) > OUTFILE
	close(OUTFILE)
}

function doindex () {
	INDEX = OUTD "index.html"
	printf("building index: %s\n", INDEX)
	print(template_replace(IHEAD)) > INDEX
	print("<ul>") >> INDEX
	for (f in FILES) {
		printf("<li><a href=\"%s\">%s</a></li>\n",
		       outfn(FILES[f], 1), FILES[f]) >> INDEX
	}
	print("</ul>") >> INDEX
	if (readme) {
		while (((RMFL " " readme) | getline) > 0)
			print >> INDEX
	}
	print(template_replace(IFOOT)) >> INDEX
	close(INDEX)
}