Skip to content

Commit

Permalink
resolves #2510 always use /Producer field in document info to credit …
Browse files Browse the repository at this point in the history
…Asciidoctor PDF and Prawn
  • Loading branch information
mojavelinux committed May 31, 2024
1 parent 4d5fca4 commit 46f1084
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Improvements::
* allow relative font size for sub and sup to be set independently; support combined setting for backwards compatibility
* don't hyphenate autolink when hyphenation is enabled (#2521) (*@meonkeys*)
* add support for base64-encoded SVG image reference in SVG (#2512)
* 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)

Bug Fixes::

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 @@ -531,13 +531,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 <<~'END'
= Document Title
:producer: ACME
hello
END
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 <<~'END'
= Document Title
Author Name
hello
END
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 <<~END
= Document Title
#{author_line}
content
END
(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 <<~END
= Document Title
Expand All @@ -55,14 +77,14 @@
Second Author: {author_2}
END
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 <<~END
= Document Title
Expand All @@ -73,8 +95,8 @@
Second Author: {author_2}
END
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 <<~'END'
= Document Title
Author Name
Expand All @@ -141,7 +163,7 @@
content
END
(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
END

(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 46f1084

Please sign in to comment.