json2htmltable.sh - randomcrap - random crap programs of varying quality
(HTM) git clone git://git.codemadness.org/randomcrap
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
json2htmltable.sh (1106B)
---
1 #!/bin/sh
2 # stupid script to visualize JSON to a HTML table, depends on json2tsv:
3 # git://git.codemadness.org/json2tsv
4
5 cat <<!__EOF__
6 <html>
7 <head>
8 <style type="text/css">
9 table { border-collapse: collapse; }
10 th { background-color: #eee; }
11 th, td { border: 1px solid #ddd; padding: 2px; }
12 td.n, td.t { background-color: #eee; }
13 </style>
14 </head>
15 <body>
16 <table>
17 <thead>
18 <tr>
19 <th class="n" align="left">Node</th>
20 <th class="t" align="center">Type</th>
21 <th class="v" align="left">Value</th>
22 </tr>
23 </thead>
24 <tbody>
25 !__EOF__
26
27 json2tsv -n -F '\x1f' -R '\x1e' | \
28 LC_ALL=C awk '
29 BEGIN {
30 FS = "\x1f"; RS = "\x1e";
31 }
32 function htmlencode(s) {
33 # HTML.
34 gsub("&", "\\&", s);
35 gsub("<", "\\<", s);
36 gsub(">", "\\>", s);
37 gsub("\"", "\\"", s);
38 gsub("'"'"'", "\\'", s);
39 return s;
40 }
41 {
42 print "<tr>";
43 print "\t<td class=\"n\" valign=\"top\">" htmlencode($1) "</td>";
44 print "\t<td class=\"t\" align=\"center\" valign=\"top\">" htmlencode($2) "</td>";
45 print "\t<td valign=\"top\" class=\"v\">" htmlencode($3) "</td>";
46 print "</tr>";
47 }'
48
49 cat <<!__EOF__
50 </tbody>
51 </table>
52 </body>
53 </html>
54 !__EOF__