lua-users home
lua-l archive

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


On Nov 16, 2012, at 11:47 PM, Tangent 128 wrote:

> Lua 5.2's _ENV functionality naturally expedites some fun syntax tricks;
> partly for a side project, and partly for the fun of it, I wrote a
> module that creates an _ENV object with a metatable to synthesize
> HTML-wrapping functions.

> Implementaion code at
> http://fossil.tangent128.name/LuaForum/artifact/adbc426fafc3cb1f37e0a1a2d7f4f50cc4a498c0

One very nice thing is that it produces correct HTML without any extra effort. Strings encountered during processing are considered to be...strings. There is a separate type for "HTML fragment" (a particular metatable) so you have to go out of your way to let people inject cross-side scripting. You mark strings with rawHTML() if you're willing to guarantee that what you're putting in is well-formed, properly quoted HTML.

I know the point was _ENV stuff, but entirely too much Web hackery requires special effort to keep these things straight, and I'm happy to see good examples.

There's still charset correctness to worry about, but standard Lua doesn't help much there (since C doesn't either).

OTOH, your example had a fun braino:

> do _ENV = htmlua
...
> end
> -- use tostring(result)

tostring(result) ->" <tostring>"..result.."</tostring>"
print(result) -> "<print>"..result.."</print>"

:-)