Skip to content

Commit

Permalink
resolves asciidoctor#3602 move abstract inside info tag in DocBook ou…
Browse files Browse the repository at this point in the history
…tput
  • Loading branch information
mojavelinux committed Feb 19, 2024
1 parent 38f464f commit 6cee5c1
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Bug Fixes::
* Preserve paragraph breaks in normal table cell in manpage output (#4481)
* Style cells in head row as bold in manpage output (#4490)
* Escape spaces in include target (using inline passthrough) when generating link from include directive (#4461)
* Move abstract inside info tag in DocBook output (#3602)
* Honor secondary and tertiary terms on `indexterm` macro when primary term is quoted and contains an equals sign (#3652)
* Remove extra border below doctitle when sidebar toc is collapsed into main content area (#4523)

Expand Down
52 changes: 46 additions & 6 deletions lib/asciidoctor/converter/docbook5.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def convert_document node
end
root_tag_idx = result.size
id = node.id
result << (document_info_tag node) unless node.noheader
abstract = find_root_abstract node
result << (document_info_tag node, abstract) unless node.noheader
if manpage
result << '<refentry>'
result << '<refmeta>'
Expand All @@ -62,7 +63,9 @@ def convert_document node
unless (docinfo_content = node.docinfo :header).empty?
result << docinfo_content
end
result << node.content if node.blocks?
abstract = extract_abstract node, abstract if abstract
result << (node.blocks.map {|block| block.convert }.compact.join LF) if node.blocks?
restore_abstract abstract if abstract
unless (docinfo_content = node.docinfo :footer).empty?
result << docinfo_content
end
Expand All @@ -74,7 +77,14 @@ def convert_document node
result.join LF
end

alias convert_embedded content_only
def convert_embedded node
if @backend == 'docbook5' && (abstract = find_root_abstract node)
abstract = extract_abstract node, abstract
end
result = node.blocks.map {|block| block.convert }.compact.join LF
restore_abstract abstract if abstract
result
end

def convert_section node
if node.document.doctype == 'manpage'
Expand Down Expand Up @@ -310,13 +320,17 @@ def convert_olist node
def convert_open node
case node.style
when 'abstract'
if node.parent == node.document && node.document.doctype == 'book'
if (parent = node.parent) == node.document && node.document.doctype == 'book'
logger.warn 'abstract block cannot be used in a document without a title when doctype is book. Excluding block content.'
''
else
%(<abstract>
result = %(<abstract>
#{title_tag node}#{enclose_content node}
</abstract>)
if @backend == 'docbook5' && !(node.option? 'root') && (parent.context == :open ? parent.style == 'partintro' : parent.context == :section && parent.sectname == 'partintro') && node == parent.blocks[0]
result = %(<info>\n#{result}\n</info>)
end
result
end
when 'partintro'
if node.level == 0 && node.parent.context == :section && node.document.doctype == 'book'
Expand Down Expand Up @@ -663,7 +677,7 @@ def author_tag doc, author
result.join LF
end

def document_info_tag doc
def document_info_tag doc, abstract
result = ['<info>']
unless doc.notitle
if (title = doc.doctitle partition: true, use_fallback: true).subtitle?
Expand Down Expand Up @@ -717,11 +731,37 @@ def document_info_tag doc
result << docinfo_content
end
end
if abstract
abstract.set_option 'root'
result << (convert abstract, abstract.node_name)
abstract.remove_attr 'root-option'
end
result << '</info>'

result.join LF
end

def find_root_abstract doc
return unless doc.blocks?
if (first_block = doc.blocks[0]).context == :preamble
return unless (first_block = first_block.blocks[0])
elsif first_block.context == :section
return first_block if first_block.sectname == 'abstract'
return unless first_block.sectname == 'preface' && (first_block = first_block.blocks[0])
end
return first_block if first_block.style == 'abstract' && first_block.context == :open
end

def extract_abstract document, abstract
parent = abstract.parent
parent = parent.parent while parent != document && parent.blocks.length == 1
parent.blocks.delete_at 0
end

def restore_abstract abstract
abstract.parent.blocks.insert 0, abstract
end

def get_root_document node
while (node = node.document).nested?
node = node.parent_document
Expand Down
13 changes: 7 additions & 6 deletions test/blocks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3701,8 +3701,8 @@ def names
EOS

output = convert_string input, backend: 'docbook'
assert_css 'abstract', output, 1
assert_css 'abstract > simpara', output, 2
assert_css 'info > abstract', output, 1
assert_css 'info > abstract > simpara', output, 2
end

test 'should make abstract on open block with title converted to DocBook' do
Expand All @@ -3717,9 +3717,9 @@ def names
EOS

output = convert_string input, backend: 'docbook'
assert_css 'abstract', output, 1
assert_css 'abstract > title', output, 1
assert_css 'abstract > title + simpara', output, 1
assert_css 'info > abstract', output, 1
assert_css 'info > abstract > title', output, 1
assert_css 'info > abstract > title + simpara', output, 1
end

test 'should allow abstract in document with title if doctype is book converted to DocBook' do
Expand All @@ -3732,7 +3732,8 @@ def names
EOS

output = convert_string input, backend: 'docbook'
assert_css 'abstract', output, 1
assert_css 'info > abstract', output, 1
assert_css 'preface', output, 0
end

test 'should not allow abstract as direct child of document if doctype is book converted to DocBook' do
Expand Down
54 changes: 54 additions & 0 deletions test/preamble_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,58 @@
output = convert_string input
assert_xpath '//*[@id="preamble"]/*[@id="toc"]', output, 1
end

test 'should move abstract in preface to info tag when converting to DocBook' do
input = <<~'EOS'
= Document Title
[abstract]
This is the abstract.
== Fin
EOS

%w(article book).each do |doctype|
output = convert_string input, backend: 'docbook', doctype: doctype
assert_xpath '//abstract', output, 1
assert_xpath %(/#{doctype}/info/abstract), output, 1
end
end

test 'should move abstract as first section to info tag when converting to DocBook' do
input = <<~'EOS'
= Document Title
[abstract]
== Abstract
This is the abstract.
== Fin
EOS

output = convert_string input, backend: 'docbook'
assert_xpath '//abstract', output, 1
assert_xpath '/article/info/abstract', output, 1
end

test 'should move abstract in preface to info tag when converting to DocBook' do
input = <<~'EOS'
= Document Title
:doctype: book
[preface]
== Preface
[abstract]
This is the abstract.
== Fin
EOS

output = convert_string input, backend: 'docbook'
assert_xpath '//abstract', output, 1
assert_xpath '/book/info/abstract', output, 1
assert_xpath '//preface', output, 0
end
end
50 changes: 47 additions & 3 deletions test/sections_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2759,9 +2759,9 @@
Abstract content
EOS

output = convert_string_to_embedded input, backend: 'docbook'
assert_xpath '/abstract[@xml:id="abstract_title"]', output, 1
assert_xpath '/abstract[@xml:id="abstract_title"]/title[text()="Abstract Title"]', output, 1
output = convert_string input, backend: 'docbook'
assert_xpath '/article/info/abstract[@xml:id="abstract_title"]', output, 1
assert_xpath '/article/info/abstract[@xml:id="abstract_title"]/title[text()="Abstract Title"]', output, 1
end

test 'should allow a special section to be nested at arbitrary depth in DocBook output' do
Expand Down Expand Up @@ -3846,6 +3846,50 @@
assert_equal :paragraph, partintro.blocks[1].context
end

test 'should wrap abstract in implicit part intro in info tag when converting to DocBook' do
input = <<~'EOS'
= Book
:doctype: book
= Part 1
[abstract]
Abstract of part.
more part intro
== Chapter 1
EOS

output = convert_string input, backend: 'docbook'
assert_xpath '//abstract', output, 1
assert_xpath '//partintro/info/abstract', output, 1
end

test 'should wrap abstract in part intro section in info tag when converting to DocBook' do
input = <<~'EOS'
= Book
:doctype: book
= Part 1
[partintro]
== Part Intro
[abstract]
Abstract of part.
more part intro
== Chapter 1
EOS

output = convert_string input, backend: 'docbook'
assert_xpath '//abstract', output, 1
assert_xpath '//partintro/info/abstract', output, 1
assert_xpath '//partintro/simpara', output, 1
end

test 'should warn if part has no sections' do
input = <<~'EOS'
= Book
Expand Down

0 comments on commit 6cee5c1

Please sign in to comment.