diff options
Diffstat (limited to 'xml.lua')
-rw-r--r-- | xml.lua | 24 |
1 files changed, 20 insertions, 4 deletions
@@ -1,3 +1,6 @@ +-- originally from http://lua-users.org/wiki/LuaXml +-- modified by me a bit + local function parseargs(s) local arg = {} string.gsub(s, "([%-%w]+)=([\"'])(.-)%2", function (w, _, a) @@ -6,6 +9,18 @@ local function parseargs(s) return arg end +local function tag(x) + return setmetatable(x, {__call=function(x, label) + -- search for child with given label + for _,c in ipairs(x) do + if c.label == label then + return c + end + end + return nil + end}) +end + local psingle local function pmulti(s, i, parent) @@ -41,11 +56,11 @@ psingle = function(s, i) end if empty == "/" then - return nexti, {label=label, xarg=parseargs(xarg), empty=true} + return nexti, tag{label=label, xarg=parseargs(xarg), empty=true} elseif c == "" then -- start tag - return multi(s, nexti, {label=label, xarg=parseargs(xarg)}) + return pmulti(s, nexti, tag{label=label, xarg=parseargs(xarg)}) else -- end tag - return nexti, {label=label, close=true} + return nexti, tag{label=label, close=true} end end @@ -63,7 +78,8 @@ local function stanzae(getmore) coroutine.yield(el) buf = buf:sub(ni) else - buf = buf..getmore() + local more = getmore() + buf = buf .. more end end end) |