Fair enough, I've fixed it and it validates as HTML now. I also added generation time and log size optimizations.
There is a slight optimization bug in the sense that it makes stuff like this:
Code: Select all
<span style="color:rgb(73,149,0);">m</span>
<span style="color:rgb(73,149,0);background:rgb(0,0,0);">apper</span>
Which should be optimized out. Further optimizations would be generating classes as necessary for new color combos and reusing them (currently, timestamp colors are optimized very well using this).
Still though, a usability improvement that allows the user to select where to dump from, not just the whole buffer, would be a very welcome addition.
function logScreen(logdir, logfile)
if logfile == nil then
cecho("\n<red>Error:<white> You must sepecify a logname!")
return false
end
if string.char(getMudletHomeDir():byte()) == "/" then _sep = "/" else _sep = "\\" end
local logdir = logdir .. _sep .. logfile .. ".html"
local file_output = io.open(logdir, "w")
local fo = {}
local line_count = getLineCount()
local cur_buffer = getLines(1, line_count-1)
local curcolor = ""
local index = 0
local r,g,b = 0,0,0
local br,bg,bb = 0,0,0
local cbr,cbg,cbb --last bg colors
local cr,cg,cb -- last colors
local tc = 1 -- table count
local started_similar
local moveCursor = moveCursor
local selectString = selectString
local sformat = string.format
local getFgColor = getFgColor
local getBgColor = getBgColor
local perf = createStopWatch()
startStopWatch(perf)
local conversions = {
["¦"] = "¦",
["×"] = "×",
["«"] = "«",
["»"] = "»",
}
fo[tc] = [[<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<link href='http://fonts.googleapis.com/css?family=Droid+Sans+Mono' rel='stylesheet' type='text/css'>
<title>]]..logfile..[[</title>
<style type="text/css">
body {
color: silver;
background-color: black;
font-family: 'Droid Sans Mono';
white-space: pre;
font-size: 12px;
}
span.t {
color: silver;
background-color: black;
}
</style>
</head>
<body>]]
tc = tc + 1
for line_num,cur_line in ipairs(cur_buffer) do
index = 0
-- add in the timestamp!
fo[tc] = sformat([[<span class="t">%s</span>]], getTimestamp(line_num))
tc = tc +1
-- process the line
while index < #cur_line do
index = index + 1
moveCursor("main", index, line_num)
selectString(cur_line:sub(index), 1)
r,g,b = getFgColor()
br,bg,bb = getBgColor()
-- if the color is any different for this character, add in spans
if cr ~= r or cg ~= g or cb ~= b or cbr ~= br or cbg ~= bg or cbb ~= bb then
cr,cg,cb = r,g,b
cbr,cbg,cbb = br,bg,bb
if started_similar then
fo[tc] = "</span>"
tc = tc + 1
started_similar = false
end
-- log file size spacer saver: only change fg or bg
if cbr == br and cbg == bg and cbb == bb then
fo[tc] = sformat("<span style=\'color:rgb(%d,%d,%d)'>%s</span>", r,g,b, cur_line:sub(index, index):gsub("&", "&"):gsub("<", "<"):gsub(">", ">"))
else
fo[tc] = sformat("<span style=\'color:rgb(%d,%d,%d);background:rgb(%d,%d,%d)'>%s</span>", r,g,b,br,bg,bb, cur_line:sub(index, index):gsub("&", "&"):gsub("<", "<"):gsub(">", ">"))
end
tc = tc +1
else -- if it's the same, then just stuff them into the line
if not started_similar then fo[tc] = sformat("<span style=\'color:rgb(%d,%d,%d);background:rgb(%d,%d,%d)'>", r,g,b,br,bg,bb); tc = tc + 1 end
fo[tc] = cur_line:sub(index, index):gsub("&", "&"):gsub("<", "<"):gsub(">", ">")
tc = tc +1
started_similar = true
end
cur_line:sub(index, index)
end
fo[tc] = "\n"
tc = tc +1
end
fo[#fo+1] = "</span></body></html>"
local s = table.concat(fo)
for from, to in pairs(conversions) do
s = string.gsub(s, from, to)
end
file_output:write(s)
file_output:close()
-- this can use quite a bit of memory on a large buffer, so free it up right away
collectgarbage("collect")
cecho("\n<cyan>Current buffer saved to: <white>" .. logdir .. "<cyan>, took <white>" .. stopStopWatch(perf) .. "ms<cyan>.")
end