diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2023-05-06 23:50:46 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-06 23:50:46 -0600 |
| commit | c9c8373b44e2d268566f988dc7a956cd1943680f (patch) | |
| tree | fbb679daa2edc26a8d23735d698641d70ffb243d | |
| parent | 867866575fb97947b037d47850914985686387f0 (diff) | |
resolves #4450 consistently produce partintro as a compound open block (PR #4451)
| -rw-r--r-- | CHANGELOG.adoc | 1 | ||||
| -rw-r--r-- | lib/asciidoctor/parser.rb | 19 | ||||
| -rw-r--r-- | test/sections_test.rb | 53 |
3 files changed, 64 insertions, 9 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 3268e598..c126bfd8 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -67,6 +67,7 @@ Improvements:: * Don't uppercase monospace span in section title in manpage output (#4402) * Simplify processing of implicit link (i.e., autolink) by separating implicit and explicit match * Add single and double role hint to `<quote>` tag in DocBook output (#2947) + * Generate partintro block consistently (#4450) Bug Fixes:: diff --git a/lib/asciidoctor/parser.rb b/lib/asciidoctor/parser.rb index e62a2324..2b50aa46 100644 --- a/lib/asciidoctor/parser.rb +++ b/lib/asciidoctor/parser.rb @@ -399,18 +399,22 @@ class Parser # REVIEW this may be doing too much if part if !section.blocks? - # if this block wasn't marked as [partintro], emulate behavior as if it had + # if this not a [partintro] open block, enclose it in a [partintro] open block if new_block.style != 'partintro' - # emulate [partintro] paragraph - if new_block.context == :paragraph - new_block.context = :open + # if this is already a normal open block, simply add the partintro style + if new_block.style == 'open' && new_block.context == :open new_block.style = 'partintro' - # emulate [partintro] open block else new_block.parent = (intro = Block.new section, :open, content_model: :compound) intro.style = 'partintro' section.blocks << intro end + # if this is a [partintro] paragraph, convert it to a [partintro] open block w/ single paragraph + elsif new_block.content_model == :simple + new_block.content_model = :compound + new_block << (Block.new new_block, :paragraph, source: new_block.lines, subs: new_block.subs) + new_block.lines.clear + new_block.subs.clear end elsif section.blocks.size == 1 first_block = section.blocks[0] @@ -420,12 +424,11 @@ class Parser # rebuild [partintro] paragraph as an open block elsif first_block.content_model != :compound new_block.parent = (intro = Block.new section, :open, content_model: :compound) - intro.style = 'partintro' - section.blocks.shift - if first_block.style == 'partintro' + if first_block.style == (intro.style = 'partintro') first_block.context = :paragraph first_block.style = nil end + section.blocks.shift intro << first_block section.blocks << intro end diff --git a/test/sections_test.rb b/test/sections_test.rb index 6e076d93..83863424 100644 --- a/test/sections_test.rb +++ b/test/sections_test.rb @@ -3633,6 +3633,8 @@ context 'Sections' do assert_xpath '//h1[@id="_chapter_one"][text() = "Chapter One"]', output, 1 assert_xpath '//h1[@id="_chapter_two"][text() = "Chapter Two"]', output, 1 assert_xpath '//h1[@id="_chapter_three"][text() = "Chapter Three"]', output, 1 + assert_css '#_chapter_one + .openblock.partintro p', output, 1 + assert_css '#_chapter_two + .openblock.partintro p', output, 1 end test 'should print error if level 0 section comes after nested section and doctype is not book' do @@ -3741,7 +3743,7 @@ context 'Sections' do = Part 1 - part intro + part intro--a summary == Chapter 1 EOS @@ -3749,7 +3751,53 @@ context 'Sections' do doc = document_from_string input partintro = doc.blocks.first.blocks.first assert_equal :open, partintro.context + assert_equal :compound, partintro.content_model + assert_empty partintro.lines + assert_empty partintro.subs assert_equal 'partintro', partintro.style + assert_equal :paragraph, partintro.blocks[0].context + assert_equal ['part intro--a summary'], partintro.blocks[0].lines + assert_include 'part intro—​a summary', partintro.convert + end + + test 'should preserve title on partintro defined as partintro paragraph' do + input = <<~'EOS' + = Book + :doctype: book + + = Part 1 + + .Intro + [partintro] + Read this first. + + == Chapter 1 + EOS + + doc = document_from_string input + partintro = doc.blocks.first.blocks.first + assert_equal :open, partintro.context + assert_equal 'Intro', partintro.title + end + + test 'should not promote title on partintro defined as normal paragraph' do + input = <<~'EOS' + = Book + :doctype: book + + = Part 1 + + .Intro + Read this first. + + == Chapter 1 + EOS + + doc = document_from_string input + partintro = doc.blocks.first.blocks.first + assert_equal :open, partintro.context + assert_nil partintro.title + assert_equal 'Intro', partintro.blocks[0].title end test 'should add partintro style to child open block of part' do @@ -3769,7 +3817,9 @@ context 'Sections' do doc = document_from_string input partintro = doc.blocks.first.blocks.first assert_equal :open, partintro.context + assert_equal :compound, partintro.content_model assert_equal 'partintro', partintro.style + assert_equal :paragraph, partintro.blocks[0].context end test 'should wrap child paragraphs of part in partintro open block' do @@ -3789,6 +3839,7 @@ context 'Sections' do doc = document_from_string input partintro = doc.blocks.first.blocks.first assert_equal :open, partintro.context + assert_equal :compound, partintro.content_model assert_equal 'partintro', partintro.style assert_equal 2, partintro.blocks.size assert_equal :paragraph, partintro.blocks[0].context |
