lua2html.lua

Fri, 13 Nov 2009 16:39:27 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 13 Nov 2009 16:39:27 +0000
changeset 3
1e093cebd5e9
parent 2
520a1e0d187e
child 5
71da0b9c138c
permissions
-rwxr-xr-x

Add squishy file

2
520a1e0d187e Make script executable
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
1 #!/usr/bin/env lua
520a1e0d187e Make script executable
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
2
1
b3889bfb6052 Add README and COPYING files
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
3 -- Copyright (C) 2008-2009 Matthew Wild
b3889bfb6052 Add README and COPYING files
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
4 --
b3889bfb6052 Add README and COPYING files
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b3889bfb6052 Add README and COPYING files
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
6 -- COPYING file in the source package for more information.
0
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local keywords = { "function", "for", "if", "elseif", "then", "else", "do", "repeat", "until", "end", "return", "true", "false", "and", "not", "or", "local", "nil", "break" }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 for _, keyword in ipairs(keywords) do keywords[keyword] = true; end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local ops = {};
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local bytelisting, err = io.popen("luac -l -p "..arg[1]);
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 if not bytelisting then print(err); return 1; end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 for line in bytelisting:lines() do
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local opnum, linenum, opname, comments = line:match("^\t(%d+)%s*%[(%d+)%]%s*(%u+)[^;]*;? ?(.*)$");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 if opnum then
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 linenum = tonumber(linenum);
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 -- print(string.format("opnum: %d line: %d op: %s ; %s", opnum, linenum, opname, comments));
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if not ops[linenum] then
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 ops[linenum] = { };
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 if ops[linenum-1] then
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 table.sort(ops[linenum-1], function (a,b) return a.comment and b.comment and #a.comment > #b.comment; end);
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 table.insert(ops[linenum], { name = opname, comment = comments });
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local test_coverage = {};
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local test_report, test_report_err = io.open("tests/reports/coverage_"..arg[1]:gsub("^%W", ""):gsub("%W+", "_")..".report");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 if test_report then
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 for line in test_report:lines() do
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local f, linenum, test, success = line:match("^(.-)|(.-)|(.-)|(.-)$");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 if linenum and success then
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 test_coverage[tonumber(linenum)] = success;
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 else
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 io.stderr:write("Warning: Can't parse this: [[", line, "]] in test report\n");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 else
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 test_coverage = nil;
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local linenum = 1;
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local html_spaces = { ["\t"] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", [" "] = "&nbsp;" };
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 function escape_html(s)
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 return (s and s:gsub("[^%w]", { ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;", ["\""] = "&quot;", ["'"] = "&apos;" })) or "";
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 function escape_pattern(s)
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 return s:gsub("(%p)", "%%%1");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local sub;
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 print("<html><head><title>"..escape_html(arg[1]).."</title><style type='text/css'>");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 print [[
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 .getglobal { color:#cc0000 !important; text-decoration:underline; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 .setglobal { color:#ff0000; font-weight:bold; text-decoration:underline; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 .getupval { color:#00cc00; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 .nop { color:#cccccc; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 .keyword { color:#0000cc; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 .comment, .comment .keyword { color:#007733; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 .string, .string * { color:#007755; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 td .test-false, .test-false { background-color:#ffa6be !important; display:block; width:100%; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 td .test-true, .test-true { background-color:#beffa6; display:block; width:100%; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 td .test-miss, .test-miss { background-color:#a6beff; display:block; width:100%; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 table { border-collapse: collapse; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 span, tr, *, table { padding:2px; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 td { padding:0px; padding-right:2em; }
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 ]]
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 print("</style></head><body>\n<br/>\n<table>");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 for line in io.lines(arg[1]) do
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 line = " "..line:gsub("%s", html_spaces).." ";
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 line = line:gsub("FIXME:?", "<b>%1</b>");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 line = line:gsub("TODO:?", "<b>%1</b>");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 line = line:gsub("%-%-.*$", "<span class='comment'>%1</span>");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 line = line:gsub("\".-[^\\]\"", "<span class='string'>%1</span>");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 if ops[linenum] then
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 for _, op in pairs(ops[linenum]) do
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 if op.comment and #op.comment > 0 and op.name ~= "JMP" then
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 --print("Replacing "..escape_pattern(op.comment).."("..op.comment..") for "..op.name);
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 line, sub = line:gsub("([^%w_])("..escape_pattern(op.comment)..")([^%w_])", "%1<span class='"..op.name:lower().."'>"..escape_html(op.comment).."</span>%3");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 if sub == 0 and op.name == "GETTABLE" then
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 --print("Trying again...");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 local id = op.comment:match("[%w_]+");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 if id then
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 --print("Got "..id);
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 line = line:gsub(escape_pattern("."..id), ".<span class='"..op.name:lower().."'>"..escape_html(id).."</span>");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 else
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 --print("??", tostring(op.comment), tostring(#op.comment));
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 line = line:gsub("[%w_]+", function (id) if keywords[id] then return "<span class='keyword'>"..id.."</span>"; end end);
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 local classes = { "line" };
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 if test_coverage and test_coverage[linenum] ~= nil then
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 table.insert(classes, "test-"..test_coverage[linenum]);
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 print("<tr><td><span class='"..table.concat(classes, " ").."'>"..linenum.."</span></td><td>", line, "</td></tr>");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 else
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 line = line:gsub("[%w_]+", function (id) if keywords[id] then return "<span class='keyword'>"..id.."</span>"; end end);
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 print("<tr><td><span class='line nop'>"..linenum.."</span></td><td>", line, "</td></tr>");
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 linenum = linenum + 1;
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 end
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116
140ff9cafe2f Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 print("</table>\n</body>\n</html>");

mercurial