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

sile-typesetter / sile / 11534409649

26 Oct 2024 07:27PM UTC coverage: 33.196% (-28.7%) from 61.897%
11534409649

push

github

alerque
chore(tooling): Update editor-config key for stylua as accepted upstream

Our setting addition is still not in a tagged release, but the PR was
accepted into the default branch of stylua. This means you no longer
need to run my fork of Stylua to get this project's style, you just nead
any build from the main development branch. However the config key was
renamed as part of the acceptance, so this is the relevant adjustment.

5810 of 17502 relevant lines covered (33.2%)

1300.57 hits per line

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

0.0
/packages/pdfstructure/init.lua
1
local base = require("packages.base")
×
2

3
local package = pl.class(base)
×
4
package._name = "pdfstructure"
×
5

6
local pdf
7
local stPointer
8
local mcid = 0
×
9
local actualtext = {}
×
10
local structureNumberTree
11
local numberTreeIndex = 0
×
12

13
local function stNode (notetype)
14
   return {
×
15
      notetype = notetype,
16
      lang = SILE.settings:get("document.language"),
17
      kids = {},
18
      parent = stPointer,
19
   }
20
end
21

22
local function addChild (node)
23
   stPointer.kids[#stPointer.kids + 1] = node
×
24
   node.parent = stPointer
×
25
end
26

27
local function ensureStructureNumber (node, pdfnode)
28
   local p = node.page
×
29
   if not pdf.lookup_dictionary(p, "StructParents") then
×
30
      pdf.add_dict(p, pdf.parse("/StructParents"), pdf.parse(numberTreeIndex))
×
31
      local nums = pdf.lookup_dictionary(structureNumberTree, "Nums")
×
32
      pdf.push_array(nums, pdf.parse(numberTreeIndex))
×
33
      pdf.push_array(nums, pdf.parse("[]"))
×
34
      numberTreeIndex = numberTreeIndex + 1
×
35
   end
36
   local nums = pdf.lookup_dictionary(structureNumberTree, "Nums")
×
37
   -- This is an array and its last element is an array
38
   local r = pdf.get_array(nums, pdf.array_length(nums) - 1)
×
39
   pdf.push_array(r, pdf.reference(pdfnode))
×
40
end
41

42
local function dumpTree (node)
43
   local k = {}
×
44
   local pdfNode = pdf.parse("<< /Type /StructElem /S /" .. node.notetype .. ">>")
×
45
   if #node.kids > 0 then
×
46
      for i = 1, #node.kids do
×
47
         k[#k + 1] = dumpTree(node.kids[i])
×
48
      end
49
      local kArray = pdf.parse("[]")
×
50
      for i = 1, #k do
×
51
         pdf.push_array(kArray, k[i])
×
52
      end
53
      pdf.add_dict(pdfNode, pdf.parse("/K"), kArray)
×
54
   else
55
      pdf.add_dict(pdfNode, pdf.parse("/K"), pdf.parse(node.mcid))
×
56
   end
57
   if node.page then
×
58
      pdf.add_dict(pdfNode, pdf.parse("/Pg"), pdf.reference(node.page))
×
59
      ensureStructureNumber(node, pdfNode)
×
60
   end
61
   if node.lang then
×
62
      pdf.add_dict(pdfNode, pdf.parse("/Lang"), pdf.parse("(" .. node.lang:upper() .. ")"))
×
63
   end
64

65
   if node.actualtext then
×
66
      pdf.add_dict(pdfNode, pdf.parse("/ActualText"), pdf.string(node.actualtext))
×
67
   end
68
   local ref = pdf.reference(pdfNode)
×
69
   pdf.release(pdfNode)
×
70
   return ref
×
71
end
72

73
function package:_init ()
×
74
   base._init(self)
×
75
   pdf = require("justenoughlibtexpdf")
×
76
   local _typeset = SILE.typesetter.typeset
×
77
   SILE.typesetter.typeset = function (node, text)
×
78
      actualtext[#actualtext] = tostring(actualtext[#actualtext]) .. text
×
79
      _typeset(node, text)
×
80
   end
81
   local stRoot = stNode("Document")
×
82
   stPointer = stRoot
×
83
   self:loadPackage("pdf")
×
84
   SILE.outputter:registerHook("prefinish", function ()
×
85
      local catalog = pdf.get_dictionary("Catalog")
×
86
      local structureTree = pdf.parse("<< /Type /StructTreeRoot >>")
×
87
      pdf.add_dict(catalog, pdf.parse("/StructTreeRoot"), pdf.reference(structureTree))
×
88
      structureNumberTree = pdf.parse("<< /Nums [] >>")
×
89
      pdf.add_dict(structureTree, pdf.parse("/ParentTree"), pdf.reference(structureNumberTree))
×
90
      pdf.add_dict(structureTree, pdf.parse("/K"), dumpTree(stRoot))
×
91
      if structureNumberTree then
×
92
         pdf.release(structureNumberTree)
×
93
      end
94
      if structureTree then
×
95
         pdf.release(structureTree)
×
96
      end
97
   end)
98
end
99

100
function package:registerCommands ()
×
101
   self:registerCommand("pdf:structure", function (options, content)
×
102
      local notetype = SU.required(options, "type", "pdf structure")
×
103
      local node = stNode(notetype)
×
104
      addChild(node)
×
105
      node.lang = SILE.settings:get("document.language")
×
106
      if type(SILE.outputter._ensureInit) == "function" then
×
107
         SILE.outputter:_ensureInit()
×
108
      end
109
      node.page = pdf.get_dictionary("@THISPAGE")
×
110
      node.mcid = mcid
×
111
      local oldstPointer = stPointer
×
112
      stPointer = node
×
113
      actualtext[#actualtext + 1] = ""
×
114
      if not options.block then
×
115
         SILE.call("pdf:literal", {}, { "/" .. notetype .. " <</MCID " .. mcid .. " >>BDC" })
×
116
         mcid = mcid + 1
×
117
         SILE.process(content)
×
118
         SILE.call("pdf:literal", {}, { "EMC" })
×
119
      else
120
         SILE.process(content)
×
121
      end
122
      stPointer.actualtext = actualtext[#actualtext]
×
123
      actualtext[#actualtext] = nil
×
124
      stPointer = oldstPointer
×
125
   end)
126

127
   self:registerCommand("pdf:literal", function (_, content)
×
128
      -- NOTE: This method is used by the pdfstructure package and should
129
      -- probably be moved elsewhere, so there's no attempt here to delegate
130
      -- the low-level libtexpdf call to te outputter.
131
      if SILE.outputter._name ~= "libtexpdf" then
×
132
         SU.error("pdf package requires libtexpdf backend")
×
133
      end
134
      SILE.typesetter:pushHbox({
×
135
         value = nil,
136
         height = SILE.types.measurement(0),
137
         width = SILE.types.measurement(0),
138
         depth = SILE.types.measurement(0),
139
         outputYourself = function (_, _, _)
140
            SILE.outputter:drawRaw(content[1])
×
141
         end,
142
      })
143
   end)
144
end
145

146
package.documentation = [[
147
\begin{document}
148
\use[module=packages.pdfstructure]
149
\pdf:structure[type=P]{%
150
For PDF documents to be considered accessible, they must contain a description of the PDF’s document structure.
151
This package allows structure trees to be created and saved to the PDF file.
152
Currently this provides a low-level interface to creating nodes in the tree;
153
   classes which require PDF accessibility should use the \autodoc:command{\pdf:structure} command in their sectioning implementation to declare the document structure.
154
}
155

156
\pdf:structure[type=P]{%
157
See \code{tests/pdf.sil} for an example of using the \autodoc:package{pdfstructure} package to create a PDF/UA compatible document.
158
}
159
\end{document}
160
]]
×
161

162
return package
×
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