Advertisement
Guest User

lua2html v0.9

a guest
Oct 29th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.55 KB | None | 0 0
  1. --[[lua2html by WeBuLtRa v0.9]]--
  2. function lua2html(sFile, sFileOut)
  3.     local keywords = { "while", "function", "for", "require", "elseif", "if", "then", "else", "do", "repeat", "until", "end", "return", "true", "false", "and", "not", "or", "local", "nil", "break", "in"};
  4.     local functions = {":write", ":read", ":close", ":seek", ":gsub", ":find", ":gmatch", ":format", ":lower", ":upper", ":sub", ":byte", ":reverse", ":rep", ":len", ":dump", ":char"};
  5.     local operators = {"%+", "%#", "%-", "%*", "%=", "~", "%."};
  6.     local t = {};
  7.     local sF = io.open(sFile, "rb");
  8.     if sF then
  9.         local function print_r(a,b)
  10.             if b ~= _G then
  11.                 if type(b) == "function" then
  12.                     table.insert(t,a);
  13.                 elseif type(b) == "table" then
  14.                     for x, y in pairs(b) do
  15.                         if type(y) == "function" then
  16.                             table.insert(t,a.."%."..x);
  17.                         end
  18.                     end
  19.                 end
  20.             end
  21.         end
  22.         table.foreach(_G, print_r);
  23.         local function ParseScript(s)
  24.             local tFs, tVars = {}, {};
  25.             for key in string.gmatch(s, "function ([%w%._%:]+)") do
  26.                 table.insert(tFs, #tFs+1, key)
  27.             end
  28.             for key in string.gmatch(s, "(%w+%.%w+) = function") do
  29.                 table.insert(tFs, #tFs+1, key)
  30.             end
  31.             return tFs;
  32.         end
  33.         local function CheckKeywords(line, multi)
  34.             if not multi then
  35.             for x,y in pairs(keywords) do
  36.                     line = line:gsub("([%W]+)("..y..")([%c%s%;%,%(%)]+)",
  37.                         function(a,b,c)
  38.                             return a.."<span style='color:blue;'>"..b.."</span>"..c
  39.                         end
  40.                     );
  41.                     line = line:gsub("^"..y.."%s",
  42.                         function(b)
  43.                             return "<span style='color:blue;'>"..b.."</span>";
  44.                         end
  45.                     );
  46.             end
  47.             end
  48.             return line;
  49.         end
  50.         local function CheckOperators(line, operators, multi)
  51.             if not multi then
  52.                 for x,y in pairs(operators) do
  53.                         line = line:gsub(y, function(a) return "<span style='color:red;'>"..a.."</span>" end);
  54.                 end
  55.             end
  56.             return line;
  57.         end
  58.         local function CheckFunctions(line, t, multi, color)
  59.             if not multi then
  60.                 for x,y in pairs(t) do
  61.                         line = line:gsub("("..y..")%(", function(a) return "<span style='color:"..color..";'>"..a.."</span>(" end);
  62.                 end
  63.             end
  64.             return line;
  65.         end
  66.         local function CheckALL(line, multi,t, tFs, functions)
  67.             line = CheckKeywords(line, multi);
  68.             line = CheckFunctions(line, t, multi, "darkorange");
  69.             line = CheckFunctions(line, functions, multi, "darkorange");
  70.             line = CheckFunctions(line, tFs, multi, "red");
  71.             line = CheckOperators(line, operators, multi);
  72.             line = line:gsub("(&apos;[%w%d%s]-&apos;)", function(a) return "<span style='color:darkmagenta;'>"..a.."</span>" end);
  73.             return line;
  74.         end
  75.         local function CheckStrings(line,t,tFs,functions)
  76.             line = line:gsub("(.-)([\"].-[\"])", function(b,a) return CheckALL(b, multi,t, tFs, functions).."<span style='color:darkmagenta;'>"..a.."</span>" end);
  77.             line = line:gsub('(".+")(.+)', function(a,b)  return a..CheckALL(b, multi,t, tFs, functions) end);
  78.             line = line:gsub("(&apos;[.]+&apos;)", function(a) return "<span style='color:darkmagenta;'>"..a.."</span>" end);
  79.             return line;
  80.         end
  81.         local sText = "";
  82.         local sRaw = sF:read("*a");
  83.         local tFs = ParseScript(sRaw);
  84.         sF:seek("set");
  85.         local comment, multi, d = false, false, 0;
  86.         for line in sF:lines() do
  87.             d = d + 1;
  88.             local g = "<span style='background-color:yellow;' name='linea' >"..d.."</span><span name='linea' >  </span> ";
  89.             line = line:gsub("[^%w]", { ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;", ["'"] = "&apos;", ["="] = "&equals;" }) or "";
  90.             if not comment then
  91.                 if line:find("%-%-%[%[") then
  92.                     line = line:gsub("%-%-%[%[", function(s) return "<span style='color:green;'>"..s; end);
  93.                     if line:find("%]%]") then
  94.                         line = line:gsub("%]%]", function(s) return s.."</span>"; end);
  95.                     else
  96.                         comment = true;
  97.                     end
  98.                     sText = sText..g..line;
  99.                 else
  100.                     local waka = false;
  101.                     if line:find("%-%-") then
  102.                         local a,b,c,e,f = line:find("(.-)(%-%-)(.+)");
  103.                         if a then
  104.                             if c:find("[\"].-[\"]") then
  105.                                 c = CheckStrings(c,t,tFs,functions);
  106.                                 waka = true;
  107.                             else
  108.                                 waka = false;
  109.                             end
  110.                             if c:find("%[%[") and not c:find("%]%]") then
  111.                                 c = c:gsub("(.+)(%[%[.+)", function(a,s) return CheckALL(a, multi, t, tFs, functions).."<span style='color:darkmagenta;'>"..s; end);
  112.                                 multi = true;
  113.                             end
  114.                             if not waka then
  115.                                 c = CheckALL(c, multi, t, tFs, functions);
  116.                             end
  117.                             if not c:find("%[%[") and c:find("%]%]") then
  118.                                 c = c.."</span>";
  119.                                 multi = false;
  120.                             elseif c:find("%[%[.+%]%]") then
  121.                                 c = c:gsub("%[%[.+%]%]", function(s) return "<span style='color:darkmagenta;'>"..s.."</span>"; end);
  122.                             end
  123.                         end
  124.                         line = c.."<span style='color:green;'>"..e..f.."</span>";
  125.                     else
  126.                         if line:find("[\"].-[\"]") then
  127.                             waka = true;
  128.                             line = CheckStrings(line,t,tFs,functions,d);
  129.                         else
  130.                             waka = false;
  131.                         end
  132.                         if line:find("%[%[") and not line:find("%]%]") then
  133.                             line = line:gsub("(.+)(%[%[.+)", function(a,s) return CheckALL(a, multi, t, tFs, functions).."<span style='color:darkmagenta;'>"..s; end);
  134.                             multi = true;
  135.                         end
  136.                         if not waka then
  137.                             line = CheckALL(line, multi, t, tFs, functions, d);
  138.                         end
  139.                         if not line:find("%[%[") and line:find("%]%]") then
  140.                             line = line.."</span>";
  141.                             multi = false;
  142.                         elseif line:find("%[%[.+%]%]") then
  143.                             line = line:gsub("%[%[.+%]%]", function(s) return "<span style='color:darkmagenta;'>"..s.."</span>"; end);
  144.                         end
  145.                     end
  146.                     sText = sText..g..line;
  147.                 end
  148.             else
  149.                 if line:find("%]%]") then
  150.                     sText = sText..g..line.."</span>";
  151.                     comment = false;
  152.                 else
  153.                     sText = sText..g..line;
  154.                 end
  155.             end
  156.         end
  157.         sF:close();
  158.         local sStart = [[<p>Archivo: %s<br/>
  159.             Total de lineas: %d<br/>
  160.             Total de funciones propias: %d (
  161.             <span style='color:gray;'>%s</span> )<br/>
  162.             <input type='button' id='b' onclick='expand();' value='Expandir/Contraer codigo'/>
  163.             <input type='button' onclick='hideLinea();' value='Esconder numero de linea'/>
  164.             <input type='button' onclick='ShowLinea();' value='Mostrar numero de linea'/></p>]];
  165.             sStart = sStart:format(sFile, d,#tFs, table.concat(tFs,', '));
  166.             local sFinish = [[<script>
  167.                     var e = document.getElementById('formated');
  168.                     var b = document.getElementById('b');
  169.                     function expand(){
  170.                         e.style.width = 'auto';
  171.                         e.style.height = 'auto';
  172.                         b.onclick = contraer;
  173.                     }
  174.                     function contraer(){
  175.                         e.style.width = '800px';
  176.                         e.style.height = '300px';
  177.                         b.onclick = expand
  178.                     }
  179.                     function hideLinea(){
  180.                         aL = document.getElementsByName('linea');
  181.                         for(var i=0;i<aL.length;i++){
  182.                             aL[i].style.display = 'none';
  183.                         }
  184.                     }
  185.                     function ShowLinea(){
  186.                         aL = document.getElementsByName('linea');
  187.                         for(var i=0;i<aL.length;i++){
  188.                             aL[i].style.display = 'inline';
  189.                         }
  190.                     }
  191.                 </script>]];
  192.         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;
  193.         if not sFileOut then
  194.             return sRet;
  195.         elseif type(sFileOut) == 'userdata' then
  196.             sFileOut:write(sRet);
  197.         elseif type(sFileOut) == 'string' then
  198.             local sO = io.open(sFileOut, "wb");
  199.             if sO then
  200.                 sO:write(sRet);
  201.                 sO:close();
  202.                 return true;
  203.             else
  204.                 return nil, "Couldn't open destination file";
  205.             end
  206.         else
  207.             return nil, 'jeje';
  208.         end
  209.     else
  210.         return nil, "Couldn't open file";
  211.     end
  212. end
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement