lua-users home
lua-l archive

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


This came from the following discussion about XML pretty-printing, and
I used Mathew Wild's stanza.lua as a departure point (sometimes OSS is
very much like permitted stealing.)

http://lua-users.org/lists/lua-l/2010-08/msg00715.html

doc.lua provides a flexible XML pretty-printer for LOM documents:

    local doc = require 'lxp.doc'
    local lom = require 'lxp.lom'
    local d = lom.parse '<abc a1="A1" a2="A2"><ef>hello</ef></abc>'
    print(doc.tostring(d,'','  '))

which gives the following output, with an initial indent of '' and a
per-element indent of '  '.

    <abc a1='A1' a2='A2'>
      <ef>hello</ef>
    </abc>

Setting the attribute indent with `doc.tostring(d,'','  ','  ')` we
get exactly what Alexander was originally looking for:

    <abc
      a1='A1'
      a2='A2'>
      <ef>hello</ef>
    </abc>

Apart from the stanza-style LOM building, it provides an
Orbit-htmlify-like LOM builder (except that the element constructors
are explicitly declared and no function environment magic takes
place.)

    local children,child,toy,name,age = doc.tags 'children,child,toy,name,age'

    d2 = children {
        child {name 'alice', age '5', toy {type='fluffy'}},
        child {name 'bob', age '6', toy {type='squeaky'}}
    }

There is also LOM template expansion and LOM structural pattern
matching, which is Tamale-like but uses partial matching and allows
for explicit repeating.

The README.md goes in more detail. This is very much a preliminary API
'executable proposal' sketch, and I welcome any suggestions on more
logical structuring.

You can get it from the Github page (the download button is pretty
obvious these days ;)). The tests assume that doc.lua is sitting in
the lxp namespace, but doc.lua itself does not care where it goes.

http://http://github.com/stevedonovan/LuaExpatUtils

steve d.