]> git.scottworley.com Git - tablify/blob - log2table.awk
The prototype
[tablify] / log2table.awk
1
2 # TODO: Implement quoting properly
3 function quote_html(s) { return s; }
4 function quote_html_attribute(s) { return gensub("\"", """, "g", s) }
5
6 BEGIN {
7 FS = ":"
8 num_rows = 0
9 num_cols = 0
10 read_header = 1
11 # h/t https://wabain.github.io/2019/10/13/css-rotated-table-header.html
12 print "<html><head><style>"
13 print "th, td { white-space: nowrap; }"
14 print "th { text-align: left; font-weight: normal; }"
15 print "table { border-collapse: collapse }"
16 print "tr.key > th { height: 8em; vertical-align: bottom; line-height: 1 }"
17 print "tr.key > th > div { width: 1em; }"
18 print "tr.key > th > div > div { width: 5em; transform-origin: bottom left; transform: translateX(1em) rotate(-65deg) }"
19 print "td { border: thin solid gray; }"
20 print "td.numeric { text-align: right; }"
21 print "td.yes { border: thin solid gray; background-color: gray; }"
22 print "td.spacer { border: none; }"
23 # h/t https://stackoverflow.com/questions/5687035/css-bolding-some-text-without-changing-its-containers-size/46452396#46452396
24 print ".highlight { text-shadow: -0.06ex 0 black, 0.06ex 0 black; }"
25 print "img { height: 1.2em; }"
26 print "</style><script>"
27 print "function highlight(id) { const e = document.getElementById(id); if (e) { e.classList.add(\"highlight\"); } }"
28 print "function clear_highlight(id) { const e = document.getElementById(id); if (e) { e.classList.remove(\"highlight\"); } }"
29 print "function h2(a, b) { highlight(a); highlight(b); }"
30 print "function ch2(a, b) { clear_highlight(a); clear_highlight(b); }"
31 print "</script></head><body><table><tbody>"
32 }
33
34 END {
35 printf "<tr class=\"key\"><th></th>"
36 for (i=0; i < num_cols; i++) {
37 printf "<th id=\"%s\"><div><div>%s</div></div></th>", quote_html_attribute(col_headers[i]), quote_html(col_headers[i])
38 }
39 print "</tr>";
40 for (i=1*skip_rows; i <= num_rows; i++) {
41 spacer_row = substr(row_headers[i], 1, 1) == "!"
42 row_header = spacer_row ? substr(row_headers[i], 2) : row_headers[i]
43 printf "<tr><th id=\"%s\">%s</th>", quote_html_attribute(row_header), quote_html(row_header)
44 for (j = 0;j < num_cols; j++) {
45 printf "<td "
46 printf "onmouseover=\"h2('%s','%s')\" ", quote_html_attribute(row_header), quote_html_attribute(col_headers[j])
47 printf "onmouseout=\"ch2('%s','%s')\" ", quote_html_attribute(row_header), quote_html_attribute(col_headers[j])
48 printf "class=\""
49 if (rows[i][j] == "&nbsp;") { printf " yes " }
50 if (rows[i][j] ~ /^[0-9]+$/) { printf " numeric " }
51 if (spacer_row || substr(col_headers[j], 0, 4) == "<!--") { printf " spacer " }
52 printf "\">" rows[i][j] "</td>"
53 }
54 print "</tr>"
55 }
56 print "</tbody></table></body></html>"
57 }
58
59 {
60 if (/^$/) {
61 read_header = 1
62 num_rows++
63 } else {
64 if (read_header) {
65 read_header = 0
66 row_headers[num_rows] = $0
67 } else {
68 if (! ($1 in col_index)) {
69 col_headers[num_cols] = $1
70 col_index[$1] = num_cols++
71 }
72 rows[num_rows][col_index[$1]] = $2 ? $2 : "&nbsp;"
73 }
74 }
75 }