diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2021-11-12 23:46:40 -0700 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2021-11-13 03:12:08 -0700 |
| commit | 213907c543e4190ac814930549e1219df2369269 (patch) | |
| tree | 7520541fe0934c7b5fc01d12135461518ec0e6a4 | |
| parent | 50ce4a5b9177a7462294cdc1f4c2910e02b29115 (diff) | |
add flag to suppress retrieving metadata from parse_header_metadata; include metadata from author overrides
| -rw-r--r-- | lib/asciidoctor/parser.rb | 42 | ||||
| -rw-r--r-- | test/parser_test.rb | 14 |
2 files changed, 29 insertions, 27 deletions
diff --git a/lib/asciidoctor/parser.rb b/lib/asciidoctor/parser.rb index 62d4c787..48e7df2f 100644 --- a/lib/asciidoctor/parser.rb +++ b/lib/asciidoctor/parser.rb @@ -169,7 +169,7 @@ class Parser end block_attrs.clear (modified_attrs = document.instance_variable_get :@attributes_modified).delete 'doctitle' - parse_header_metadata reader, document + parse_header_metadata reader, document, nil if modified_attrs.include? 'doctitle' if (val = doc_attrs['doctitle']).nil_or_empty? || val == doctitle_attr_val doc_attrs['doctitle'] = doctitle_attr_val @@ -1770,13 +1770,11 @@ class Parser # parse_header_metadata(Reader.new data, nil, normalize: true) # # => { 'author' => 'Author Name', 'firstname' => 'Author', 'lastname' => 'Name', 'email' => 'author@example.org', # # 'revnumber' => '1.0', 'revdate' => '2012-12-21', 'revremark' => 'Coincide w/ end of world.' } - def self.parse_header_metadata reader, document = nil + def self.parse_header_metadata reader, document = nil, retrieve = true doc_attrs = document && document.attributes # NOTE this will discard any comment lines, but not skip blank lines process_attribute_entries reader, document - metadata = {} - if reader.has_more_lines? && !reader.next_line_empty? authorcount = (implicit_author_metadata = process_authors reader.read_line).delete 'authorcount' if document && (doc_attrs['authorcount'] = authorcount) > 0 @@ -1788,16 +1786,15 @@ class Parser implicit_authorinitials = doc_attrs['authorinitials'] implicit_authors = doc_attrs['authors'] end - (metadata = implicit_author_metadata)['authorcount'] = authorcount + implicit_author_metadata['authorcount'] = authorcount # NOTE this will discard any comment lines, but not skip blank lines process_attribute_entries reader, document - rev_metadata = {} - if reader.has_more_lines? && !reader.next_line_empty? rev_line = reader.read_line if (match = RevisionInfoLineRx.match rev_line) + rev_metadata = {} rev_metadata['revnumber'] = match[1].rstrip if match[1] unless (component = match[2].strip).empty? # version must begin with 'v' if date is absent @@ -1808,25 +1805,18 @@ class Parser end end rev_metadata['revremark'] = match[3].rstrip if match[3] + if document && !rev_metadata.empty? + # apply header subs and assign to document + rev_metadata.each do |key, val| + doc_attrs[key] = document.apply_header_subs val unless doc_attrs.key? key + end + end else # throw it back reader.unshift_line rev_line end end - unless rev_metadata.empty? - if document - # apply header subs and assign to document - rev_metadata.each do |key, val| - unless doc_attrs.key? key - doc_attrs[key] = document.apply_header_subs val - end - end - end - - metadata.update rev_metadata - end - # NOTE this will discard any comment lines, but not skip blank lines process_attribute_entries reader, document @@ -1871,12 +1861,16 @@ class Parser # process as names only author_metadata = process_authors authors, true, false else - author_metadata = {} + author_metadata = { 'authorcount' => 0 } end end - if author_metadata.empty? - metadata['authorcount'] ||= (doc_attrs['authorcount'] = 0) + if author_metadata['authorcount'] == 0 + if authorcount + author_metadata = nil + else + doc_attrs['authorcount'] = 0 + end else doc_attrs.update author_metadata @@ -1887,7 +1881,7 @@ class Parser end end - metadata + implicit_author_metadata.merge rev_metadata.to_h, author_metadata.to_h if retrieve end # Internal: Parse the author line into a Hash of author metadata diff --git a/test/parser_test.rb b/test/parser_test.rb index 95d3bebc..860ffaf3 100644 --- a/test/parser_test.rb +++ b/test/parser_test.rb @@ -395,13 +395,16 @@ context 'Parser' do test 'replace implicit authors if value of authors attribute does not match computed value' do input = <<~'EOS' Doc Writer; Junior Writer - :authors: Stuart Rackham; Dan Allen + :authors: Stuart Rackham; Dan Allen; Sarah White EOS doc = empty_document - parse_header_metadata input, doc - assert_equal 'Stuart Rackham, Dan Allen', doc.attributes['authors'] + metadata = parse_header_metadata input, doc + assert_equal metadata['authorcount'], 3 + assert_equal doc.attributes['authorcount'], 3 + assert_equal 'Stuart Rackham, Dan Allen, Sarah White', doc.attributes['authors'] assert_equal 'Stuart Rackham', doc.attributes['author_1'] assert_equal 'Dan Allen', doc.attributes['author_2'] + assert_equal 'Sarah White', doc.attributes['author_3'] end test 'sets authorcount to 0 if document has no authors' do @@ -412,6 +415,11 @@ context 'Parser' do assert_equal 0, metadata['authorcount'] end + test 'returns empty hash if document has no authors and invoked without document' do + metadata = parse_header_metadata '' + assert_empty metadata + end + test 'does not drop name joiner when using multiple authors' do input = 'Kismet Chameleon; Lazarus het_Draeke' doc = empty_document |
