From f5908fa185552d15fda0f5cfa6cefc4de29c1f33 Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Fri, 31 May 2024 15:25:57 -0400 Subject: [PATCH] backport fix for #2510 always use /Producer field in document info to credit Asciidoctor PDF and Prawn --- CHANGELOG.adoc | 6 +++- lib/asciidoctor/pdf/converter.rb | 9 +++-- spec/pdf_info_spec.rb | 58 ++++++++++++++++++++++---------- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 81958a568..b7cd03438 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -7,7 +7,11 @@ For a detailed view of what has changed, refer to the {url-repo}/commits/main[co == Unreleased -_No changes since previous release._ +Improvements:: + +* always use /Producer field in document info to credit Asciidoctor PDF and Prawn, even when author is set (#2510) +* map `producer` attribute to /Producer field in document info to override or default value (#2510) +* map `publisher` attribute to /Creator field instead of /Producer field in document info; use author as fallback (#2510) == 2.3.16 (2024-05-31) - @mojavelinux diff --git a/lib/asciidoctor/pdf/converter.rb b/lib/asciidoctor/pdf/converter.rb index 6ad7929ac..3b8136a91 100644 --- a/lib/asciidoctor/pdf/converter.rb +++ b/lib/asciidoctor/pdf/converter.rb @@ -525,13 +525,12 @@ def build_pdf_info doc end info[:Subject] = (sanitize doc.attr 'subject').as_pdf if doc.attr? 'subject' info[:Keywords] = (sanitize doc.attr 'keywords').as_pdf if doc.attr? 'keywords' - info[:Producer] = (sanitize doc.attr 'publisher').as_pdf if doc.attr? 'publisher' + info[:Creator] = (doc.attr? 'publisher') ? (sanitize doc.attr 'publisher').as_pdf : (info[:Author] || '') + info[:Producer] = (sanitize doc.attr 'producer').as_pdf if doc.attr? 'producer' if doc.attr? 'reproducible' - info[:Creator] = 'Asciidoctor PDF, based on Prawn'.as_pdf - info[:Producer] ||= (info[:Author] || info[:Creator]) + info[:Producer] ||= 'Asciidoctor PDF, based on Prawn'.as_pdf else - info[:Creator] = %(Asciidoctor PDF #{::Asciidoctor::PDF::VERSION}, based on Prawn #{::Prawn::VERSION}).as_pdf - info[:Producer] ||= (info[:Author] || info[:Creator]) + info[:Producer] ||= %(Asciidoctor PDF #{::Asciidoctor::PDF::VERSION}, based on Prawn #{::Prawn::VERSION}).as_pdf # NOTE: since we don't track the creation date of the input file, we map the ModDate header to the last modified # date of the input document and the CreationDate header to the date the PDF was produced by the converter. info[:ModDate] = (::Time.parse doc.attr 'docdatetime') rescue (now ||= ::Time.now) diff --git a/spec/pdf_info_spec.rb b/spec/pdf_info_spec.rb index 44d85250a..c19104ef5 100644 --- a/spec/pdf_info_spec.rb +++ b/spec/pdf_info_spec.rb @@ -18,20 +18,42 @@ end context 'attribution' do - it 'should include Asciidoctor PDF and Prawn versions in Creator field' do + it 'should set the Creator field to empty by default' do creator = (to_pdf 'hello').info[:Creator] - (expect creator).not_to be_nil - (expect creator).to include %(Asciidoctor PDF #{Asciidoctor::PDF::VERSION}) - (expect creator).to include %(Prawn #{Prawn::VERSION}) + (expect creator).to be_empty end - it 'should set Producer field to value of Creator field by default' do - pdf = to_pdf 'hello' - (expect pdf.info[:Producer]).not_to be_nil - (expect pdf.info[:Producer]).to eql pdf.info[:Creator] + it 'should set the Producer field to Asciidoctor PDF and Prawn by default' do + producer = (to_pdf 'hello').info[:Producer] + (expect producer).not_to be_nil + (expect producer).to include %(Asciidoctor PDF #{Asciidoctor::PDF::VERSION}) + (expect producer).to include %(Prawn #{Prawn::VERSION}) end - it 'should set Author and Producer field to value of author attribute if set' do + it 'should allow Producer field to be overridden using producer attribute' do + pdf = to_pdf <<~'EOS' + = Document Title + :producer: ACME + + hello + EOS + producer = pdf.info[:Producer] + (expect producer).not_to be_nil + (expect producer).to eql 'ACME' + end + + it 'should set the Creator field to the author if author is specified' do + pdf = to_pdf <<~'EOS' + = Document Title + Author Name + + hello + EOS + creator = pdf.info[:Creator] + (expect creator).to eql 'Author Name' + end + + it 'should set Author and Creator field to value of author attribute if set' do ['Author Name', ':author: Author Name'].each do |author_line| pdf = to_pdf <<~EOS = Document Title @@ -39,12 +61,12 @@ content EOS - (expect pdf.info[:Producer]).to eql pdf.info[:Author] (expect pdf.info[:Author]).to eql 'Author Name' + (expect pdf.info[:Creator]).to eql pdf.info[:Author] end end - it 'should set Author and Producer field to value of author attribute if set to multiple authors' do + it 'should set Author and Creator field to value of author attribute if set to multiple authors' do ['Author Name; Assistant Name', ':authors: Author Name; Assistant Name'].each do |author_line| pdf = to_pdf <<~EOS = Document Title @@ -55,14 +77,14 @@ Second Author: {author_2} EOS lines = ((pdf.page 1).text.split ?\n).map(&:strip) - (expect pdf.info[:Producer]).to eql pdf.info[:Author] (expect pdf.info[:Author]).to eql 'Author Name, Assistant Name' + (expect pdf.info[:Creator]).to eql pdf.info[:Author] (expect lines).to include 'First Author: Author Name' (expect lines).to include 'Second Author: Assistant Name' end end - it 'should set Author and Producer field using authors attribute with non-Latin characters' do + it 'should set Author and Creator field using authors attribute with non-Latin characters' do ['Doc Writer; Antonín Dvořák', ':authors: Doc Writer; Antonín Dvořák'].each do |author_line| pdf = to_pdf <<~EOS = Document Title @@ -73,8 +95,8 @@ Second Author: {author_2} EOS lines = ((pdf.page 1).text.split ?\n).map(&:strip) - (expect pdf.info[:Producer]).to eql pdf.info[:Author] (expect pdf.info[:Author]).to eql 'Doc Writer, Antonín Dvořák' + (expect pdf.info[:Creator]).to eql pdf.info[:Author] (expect lines).to include 'First Author: Doc Writer' (expect lines).to include 'Second Author: Antonín Dvořák' end @@ -132,7 +154,7 @@ (expect pdf.info[:Author]).to eql 'Author Name' end - it 'should set Producer field to value of publisher attribute if set' do + it 'should set Creator field to value of publisher attribute if set' do pdf = to_pdf <<~'EOS' = Document Title Author Name @@ -141,7 +163,7 @@ content EOS (expect pdf.info[:Author]).to eql 'Author Name' - (expect pdf.info[:Producer]).to eql 'Big Cheese' + (expect pdf.info[:Creator]).to eql 'Big Cheese' end it 'should set Subject field to value of subject attribute if set' do @@ -179,7 +201,7 @@ (expect pdf_info[:Author]).to eql 'D_J Allen' (expect pdf_info[:Subject]).to eql 'Science & Math' (expect pdf_info[:Keywords]).to eql 'mass–energy equivalence' - (expect pdf_info[:Producer]).to eql 'Schrödinger’s Cat' + (expect pdf_info[:Creator]).to eql 'Schrödinger’s Cat' end it 'should parse date attributes as local date objects' do @@ -239,7 +261,7 @@ content EOS - (expect pdf.info[:Creator]).to eql 'Asciidoctor PDF, based on Prawn' + (expect pdf.info[:Producer]).to eql 'Asciidoctor PDF, based on Prawn' end it 'should set mod and creation dates to match SOURCE_DATE_EPOCH environment variable' do