viernes, 28 de octubre de 2011

lua2html script

Este es un pequeño script para pasar código de un archivo lua a html. El resultado puede ser retornado a un string, escrito en un file handler o en un archivo nuevo.

La marcación funciones son las que esten disponibles en el actual lua state desde donde se ejecuta así que es compatible con cualquier sdk.

Al dar el código fuente pueden modificarlo como quieran (como que sólo retorne cierto html, etc.) pero eso ya va de ustedes.


Link to pastebin: http://pastebin.com/73Hc2PHF

Aca el mismo script analizado por si mismo:


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

No hay comentarios:

Publicar un comentario