summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2024-02-19 15:53:14 -0700
committerGitHub <noreply@github.com>2024-02-19 15:53:14 -0700
commit2683f1f096c8ca86e3d3ec6d31777073024c95f3 (patch)
tree408b31bc5fcf0f5f6409ce28f9b60f6c61660d87
parentf66e2ce882631452f3fcd93cc95130db2a380439 (diff)
resolves #3602 move abstract inside info tag in DocBook output (PR #4508)
-rw-r--r--CHANGELOG.adoc1
-rw-r--r--lib/asciidoctor/converter/docbook5.rb53
-rw-r--r--test/blocks_test.rb13
-rw-r--r--test/preamble_test.rb54
-rw-r--r--test/sections_test.rb50
5 files changed, 156 insertions, 15 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index f58d472c..25ee1621 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -76,6 +76,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)
diff --git a/lib/asciidoctor/converter/docbook5.rb b/lib/asciidoctor/converter/docbook5.rb
index 70dec1f7..4bfda1db 100644
--- a/lib/asciidoctor/converter/docbook5.rb
+++ b/lib/asciidoctor/converter/docbook5.rb
@@ -45,7 +45,8 @@ class Converter::DocBook5Converter < Converter::Base
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>'
@@ -62,7 +63,9 @@ class Converter::DocBook5Converter < Converter::Base
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
@@ -74,7 +77,15 @@ class Converter::DocBook5Converter < Converter::Base
result.join LF
end
- alias convert_embedded content_only
+ def convert_embedded node
+ # NOTE in DocBook 5, the root abstract must be in the info tag and is thus not part of the body
+ 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'
@@ -310,13 +321,17 @@ class Converter::DocBook5Converter < Converter::Base
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'
@@ -663,7 +678,7 @@ class Converter::DocBook5Converter < Converter::Base
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?
@@ -717,11 +732,37 @@ class Converter::DocBook5Converter < Converter::Base
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
diff --git a/test/blocks_test.rb b/test/blocks_test.rb
index 78e9ed0c..c5c15002 100644
--- a/test/blocks_test.rb
+++ b/test/blocks_test.rb
@@ -3701,8 +3701,8 @@ context 'Blocks' do
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
@@ -3717,9 +3717,9 @@ context 'Blocks' do
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
@@ -3732,7 +3732,8 @@ context 'Blocks' do
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
diff --git a/test/preamble_test.rb b/test/preamble_test.rb
index 040ebb0f..9fdef546 100644
--- a/test/preamble_test.rb
+++ b/test/preamble_test.rb
@@ -167,4 +167,58 @@ context 'Preamble' do
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
diff --git a/test/sections_test.rb b/test/sections_test.rb
index 83863424..4193c37b 100644
--- a/test/sections_test.rb
+++ b/test/sections_test.rb
@@ -2759,9 +2759,9 @@ context 'Sections' do
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
@@ -3846,6 +3846,50 @@ context 'Sections' do
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