lua-users home
lua-l archive

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


On 2012-11-18 6:03 PM, "Jay Carlson" <nop@nop.com> wrote:
>
> 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>"
>
> :-)

An issue I've found with this idea though (really any HTML generator) is HTML is pretty silly. There are some tags that have to use short form, and some that can't:
<script src="" /> -- invalid, and browsers will treat this as an unclosed <script> that will eat the entire page
<br></br> -- invalid AFAIK, at least very silly.

The only solution I know of is to have a table listing what tags can't use short form, but I don't know them all. (I really wonder why there are any...)

p.s. another nice side effect of this type of HTML generation is, if you keep track of recursion depth, it's fairly simple to add the necessary whitespace to generate nicely indented readable code. But again HTML makes things a little more difficult, as adding whitespace can sometimes change the result... It's good for debugging the generated code at least.