From 1b87305b7503c1c7deb88793b428d5278b42f14c Mon Sep 17 00:00:00 2001 From: Fredrick Brennan Date: Thu, 25 Nov 2021 23:43:26 -0500 Subject: [PATCH] fix(packages): Add \pdf:metadata support for dates --- packages/pdf.lua | 20 ++++++++++++++++++-- tests/bug-1280.sil | 7 +++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/pdf.lua b/packages/pdf.lua index 4d20a1cde..7f37aa118 100644 --- a/packages/pdf.lua +++ b/packages/pdf.lua @@ -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 @@ -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), diff --git a/tests/bug-1280.sil b/tests/bug-1280.sil index a4f8dc60e..fa535ae25 100644 --- a/tests/bug-1280.sil +++ b/tests/bug-1280.sil @@ -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}