#!/usr/local/bin/lua ------------------------------------------------------------------------------ -- Lua 2 HTML ------------------------------------------------------------------------------ assert(loadlib('/usr/local/lib/liblexlua.so','openLexLua'))() local special = { ['&'] = '&'; ['"'] = '"'; ['<'] = '<'; ['>'] = '>'; } local function quote(str) return( string.gsub( str, '([&"<>])', function(x) return special[x] end ) ) end function colour(color) local b = '' local e = '' return function(x) return b .. quote(x) .. e end end function html(t,f) local b = '<'..t..'>' local e = ''..t..'>' return function(x) return b .. f(x) .. e end end literal = colour'Crimson' operator = html('B',colour'Teal') decl = colour'Blue' ctrl = colour'Brown' std = colour'Teal' comment = colour'Green' local keywords = { ['nil'] = literal; ['true'] = literal; ['false'] = literal; ['not'] = operator; ['and'] = operator; ['or'] = operator; ['global'] = decl; ['local'] = decl; ['function'] = decl; ['return'] = decl; ['break'] = decl; ['end'] = ctrl; ['if'] = ctrl; ['then'] = ctrl; ['else'] = ctrl; ['elseif'] = ctrl; ['for'] = ctrl; ['in'] = ctrl; ['while'] = ctrl; ['do'] = ctrl; ['repeat'] = ctrl; ['until'] = ctrl; } local stdfunctions = { arg = std; self = std; _G = std; _LOADED = std; _TRACEBACK = std; _REQUIREDNAME = std; _VERSION = std; assert = std; collectgarbage = std; coroutine = std; create = std; yield = std; debug = std; debug = std; gethook = std; getinfo = std; getlocal = std; sethook = std; setlocal = std; traceback = std; dofile = std; error = std; gcinfo = std; getglobals = std; getmetatable = std; getmode = std; io = std; close = std; flush = std; input = std; open = std; output = std; popen = std; read = std; stderr = std; stdin = std; stdout = std; tmpfile = std; write = std; ipairs = std; loadfile = std; loadlib = std; loadstring = std; math = std; abs = std; acos = std; asin = std; atan = std; atan2 = std; ceil = std; cos = std; deg = std; exp = std; floor = std; frexp = std; ldexp = std; log = std; log10 = std; max = std; min = std; mod = std; pi = std; pow = std; rad = std; random = std; randomseed = std; sin = std; sqrt = std; tan = std; newproxy = std; next = std; os = std; clock = std; date = std; difftime = std; execute = std; exit = std; getenv = std; remove = std; rename = std; setlocale = std; time = std; tmpname = std; pairs = std; pcall = std; print = std; rawequal = std; rawget = std; rawset = std; require = std; setglobals = std; setmetatable = std; setmode = std; string = std; byte = std; char = std; find = std; format = std; gfind = std; gsub = std; len = std; lower = std; rep = std; sub = std; upper = std; table = std; concat = std; foreach = std; foreachi = std; getn = std; insert = std; remove = std; setn = std; sort = std; tonumber = std; tostring = std; type = std; unpack = std; xpcall = std; __index = std; __newindex = std; __gc = std; __eq = std; __gettable = std; __settable = std; __add = std; __sub = std; __mul = std; __div = std; __pow = std; __unm = std; __lt = std; __le = std; __concat = std; __call = std; __tostring = std; __metatable = std; } local other = { ['='] = decl; ['{'] = html('B',decl); ['}'] = html('B',decl); ['+'] = operator; ['-'] = operator; ['*'] = operator; ['/'] = operator; ['^'] = operator; ['<'] = operator; ['<='] = operator; ['>'] = operator; ['>='] = operator; ['=='] = operator; ['~='] = operator; } local fh = assert( io.open(arg[1]) ) local lexer = LexLua( assert( fh:read'*a' )) assert( fh:close() ) local function identity(x) return x end local line_num = 0 local function lnumber() line_num=line_num+1 return string.format( '\n%4d ', line_num ) end io.write'
' io.write(lnumber()) while 1 do local tok,tt,ws = lexer() ws = string.gsub( ws, '(%S[^\n]*)', comment ) ws = string.gsub( ws, '\n', lnumber ) io.write(ws) if not tok then break end if tt == ''' then local fmt = stdfunctions[tok] or identity io.write( fmt(tok) ) elseif tt == ' ' then io.write( literal(tok) ) elseif tt == ' ' then tok = string.gsub( tok, '([^\n]+)', literal ) tok = string.gsub( tok, '\n', lnumber ) io.write(tok) else local fmt = keywords[tok] or other[tok] or identity io.write( fmt(tok) ) end end io.write'