Skip to content

Commit

Permalink
backport fix for #2510 always use /Producer field in document info to…
Browse files Browse the repository at this point in the history
… credit Asciidoctor PDF and Prawn
  • Loading branch information
mojavelinux committed Jun 1, 2024
1 parent c14851c commit f5908fa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 4 additions & 5 deletions lib/asciidoctor/pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
58 changes: 40 additions & 18 deletions spec/pdf_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,55 @@
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
#{author_line}
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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f5908fa

Please sign in to comment.