Skip to content

Commit

Permalink
fix(packages): Add \pdf:metadata support for dates
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlcctrlv authored and alerque committed Dec 16, 2021
1 parent b508660 commit 1b87305
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
20 changes: 18 additions & 2 deletions packages/pdf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 106,10 @@ SILE.registerCommand("pdf:link", function (options, content)
})
end)

local function validate_date(date)
return string.match(date, [[^D:%d %s*-%s*%d%d%s*'%s*%d%d%s*'?$]]) ~= nil
end

SILE.registerCommand("pdf:metadata", function (options, _)
local key = SU.required(options, "key", "pdf:metadata")
local value
Expand All @@ -116,8 120,20 @@ SILE.registerCommand("pdf:metadata", function (options, _)
value = SU.required(options, "value", "pdf:metadata")
end

-- see comment in pdf:bookmark
value = SU.utf8_to_utf16be(value)
if key == "Trapped" then
SU.warn("Skipping special metadata key \\Trapped")
return
end

if key == "ModDate" or key == "CreationDate" then
if not validate_date(value) then
SU.warn("Invalid date: " .. value)
return
end
else
-- see comment in pdf:bookmark
value = SU.utf8_to_utf16be(value)
end
SILE.typesetter:pushHbox({
value = nil,
height = SILE.measurement(0),
Expand Down
7 changes: 5 additions & 2 deletions tests/bug-1280.sil
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 49,9 @@
SILE.registerCommand("pdf:metadata", function (_, _) end)
end
}
\pdf:metadata[key=Author, val=Erwin Schrödinger]
\pdf:metadata[key=Title, val=Erwin Schrödinger’s 猫🐈]
\pdf:metadata[key=Author, value=Erwin Schrödinger]
\pdf:metadata[key=Title, value=Erwin Schrödinger’s 猫🐈]
\pdf:metadata[key=CreationDate, value=D:19990209153925 - 08 ' 00 ']
\pdf:metadata[key=ModDate, value=should fail]
\pdf:metadata[key=Trapped, value=should be skipped]
\end{document}

0 comments on commit 1b87305

Please sign in to comment.