• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 9304060604

30 May 2024 02:07PM UTC coverage: 74.124% (-0.6%) from 74.707%
9304060604

push

github

alerque
style: Reformat Lua with stylua

8104 of 11995 new or added lines in 184 files covered. (67.56%)

15 existing lines in 11 files now uncovered.

12444 of 16788 relevant lines covered (74.12%)

7175.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.48
/inputters/xml.lua
1
local base = require("inputters.base")
181✔
2
local lxp = require("lxp")
181✔
3

4
local inputter = pl.class(base)
181✔
5
inputter._name = "xml"
181✔
6

7
inputter.order = 2
181✔
8

9
local function startcommand (parser, command, options)
10
   local stack = parser:getcallbacks().stack
750✔
11
   local lno, col, _ = parser:pos()
750✔
12
   local element = { command = command, options = options, lno = lno, col = col }
750✔
13
   table.insert(stack, element)
750✔
14
end
15

16
local function endcommand (parser, command)
17
   local stack = parser:getcallbacks().stack
750✔
18
   local element = table.remove(stack)
750✔
19
   assert(element.command == command)
750✔
20
   local level = #stack
750✔
21
   table.insert(stack[level], element)
750✔
22
end
23

24
local function text (parser, msg)
25
   local stack = parser:getcallbacks().stack
1,126✔
26
   local element = stack[#stack]
1,126✔
27
   local n = #element
1,126✔
28
   if type(element[n]) == "string" then
1,126✔
NEW
29
      element[n] = element[n] .. msg
×
30
   else
31
      table.insert(element, msg)
1,126✔
32
   end
33
end
34

35
local function parse (doc)
36
   local content = {
40✔
37
      StartElement = startcommand,
40✔
38
      EndElement = endcommand,
40✔
39
      CharacterData = text,
40✔
40
      _nonstrict = true,
41
      stack = { {} },
40✔
42
   }
43
   local parser = lxp.new(content)
40✔
44
   local status, err
45
   if type(doc) == "string" then
40✔
46
      status, err = parser:parse(doc)
40✔
47
      if not status then
40✔
NEW
48
         return nil, err
×
49
      end
50
   else
NEW
51
      for element in pairs(doc) do
×
NEW
52
         status, err = parser:parse(element)
×
NEW
53
         if not status then
×
NEW
54
            return nil, err
×
55
         end
56
      end
57
   end
58
   status, err = parser:parse()
40✔
59
   if not status then
40✔
NEW
60
      return nil, err
×
61
   end
62
   parser:close()
40✔
63
   return content.stack[1][1]
40✔
64
end
65

66
function inputter.appropriate (round, filename, doc)
181✔
67
   if round == 1 then
186✔
68
      return filename:match(".xml$")
182✔
69
   elseif round == 2 then
4✔
70
      local sniff = doc:sub(1, 100):gsub("begin.*", "") or ""
6✔
71
      local promising = sniff:match("<")
3✔
72
      return promising and inputter.appropriate(3, filename, doc)
4✔
73
   elseif round == 3 then
1✔
74
      local _, err = parse(doc)
1✔
75
      return not err
1✔
76
   end
77
end
78

79
function inputter.parse (_, doc)
181✔
80
   local tree, err = parse(doc)
39✔
81
   if not tree then
39✔
NEW
82
      SU.error(err)
×
83
   end
84
   -- XML documents can have any root element, and it should be up to the class
85
   -- to supply handling far whatever that element that is in a specific format.
86
   -- Hence we wrap the actual DOM in an extra element of our own if and only if
87
   -- it doesn't look like a native SILE one already.
88
   local rootelement = tree.command
39✔
89
   if rootelement ~= "sile" and rootelement ~= "document" then
39✔
NEW
90
      tree = { tree, command = "document" }
×
91
   end
92
   return { tree }
39✔
93
end
94

95
return inputter
181✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc