Skip to content

Commit

Permalink
chore(packages): Work on making pdfstructure package idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Jun 15, 2022
1 parent 2234b11 commit 5928470
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 70 deletions.
128 changes: 64 additions & 64 deletions packages/pdfstructure/init.lua
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
local pdf = require("justenoughlibtexpdf")
local function init (class, _)

local stPointer
local pdf = require("justenoughlibtexpdf")

local stNode = function (notetype) return {
notetype = notetype,
lang = SILE.settings:get("document.language"),
kids = {},
parent = stPointer
}
end
local stPointer

local stRoot = stNode("Document")
stPointer = stRoot
local mcid = 0
local stNode = function (notetype) return {
notetype = notetype,
lang = SILE.settings:get("document.language"),
kids = {},
parent = stPointer
}
end

local addChild = function (node)
stPointer.kids[#(stPointer.kids)+1] = node
node.parent = stPointer
end
local stRoot = stNode("Document")
stPointer = stRoot
local mcid = 0

local actualtext = {}
local _typeset = SILE.typesetter.typeset
SILE.typesetter.typeset = function (self, text)
actualtext[#actualtext] = tostring(actualtext[#actualtext]) .. text
_typeset(self, text)
end
local addChild = function (node)
stPointer.kids[#(stPointer.kids)+1] = node
node.parent = stPointer
end

local structureNumberTree
local numberTreeIndex = 0
local ensureStructureNumber = function ( node, pdfnode )
local p = node.page
if not pdf.lookup_dictionary(p, "StructParents") then
pdf.add_dict(p, pdf.parse("/StructParents"), pdf.parse(numberTreeIndex))
local nums = pdf.lookup_dictionary(structureNumberTree, "Nums")
pdf.push_array(nums, pdf.parse(numberTreeIndex))
pdf.push_array(nums, pdf.parse("[]"))
numberTreeIndex = numberTreeIndex + 1
local actualtext = {}
local _typeset = SILE.typesetter.typeset
SILE.typesetter.typeset = function (self, text)
actualtext[#actualtext] = tostring(actualtext[#actualtext]) .. text
_typeset(self, text)
end
local nums = pdf.lookup_dictionary(structureNumberTree, "Nums")
-- This is an array and its last element is an array
local r = pdf.get_array(nums, pdf.array_length(nums)-1)
pdf.push_array(r, pdf.reference(pdfnode))
end

local dumpTree
dumpTree = function (node)
local k = {}
local pdfNode = pdf.parse("<< /Type /StructElem /S /"..(node.notetype)..">>")
if #(node.kids) > 0 then
for i = 1, #(node.kids) do
k[#k+1] = dumpTree(node.kids[i])
local structureNumberTree
local numberTreeIndex = 0
local ensureStructureNumber = function ( node, pdfnode )
local p = node.page
if not pdf.lookup_dictionary(p, "StructParents") then
pdf.add_dict(p, pdf.parse("/StructParents"), pdf.parse(numberTreeIndex))
local nums = pdf.lookup_dictionary(structureNumberTree, "Nums")
pdf.push_array(nums, pdf.parse(numberTreeIndex))
pdf.push_array(nums, pdf.parse("[]"))
numberTreeIndex = numberTreeIndex + 1
end
local kArray = pdf.parse("[]")
for i = 1, #k do pdf.push_array(kArray, k[i]) end
pdf.add_dict(pdfNode, pdf.parse("/K"), kArray)
else
pdf.add_dict(pdfNode, pdf.parse("/K"), pdf.parse(node.mcid))
end
if node.page then
pdf.add_dict(pdfNode, pdf.parse("/Pg"), pdf.reference(node.page))
ensureStructureNumber(node, pdfNode)
end
if node.lang then
pdf.add_dict(pdfNode, pdf.parse("/Lang"), pdf.parse("("..node.lang:upper()..")"))
local nums = pdf.lookup_dictionary(structureNumberTree, "Nums")
-- This is an array and its last element is an array
local r = pdf.get_array(nums, pdf.array_length(nums)-1)
pdf.push_array(r, pdf.reference(pdfnode))
end

if node.actualtext then
pdf.add_dict(pdfNode, pdf.parse("/ActualText"), pdf.string(node.actualtext))
end
local ref = pdf.reference(pdfNode)
pdf.release(pdfNode)
return ref
end
local dumpTree
dumpTree = function (node)
local k = {}
local pdfNode = pdf.parse("<< /Type /StructElem /S /"..(node.notetype)..">>")
if #(node.kids) > 0 then
for i = 1, #(node.kids) do
k[#k+1] = dumpTree(node.kids[i])
end
local kArray = pdf.parse("[]")
for i = 1, #k do pdf.push_array(kArray, k[i]) end
pdf.add_dict(pdfNode, pdf.parse("/K"), kArray)
else
pdf.add_dict(pdfNode, pdf.parse("/K"), pdf.parse(node.mcid))
end
if node.page then
pdf.add_dict(pdfNode, pdf.parse("/Pg"), pdf.reference(node.page))
ensureStructureNumber(node, pdfNode)
end
if node.lang then
pdf.add_dict(pdfNode, pdf.parse("/Lang"), pdf.parse("("..node.lang:upper()..")"))
end

local function init (class, _)
if node.actualtext then
pdf.add_dict(pdfNode, pdf.parse("/ActualText"), pdf.string(node.actualtext))
end
local ref = pdf.reference(pdfNode)
pdf.release(pdfNode)
return ref
end

class:loadPackage("pdf")

Expand Down
7 changes: 1 addition & 6 deletions spec/packages_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ describe("#package", function ()

for pkg in lfs.dir("packages") do

if pkg ~= ".." and pkg ~= "."
and pkg ~= "pandoc"
and pkg ~= "pdf"
and pkg ~= "pdfstructure"
and pkg ~= "url"
then
if pkg ~= ".." and pkg ~= "." then

describe(pkg, function ()

Expand Down

0 comments on commit 5928470

Please sign in to comment.