Skip to content

Commit

Permalink
refactor(inputters): Move XML parser setup local to reader
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Jul 16, 2022
1 parent 502aede commit 590a071
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 56 deletions.
55 changes: 52 additions & 3 deletions inputters/xml.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local base = require("inputters.base")

local lom = require("lomwithpos")
local lxp = require("lxp")

local xml = pl.class(base)
xml._name = "xml"
Expand All @@ -20,8 +19,58 @@ function xml.appropriate (round, filename, doc)
end
end

local function startcommand (parser, command, options)
local stack = parser:getcallbacks().stack
local lno, col, pos = parser:pos()
local element = { command = command, options = options, lno = lno, col = col }
table.insert(stack, element)
end

local function endcommand (parser, command)
local stack = parser:getcallbacks().stack
local element = table.remove(stack)
assert(element.command == command)
local level = #stack
table.insert(stack[level], element)
end

local function text (parser, text)
local stack = parser:getcallbacks().stack
local element = stack[#stack]
local n = #element
if type(element[n]) == "string" then
element[n] = element[n] .. text
else
table.insert(element, text)
end
end

local function parse (doc)
local content = { StartElement = startcommand,
EndElement = endcommand,
CharacterData = text,
_nonstrict = true,
stack = {{}}
}
local parser = lxp.new(content)
local status, err
if type(doc) == "string" then
status, err = parser:parse(doc)
if not status then return nil, err end
else
for element in pairs(doc) do
status, err = parser:parse(element)
if not status then return nil, err end
end
end
status, err = parser:parse()
if not status then return nil, err end
parser:close()
return content.stack[1][1]
end

function xml.parse (_, doc)
local tree, err = lom.parse(doc)
local tree, err = parse(doc)
if not tree then
SU.error(err)
end
Expand Down
53 changes: 0 additions & 53 deletions lua-libraries/lomwithpos.lua

This file was deleted.

0 comments on commit 590a071

Please sign in to comment.