Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[lua2html by WeBuLtRa v0.9]]--
- function lua2html(sFile, sFileOut)
- local keywords = { "while", "function", "for", "require", "elseif", "if", "then", "else", "do", "repeat", "until", "end", "return", "true", "false", "and", "not", "or", "local", "nil", "break", "in"};
- local functions = {":write", ":read", ":close", ":seek", ":gsub", ":find", ":gmatch", ":format", ":lower", ":upper", ":sub", ":byte", ":reverse", ":rep", ":len", ":dump", ":char"};
- local operators = {"%+", "%#", "%-", "%*", "%=", "~", "%."};
- local t = {};
- local sF = io.open(sFile, "rb");
- if sF then
- local function print_r(a,b)
- if b ~= _G then
- if type(b) == "function" then
- table.insert(t,a);
- elseif type(b) == "table" then
- for x, y in pairs(b) do
- if type(y) == "function" then
- table.insert(t,a.."%."..x);
- end
- end
- end
- end
- end
- table.foreach(_G, print_r);
- local function ParseScript(s)
- local tFs, tVars = {}, {};
- for key in string.gmatch(s, "function ([%w%._%:]+)") do
- table.insert(tFs, #tFs+1, key)
- end
- for key in string.gmatch(s, "(%w+%.%w+) = function") do
- table.insert(tFs, #tFs+1, key)
- end
- return tFs;
- end
- local function CheckKeywords(line, multi)
- if not multi then
- for x,y in pairs(keywords) do
- line = line:gsub("([%W]+)("..y..")([%c%s%;%,%(%)]+)",
- function(a,b,c)
- return a.."<span style='color:blue;'>"..b.."</span>"..c
- end
- );
- line = line:gsub("^"..y.."%s",
- function(b)
- return "<span style='color:blue;'>"..b.."</span>";
- end
- );
- end
- end
- return line;
- end
- local function CheckOperators(line, operators, multi)
- if not multi then
- for x,y in pairs(operators) do
- line = line:gsub(y, function(a) return "<span style='color:red;'>"..a.."</span>" end);
- end
- end
- return line;
- end
- local function CheckFunctions(line, t, multi, color)
- if not multi then
- for x,y in pairs(t) do
- line = line:gsub("("..y..")%(", function(a) return "<span style='color:"..color..";'>"..a.."</span>(" end);
- end
- end
- return line;
- end
- local function CheckALL(line, multi,t, tFs, functions)
- line = CheckKeywords(line, multi);
- line = CheckFunctions(line, t, multi, "darkorange");
- line = CheckFunctions(line, functions, multi, "darkorange");
- line = CheckFunctions(line, tFs, multi, "red");
- line = CheckOperators(line, operators, multi);
- line = line:gsub("('[%w%d%s]-')", function(a) return "<span style='color:darkmagenta;'>"..a.."</span>" end);
- return line;
- end
- local function CheckStrings(line,t,tFs,functions)
- line = line:gsub("(.-)([\"].-[\"])", function(b,a) return CheckALL(b, multi,t, tFs, functions).."<span style='color:darkmagenta;'>"..a.."</span>" end);
- line = line:gsub('(".+")(.+)', function(a,b) return a..CheckALL(b, multi,t, tFs, functions) end);
- line = line:gsub("('[.]+')", function(a) return "<span style='color:darkmagenta;'>"..a.."</span>" end);
- return line;
- end
- local sText = "";
- local sRaw = sF:read("*a");
- local tFs = ParseScript(sRaw);
- sF:seek("set");
- local comment, multi, d = false, false, 0;
- for line in sF:lines() do
- d = d + 1;
- local g = "<span style='background-color:yellow;' name='linea' >"..d.."</span><span name='linea' > </span> ";
- line = line:gsub("[^%w]", { ["<"] = "<", [">"] = ">", ["&"] = "&", ["'"] = "'", ["="] = "=" }) or "";
- if not comment then
- if line:find("%-%-%[%[") then
- line = line:gsub("%-%-%[%[", function(s) return "<span style='color:green;'>"..s; end);
- if line:find("%]%]") then
- line = line:gsub("%]%]", function(s) return s.."</span>"; end);
- else
- comment = true;
- end
- sText = sText..g..line;
- else
- local waka = false;
- if line:find("%-%-") then
- local a,b,c,e,f = line:find("(.-)(%-%-)(.+)");
- if a then
- if c:find("[\"].-[\"]") then
- c = CheckStrings(c,t,tFs,functions);
- waka = true;
- else
- waka = false;
- end
- if c:find("%[%[") and not c:find("%]%]") then
- c = c:gsub("(.+)(%[%[.+)", function(a,s) return CheckALL(a, multi, t, tFs, functions).."<span style='color:darkmagenta;'>"..s; end);
- multi = true;
- end
- if not waka then
- c = CheckALL(c, multi, t, tFs, functions);
- end
- if not c:find("%[%[") and c:find("%]%]") then
- c = c.."</span>";
- multi = false;
- elseif c:find("%[%[.+%]%]") then
- c = c:gsub("%[%[.+%]%]", function(s) return "<span style='color:darkmagenta;'>"..s.."</span>"; end);
- end
- end
- line = c.."<span style='color:green;'>"..e..f.."</span>";
- else
- if line:find("[\"].-[\"]") then
- waka = true;
- line = CheckStrings(line,t,tFs,functions,d);
- else
- waka = false;
- end
- if line:find("%[%[") and not line:find("%]%]") then
- line = line:gsub("(.+)(%[%[.+)", function(a,s) return CheckALL(a, multi, t, tFs, functions).."<span style='color:darkmagenta;'>"..s; end);
- multi = true;
- end
- if not waka then
- line = CheckALL(line, multi, t, tFs, functions, d);
- end
- if not line:find("%[%[") and line:find("%]%]") then
- line = line.."</span>";
- multi = false;
- elseif line:find("%[%[.+%]%]") then
- line = line:gsub("%[%[.+%]%]", function(s) return "<span style='color:darkmagenta;'>"..s.."</span>"; end);
- end
- end
- sText = sText..g..line;
- end
- else
- if line:find("%]%]") then
- sText = sText..g..line.."</span>";
- comment = false;
- else
- sText = sText..g..line;
- end
- end
- end
- sF:close();
- local sStart = [[<p>Archivo: %s<br/>
- Total de lineas: %d<br/>
- Total de funciones propias: %d (
- <span style='color:gray;'>%s</span> )<br/>
- <input type='button' id='b' onclick='expand();' value='Expandir/Contraer codigo'/>
- <input type='button' onclick='hideLinea();' value='Esconder numero de linea'/>
- <input type='button' onclick='ShowLinea();' value='Mostrar numero de linea'/></p>]];
- sStart = sStart:format(sFile, d,#tFs, table.concat(tFs,', '));
- local sFinish = [[<script>
- var e = document.getElementById('formated');
- var b = document.getElementById('b');
- function expand(){
- e.style.width = 'auto';
- e.style.height = 'auto';
- b.onclick = contraer;
- }
- function contraer(){
- e.style.width = '800px';
- e.style.height = '300px';
- b.onclick = expand
- }
- function hideLinea(){
- aL = document.getElementsByName('linea');
- for(var i=0;i<aL.length;i++){
- aL[i].style.display = 'none';
- }
- }
- function ShowLinea(){
- aL = document.getElementsByName('linea');
- for(var i=0;i<aL.length;i++){
- aL[i].style.display = 'inline';
- }
- }
- </script>]];
- local sRet = sStart.."<pre id='formated' style='text-align:left;width:800px;height:300px; overflow:scroll;'>"..sText.."</pre><h3>Raw code</h3><pre style='text-align:left;width:800px;height:120px; overflow:scroll;'><code>"..sRaw.."</code></pre>"..sFinish;
- if not sFileOut then
- return sRet;
- elseif type(sFileOut) == 'userdata' then
- sFileOut:write(sRet);
- elseif type(sFileOut) == 'string' then
- local sO = io.open(sFileOut, "wb");
- if sO then
- sO:write(sRet);
- sO:close();
- return true;
- else
- return nil, "Couldn't open destination file";
- end
- else
- return nil, 'jeje';
- end
- else
- return nil, "Couldn't open file";
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement