summaryrefslogtreecommitdiff
path: root/xml_old.lua
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-09-06 16:52:27 +0100
committerubq323 <ubq323@ubq323.website>2025-02-20 15:52:55 +0000
commitbf776624fb59d147b82d2a6a13c36292844a47b7 (patch)
tree7feb87b5caabe7ff3cc3fa662659b651bbb064aa /xml_old.lua
parent20937f1155d345eade9d6b538ec8a7fc4859e17e (diff)
mysterious uncommited changes
(actually committing 2025-02-20)
Diffstat (limited to 'xml_old.lua')
-rw-r--r--xml_old.lua48
1 files changed, 48 insertions, 0 deletions
diff --git a/xml_old.lua b/xml_old.lua
new file mode 100644
index 0000000..a63c0f7
--- /dev/null
+++ b/xml_old.lua
@@ -0,0 +1,48 @@
+function parseargs(s)
+ local arg = {}
+ string.gsub(s, "([%-%w]+)=([\"'])(.-)%2", function (w, _, a)
+ arg[w] = a
+ end)
+ return arg
+end
+
+function collect(s)
+ local stack = {}
+ local top = {}
+ table.insert(stack, top)
+ local ni,c,label,xarg, empty
+ local i, j = 1, 1
+ while true do
+ ni,j,c,label,xarg, empty = string.find(s, "<(%/?)([%w:]+)(.-)(%/?)>", i)
+ if not ni then break end
+ local text = string.sub(s, i, ni-1)
+ if not string.find(text, "^%s*$") then
+ table.insert(top, text)
+ end
+ if empty == "/" then -- empty element tag
+ table.insert(top, {label=label, xarg=parseargs(xarg), empty=1})
+ elseif c == "" then -- start tag
+ top = {label=label, xarg=parseargs(xarg)}
+ table.insert(stack, top) -- new level
+ else -- end tag
+ local toclose = table.remove(stack) -- remove top
+ top = stack[#stack]
+ if #stack < 1 then
+ error("nothing to close with "..label)
+ end
+ if toclose.label ~= label then
+ error("trying to close "..toclose.label.." with "..label)
+ end
+ table.insert(top, toclose)
+ end
+ i = j+1
+ end
+ local text = string.sub(s, i)
+ if not string.find(text, "^%s*$") then
+ table.insert(stack[#stack], text)
+ end
+ if #stack > 1 then
+ error("unclosed "..stack[#stack].label)
+ end
+ return stack[1]
+end