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

sile-typesetter / sile / 14390735465

10 Apr 2025 09:30PM UTC coverage: 34.559% (-31.7%) from 66.23%
14390735465

push

github

alerque
Merge tag 'v0.15.12' into develop

4 of 4 new or added lines in 3 files covered. (100.0%)

4064 existing lines in 73 files now uncovered.

6918 of 20018 relevant lines covered (34.56%)

2597.26 hits per line

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

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

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

7
inputter.order = 2
99✔
8

9
local function startcommand (parser, command, options)
10
   -- Discard list values (non-key/value), stuffed by LXP/expat to make it possible to deduce the order of keys in
11
   -- the source. We're not using it, so we don't care and it is clutter in the AST that makes it different from
12
   -- ASTs generated from SIL inputs.
13
   for i = 1, #options do
809✔
14
      options[i] = nil
91✔
15
   end
16
   local stack = parser:getcallbacks().stack
718✔
17
   local lno, col, pos = parser:pos()
718✔
18
   local position = { lno = lno, col = col, pos = pos }
718✔
19
   -- create an empty command which content will be filled on closing tag
20
   local element = SU.ast.createCommand(command, options, nil, position)
718✔
21
   table.insert(stack, element)
718✔
22
end
23

24
local function endcommand (parser, command)
25
   local stack = parser:getcallbacks().stack
718✔
26
   local element = table.remove(stack)
718✔
27
   assert(element.command == command)
718✔
28
   local level = #stack
718✔
29
   table.insert(stack[level], element)
718✔
30
end
31

32
local function text (parser, msg)
33
   local stack = parser:getcallbacks().stack
1,069✔
34
   local element = stack[#stack]
1,069✔
35
   local n = #element
1,069✔
36
   if type(element[n]) == "string" then
1,069✔
UNCOV
37
      element[n] = element[n] .. msg
×
38
   else
39
      table.insert(element, msg)
1,069✔
40
   end
41
end
42

43
local function parse (doc)
44
   local content = {
16✔
45
      StartElement = startcommand,
16✔
46
      EndElement = endcommand,
16✔
47
      CharacterData = text,
16✔
48
      _nonstrict = true,
49
      stack = { {} },
16✔
50
   }
51
   local parser = lxp.new(content)
16✔
52
   local status, err
53
   if type(doc) == "string" then
16✔
54
      status, err = parser:parse(doc)
16✔
55
      if not status then
16✔
56
         return nil, err
×
57
      end
58
   else
UNCOV
59
      for element in pairs(doc) do
×
UNCOV
60
         status, err = parser:parse(element)
×
UNCOV
61
         if not status then
×
62
            return nil, err
×
63
         end
64
      end
65
   end
66
   status, err = parser:parse()
16✔
67
   if not status then
16✔
UNCOV
68
      return nil, err
×
69
   end
70
   parser:close()
16✔
71
   return content.stack[1][1]
16✔
72
end
73

74
function inputter.appropriate (round, filename, doc)
99✔
75
   if round == 1 then
103✔
76
      return filename:match(".xml$")
99✔
77
   elseif round == 2 then
4✔
78
      local sniff = doc:sub(1, 100):gsub("begin.*", "") or ""
6✔
79
      local promising = sniff:match("<")
3✔
80
      return promising and inputter.appropriate(3, filename, doc)
4✔
81
   elseif round == 3 then
1✔
82
      local _, err = parse(doc)
1✔
83
      return not err
1✔
84
   end
85
end
86

87
function inputter:parse (doc)
99✔
88
   local tree, err = parse(doc)
15✔
89
   if not tree then
15✔
UNCOV
90
      SU.error(err)
×
91
   end
92
   -- XML documents can have any root element, and it should be up to the class
93
   -- to supply handling far whatever that element that is in a specific format.
94
   -- Hence we wrap the actual DOM in an extra element of our own if and only if
95
   -- it doesn't look like a native SILE one already.
96
   local rootelement = tree.command
15✔
97
   if rootelement ~= "sile" and rootelement ~= "document" then
15✔
UNCOV
98
      tree = SU.ast.createCommand("document", {}, tree)
×
99
   end
100
   return { tree }
15✔
101
end
102

103
return inputter
99✔
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