News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
murgaLua to html
Author Message
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #1
RE: murgaLua to html

Made a few improvements to it this morning.
It's still sloppy, but the results are better than before.
I'm hoping it will eventually create w3c compliant HTML, but that will be a last step. First I need to deal with setting the output filename dynamically (just haven't bothered to fix that yet) and try to work out nested/multiline stuff.

Code:
filename="/home/dsl/widgets_demo/widgets.lua"
rc="#FFFF00" -- color for reserved words
pc="#00FFFF" -- "name.function" functions
fc="#00FFFF" -- lua functions
sc="#FF0000" -- strings
nc="#FF0000" -- numbers
mc="#00FF00" -- methods
wc="#FF00FF" -- FLTK classes
ec="#FF0000" -- FLTK constants
cc="#0000FF" -- comments

reserved={ "and","break","do","else","elseif","end","false","for","function","if","in",
           "local","nil","not","or","repeat","return","then","true","until","while" }
package={ "copas","coroutine","debug","fltk","gzip","io","lfs","lzo","math","md5","mime",
          "murgaLua","os","package","random","socket","sqlite3","string","table","zlib" }
funk={ "_G","_VERSION","assert","collectgarbage","dofile","error","getfenv","getmetatable",
       "ipairs","load","loadfile","loadstring","module","next","pairs","pcall","print",
       "rawequal","rawget","rawset","require","select","setfenv","setmetatable","tonumber",
       "tostring","type","unpack","xpcall" }

function parse_table(tab,color,pre,post)
for i=1,table.getn(tab) do
  mystring=string.gsub(mystring,pre..tab[i]..post,"%1<font color="..color..">%2</font>%3")
end
end

input=io.open(filename)
if input then
input:close()
print("<html><head><title>"..filename.."</title></head><body bgcolor=black text=white><pre>")
for line in io.lines(filename) do
mystring=" "..line.." " -- spaces allow %W to work with beginning/end of string
mystring=string.gsub(mystring,"%&","&amp;")
mystring=string.gsub(mystring,"%<","&lt;")
mystring=string.gsub(mystring,"%>","&gt;")
mystring=string.gsub(mystring,"(%D*)(%d+)(%D)","%1<font color="..nc..">%2</font>%3") -- digits (do this before any colors are defined)
mystring=string.gsub(mystring,'(".-")',"<font color="..sc..">%1</font>") -- quotes
mystring=string.gsub(mystring,"(%:%l.-)(%()","<font color="..mc..">%1</font>%2") -- methods
parse_table(reserved,rc,"(%W)(",")(%W)")
parse_table(package,pc,"(%W)(","%.[%w_]*)(%W)")
parse_table(funk,fc,"([^:%w])(",")(%W)")
mystring=string.gsub(mystring,"(%W)(fltk%:Fl[_%w]*)(%W)","%1<font color="..wc..">%2</font>%3") -- fltk:Fl_*
mystring=string.gsub(mystring,"(%W)(fltk%.FL[_%u]*)(%W)","%1<font color="..ec..">%2</font>%3") -- fltk constants

-- wipe out formatting from "--" comments
commentstart,n=string.find(mystring,"%-%-")
if commentstart then
  if commentstart == 1 then
    mystring=string.gsub(mystring,"%<.-%>","")
    mystring="<font color="..cc..">"..mystring.."</font>"
  else
    firststring=string.sub(mystring,1,commentstart-1)
    secondstring=string.sub(mystring,commentstart,string.len(mystring))
    secondstring=string.gsub(secondstring,"%<.-%>","")
    mystring=firststring.."<font color="..cc..">"..secondstring.."</font>"
  end
end

print(mystring)
end
--end
print("</pre></body></html>")
end

This post was last modified: 03-10-2008 05:55 AM by mikshaw.

03-10-2008 05:44 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Messages In This Thread
murgaLua to html - mikshaw - 03-09-2008, 05:42 AM
RE: murgaLua to html - iGame3D - 03-09-2008, 06:52 AM
RE: murgaLua to html - mikshaw - 03-09-2008, 07:56 AM
RE: murgaLua to html - mikshaw - 03-09-2008, 08:31 AM
RE: murgaLua to html - mikshaw - 03-10-2008 05:44 AM
RE: murgaLua to html - mikshaw - 03-12-2008, 04:59 AM
RE: murgaLua to html - mikshaw - 03-13-2008, 03:47 AM
RE: murgaLua to html - iGame3D - 03-13-2008, 10:10 AM
RE: murgaLua to html - mikshaw - 03-13-2008, 10:34 AM
RE: murgaLua to html - mikshaw - 03-13-2008, 12:01 PM

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: