diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 6e568b781d..2d70c48a1a 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -18,6 +18,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[ Bug Fixes:: * Ensure mtime of input file honors TZ environment variable on JRuby for Windows (affects value of docdatetime attribute) (#3550) + * Suppress missing attribute warning when applying substitutions to implicit document title to assign to doctitle attribute (#4024) Improvements:: diff --git a/lib/asciidoctor/parser.rb b/lib/asciidoctor/parser.rb index 77272913c8..9a589dd32d 100644 --- a/lib/asciidoctor/parser.rb +++ b/lib/asciidoctor/parser.rb @@ -144,7 +144,9 @@ def self.parse_document_header(reader, document) l0_section_title = nil else document.title = l0_section_title - doc_attrs['doctitle'] = doctitle_attr_val = document.apply_header_subs l0_section_title + if (doc_attrs['doctitle'] = doctitle_attr_val = document.sub_specialchars l0_section_title).include? ATTR_REF_HEAD + doc_attrs['doctitle'] = doctitle_attr_val = document.sub_attributes doctitle_attr_val, attribute_missing: 'skip' + end end document.header.source_location = source_location if source_location # default to compat-mode if document has setext doctitle diff --git a/test/document_test.rb b/test/document_test.rb index e2797eaaec..960f77488f 100644 --- a/test/document_test.rb +++ b/test/document_test.rb @@ -691,16 +691,48 @@ assert_xpath '//*[@id="preamble"]//p[text()="Override"]', doc.convert, 1 end - test 'header substitutions should be applied to the value of the doctitle attribute' do + test 'should apply header substitutions to value of the doctitle attribute assigned from implicit doctitle' do input = <<~'EOS' - = & + = {plus} The name of the game is {doctitle}. EOS doc = document_from_string input - assert_equal '<Foo> & <Bar>', (doc.attr 'doctitle') - assert_includes doc.blocks[0].content, '<Foo> & <Bar>' + assert_equal '<Foo> + <Bar>', (doc.attr 'doctitle') + assert_includes doc.blocks[0].content, '<Foo> + <Bar>' + end + + test 'should substitute attribute reference in implicit document title for attribute defined earlier in header' do + using_memory_logger do |logger| + input = <<~'EOS' + :project-name: ACME + = {project-name} Docs + + {doctitle} + EOS + doc = document_from_string input, attributes: { 'attribute-missing' => 'warn' } + assert_empty logger + assert_equal 'ACME Docs', (doc.attr 'doctitle') + assert_equal 'ACME Docs', doc.doctitle + assert_xpath '//p[text()="ACME Docs"]', doc.convert, 1 + end + end + + test 'should not warn if implicit document title contains attribute reference for attribute defined later in header' do + using_memory_logger do |logger| + input = <<~'EOS' + = {project-name} Docs + :project-name: ACME + + {doctitle} + EOS + doc = document_from_string input, attributes: { 'attribute-missing' => 'warn' } + assert_empty logger + assert_equal '{project-name} Docs', (doc.attr 'doctitle') + assert_equal 'ACME Docs', doc.doctitle + assert_xpath '//p[text()="{project-name} Docs"]', doc.convert, 1 + end end test 'should recognize document title when preceded by blank lines' do