lua-users home
lua-l archive

Re: Narcissus in Lua (Javascript parser)

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Oct 15, 2010 at 1:53 PM, Javier Guerra Giraldez
<javier@guerrag.com> wrote:
> but the syntax is certainly ugly.
Yep. I spent some time removing the ugly H prefix and the extra nil's,
and this is what I came up with:
----
local _print = print
setfenv(1, setmetatable({}, {
 __index = function(env, tag)
 local func = function(attrs, c, ...)
 if type(attrs) == "string" then
 if c then
 c = table.concat({attrs, c, ...})
 else
 c = attrs
 end
 attrs = nil
 elseif c then
 c = table.concat({c, ...})
 end
 local o = {('<%s'):format(tag)}
 if attrs then
 for k,v in pairs(attrs) do
 table.insert(o, (' %s="%s"'):format(k,v))
 end
 end
 if c then
 table.insert(o, '>')
 table.insert(o, c)
 table.insert(o, ('</%s>'):format(tag))
 else
 table.insert(o, ' />')
 end
 table.insert (o, '\n')
 return table.concat(o)
 end
 rawset(env, tag, func)
 return func
 end,
}))
_print(
 html(
 head(
 title('page title')),
 body(
 div({id = 'header', class = 'banner'},
 p('header div'))
 div({id = 'main'},
 p('main part'),
 img({src = 'lolcats/purr.jpg', alt = 'lol'}),
 a({href = 'somewhere/else.html'}, 'lets go')),
 div({id = 'footer'},
 p('the end'))))
)
----
~Jonathan

AltStyle によって変換されたページ (->オリジナル) /