local string = require "string"
local table = require "table"
local io = require "io"
local base = _G
module "html"
local HTML_EXT = ".html"
local ENTITIES = {
["&"] = "&", ["<"] = "<", [">"] = ">",
["'"] = "'", ["\""] = """,
}
local HEADER = [[
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>%s</title>
<meta name="Generator" content="LuaSrcDiet">
<style type="text/css">
%s</style>
</head>
<body>
<pre class="code">
]]
local FOOTER = [[
</pre>
</body>
</html>
]]
local STYLESHEET = [[
BODY {
background: white;
color: navy;
}
pre.code { color: black; }
span.comment { color: #00a000; }
span.string { color: #009090; }
span.keyword { color: black; font-weight: bold; }
span.number { }
span.operator { }
span.name { }
span.global { color: #ff0000; font-weight: bold; }
span.local { color: #0000ff; font-weight: bold; }
]]
local option
local srcfl, destfl
local toklist, seminfolist, toklnlist
local function print(...)
if option.QUIET then return end
base.print(...)
end
function init(_option, _srcfl, _destfl)
option = _option
srcfl = _srcfl
local extb, exte = string.find(srcfl, "%.[^%.%\\%/]*$")
local basename, extension = srcfl, ""
if extb and extb > 1 then
basename = string.sub(srcfl, 1, extb - 1)
extension = string.sub(srcfl, extb, exte)
end
destfl = basename..HTML_EXT
if option.OUTPUT_FILE then
destfl = option.OUTPUT_FILE
end
if srcfl == destfl then
base.error("output filename identical to input filename")
end
end
function post_load(z)
print([[
HTML plugin module for LuaSrcDiet
]])
print("Exporting: "..srcfl.." -> "..destfl.."\n")
end
function post_lex(_toklist, _seminfolist, _toklnlist)
toklist, seminfolist, toklnlist
= _toklist, _seminfolist, _toklnlist
end
local function do_entities(z)
local i = 1
while i <= #z do
local c = string.sub(z, i, i)
local d = ENTITIES[c]
if d then
c = d
z = string.sub(z, 1, i - 1)..c..string.sub(z, i + 1)
end
i = i + #c
end
return z
end
local function save_file(fname, dat)
local OUTF = io.open(fname, "wb")
if not OUTF then base.error("cannot open \""..fname.."\" for writing") end
local status = OUTF:write(dat)
if not status then base.error("cannot write to \""..fname.."\"") end
OUTF:close()
end
function post_parse(globalinfo, localinfo)
local html = {}
local function add(s)
html[#html + 1] = s
end
local function span(class, s)
add('<span class="'..class..'">'..s..'</span>')
end
for i = 1, #globalinfo do
local obj = globalinfo[i]
local xref = obj.xref
for j = 1, #xref do
local p = xref[j]
toklist[p] = "TK_GLOBAL"
end
end
for i = 1, #localinfo do
local obj = localinfo[i]
local xref = obj.xref
for j = 1, #xref do
local p = xref[j]
toklist[p] = "TK_LOCAL"
end
end
add(string.format(HEADER,
do_entities(srcfl),
STYLESHEET))
for i = 1, #toklist do
local tok, info = toklist[i], seminfolist[i]
if tok == "TK_KEYWORD" then
span("keyword", info)
elseif tok == "TK_STRING" or tok == "TK_LSTRING" then
span("string", do_entities(info))
elseif tok == "TK_COMMENT" or tok == "TK_LCOMMENT" then
span("comment", do_entities(info))
elseif tok == "TK_GLOBAL" then
span("global", info)
elseif tok == "TK_LOCAL" then
span("local", info)
elseif tok == "TK_NAME" then
span("name", info)
elseif tok == "TK_NUMBER" then
span("number", info)
elseif tok == "TK_OP" then
span("operator", do_entities(info))
elseif tok ~= "TK_EOS" then
add(info)
end
end
add(FOOTER)
save_file(destfl, table.concat(html))
option.EXIT = true
end
return base.getfenv()