From 643e4202f4c5a5ee7a87779ffb69928e685db451 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 26 Aug 2024 16:11:02 +0100 Subject: ooo --- xml.lua | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'xml.lua') diff --git a/xml.lua b/xml.lua index 7a0758a..7153831 100644 --- a/xml.lua +++ b/xml.lua @@ -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) -- cgit v1.2.3