diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2021-10-22 17:50:51 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-22 17:50:51 -0600 |
| commit | ff247f8009b81baf4a5c26667c19587e499cb66c (patch) | |
| tree | 417fd828b0de420325356ca9750b1aa977924ffb | |
| parent | fbf1c236940edc9fa5e610afd8d5d1157dde14ca (diff) | |
resolve #4192 honor :header_only option when parsing document with manpage doctype (PR #4193)
| -rw-r--r-- | CHANGELOG.adoc | 1 | ||||
| -rw-r--r-- | lib/asciidoctor/parser.rb | 12 | ||||
| -rw-r--r-- | test/document_test.rb | 41 |
3 files changed, 49 insertions, 5 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 97ebde56..eac3b3e3 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -41,6 +41,7 @@ Bug Fixes:: * Include primary video in value of `playlist` attribute when embeddding YouTube video (#4156) * Honor stripes=none on nested table (#4165) * Update default stylesheet to fix spacing around empty list item (#4184) + * Honor `:header_only` option when parsing document with manpage doctype (#4192) Improvements:: diff --git a/lib/asciidoctor/parser.rb b/lib/asciidoctor/parser.rb index 628b79dc..2cb5bc5b 100644 --- a/lib/asciidoctor/parser.rb +++ b/lib/asciidoctor/parser.rb @@ -90,10 +90,10 @@ class Parser # # returns the Document object def self.parse reader, document, options = {} - block_attributes = parse_document_header reader, document + block_attributes = parse_document_header reader, document, (header_only = options[:header_only]) # NOTE don't use a postfix conditional here as it's known to confuse JRuby in certain circumstances - unless options[:header_only] + unless header_only while reader.has_more_lines? new_section, block_attributes = next_section reader, document, block_attributes if new_section @@ -118,7 +118,7 @@ class Parser # which are automatically removed by the reader. # # returns the Hash of orphan block attributes captured above the header - def self.parse_document_header reader, document + def self.parse_document_header reader, document, header_only = false # capture lines of block-level metadata and plow away comment lines that precede first block block_attrs = reader.skip_blank_lines ? (parse_block_metadata_lines reader, document) : {} doc_attrs = document.attributes @@ -183,7 +183,7 @@ class Parser end # parse title and consume name section of manpage document - parse_manpage_header reader, document, block_attrs if document.doctype == 'manpage' + parse_manpage_header reader, document, block_attrs, header_only if document.doctype == 'manpage' # NOTE block_attrs are the block-level attributes (not document attributes) that # precede the first line of content (document title, first section or first block) @@ -193,7 +193,7 @@ class Parser # Public: Parses the manpage header of the AsciiDoc source read from the Reader # # returns Nothing - def self.parse_manpage_header reader, document, block_attributes + def self.parse_manpage_header reader, document, block_attributes, header_only = false if ManpageTitleVolnumRx =~ (doc_attrs = document.attributes)['doctitle'] doc_attrs['manvolnum'] = manvolnum = $2 doc_attrs['mantitle'] = (((mantitle = $1).include? ATTR_REF_HEAD) ? (document.sub_attributes mantitle) : mantitle).downcase @@ -210,6 +210,8 @@ class Parser doc_attrs['docname'] = manname doc_attrs['outfilesuffix'] = %(.#{manvolnum}) end + elsif header_only + # done else reader.skip_blank_lines reader.save diff --git a/test/document_test.rb b/test/document_test.rb index 0487ac39..9d3a66ca 100644 --- a/test/document_test.rb +++ b/test/document_test.rb @@ -1315,6 +1315,47 @@ context 'Document' do assert_equal 0, doc.blocks.size end + test 'should parse header only when docytpe is manpage' do + input = <<~'EOS' + = cmd(1) + Author Name + :doctype: manpage + + == Name + + cmd - does stuff + EOS + + doc = document_from_string input, parse_header_only: true + assert_equal 'cmd(1)', doc.doctitle + assert_equal 'Author Name', doc.author + assert_equal 'cmd', doc.attributes['mantitle'] + assert_equal '1', doc.attributes['manvolnum'] + assert_nil doc.attributes['manname'] + assert_nil doc.attributes['manpurpose'] + assert_equal 0, doc.blocks.size + end + + test 'should not warn when parsing header only when docytpe is manpage and body is empty' do + input = <<~'EOS' + = cmd(1) + Author Name + :doctype: manpage + EOS + + using_memory_logger do |logger| + doc = document_from_string input, parse_header_only: true + assert_empty logger.messages + assert_equal 'cmd(1)', doc.doctitle + assert_equal 'Author Name', doc.author + assert_equal 'cmd', doc.attributes['mantitle'] + assert_equal '1', doc.attributes['manvolnum'] + assert_nil doc.attributes['manname'] + assert_nil doc.attributes['manpurpose'] + assert_equal 0, doc.blocks.size + end + end + test 'outputs footnotes in footer' do input = <<~'EOS' A footnote footnote:[An example footnote.]; |
