diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2022-05-29 16:24:32 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-29 16:24:32 -0600 |
| commit | 944ed959957c74dcaf4387f919ebc5fbed4f4c66 (patch) | |
| tree | 7bd39b2035e04b01561ca05ff6e09f83e2752712 | |
| parent | 87ed094d9bde86861fb82b6146db95dba0c99d15 (diff) | |
resolves #2219 fix layout of collapsible block (PR #2220)
| -rw-r--r-- | CHANGELOG.adoc | 1 | ||||
| -rw-r--r-- | lib/asciidoctor/pdf/converter.rb | 26 | ||||
| -rw-r--r-- | spec/example_spec.rb | 69 |
3 files changed, 88 insertions, 8 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 83081150..d6771a67 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -9,6 +9,7 @@ For a detailed view of what has changed, refer to the {url-repo}/commits/main[co Bug Fixes:: +* indent content of collapsible block and apply bottom margin to match style of HTML output (#2219) * patch prawn-gmagick to reread bit depth of a PNG image if it extracts the wrong value (#2216) * interpret width of SVG correctly when width is defined in file using px units (#2215) * don't crash if inline role defines border width but not border color diff --git a/lib/asciidoctor/pdf/converter.rb b/lib/asciidoctor/pdf/converter.rb index c7dcce77..a5a33b79 100644 --- a/lib/asciidoctor/pdf/converter.rb +++ b/lib/asciidoctor/pdf/converter.rb @@ -1164,8 +1164,26 @@ module Asciidoctor alias convert_literal convert_code alias convert_listing_or_literal convert_code + def convert_collapsible node + id = node.id + title = (collapsible_marker = %(\u25bc )) + (node.title? ? node.title : 'Details') + indent_by = theme_font(:caption) { rendered_width_of_string collapsible_marker } + if !at_page_top? && (id || (node.option? 'unbreakable')) + arrange_block node do + add_dest_for_block node if id + tare_first_page_content_stream { ink_caption title } + indent(indent_by) { traverse node } + end + else + add_dest_for_block node if id + tare_first_page_content_stream { ink_caption title } + indent(indent_by) { traverse node } + end + theme_margin :block, :bottom, (next_enclosed_block node) + end + def convert_example node - return convert_open node if node.option? 'collapsible' + return convert_collapsible node if node.option? 'collapsible' caption_bottom = @theme.example_caption_end&.to_sym == :bottom arrange_block node do |extent| add_dest_for_block node if node.id @@ -1190,14 +1208,12 @@ module Asciidoctor if !at_page_top? && (has_title || id || (node.option? 'unbreakable')) arrange_block node do add_dest_for_block node if id - tare_first_page_content_stream do - node.context == :example ? (ink_caption %(\u25bc #{node.title})) : (ink_caption node, labeled: false) - end if has_title + tare_first_page_content_stream { ink_caption node, labeled: false } if has_title traverse node end else add_dest_for_block node if id - node.context == :example ? (ink_caption %(\u25bc #{node.title})) : (ink_caption node, labeled: false) if has_title + ink_caption node, labeled: false if has_title traverse node end end diff --git a/spec/example_spec.rb b/spec/example_spec.rb index 4060f655..6379ae3f 100644 --- a/spec/example_spec.rb +++ b/spec/example_spec.rb @@ -340,21 +340,84 @@ describe 'Asciidoctor::PDF::Converter - Example' do (expect (text_left - left).to_f).to eql 12.0 end - it 'should use informal title and no border or shading if collapsible option is set' do + it 'should use informal title, indented content, no border or shading, and bottom margin if collapsible option is set' do input = <<~'EOS' .Reveal Answer [%collapsible] ==== This is a PDF, so the answer is always visible. ==== + + Paragraph following collapsible block. EOS pdf = to_pdf input, analyze: true lines = pdf.lines - (expect lines).to eql [%(\u25bc Reveal Answer), 'This is a PDF, so the answer is always visible.'] - (expect pdf.text[0][:x]).to eql pdf.text[1][:x] + expected_lines = [ + %(\u25bc Reveal Answer), + 'This is a PDF, so the answer is always visible.', + 'Paragraph following collapsible block.', + ] + (expect lines).to eql expected_lines + (expect pdf.text[0][:x]).to eql pdf.text[2][:x] + (expect pdf.text[1][:x]).to be > pdf.text[2][:x] + (expect pdf.text[1][:y] - (pdf.text[2][:y] + pdf.text[2][:font_size])).to be > 12 pdf = to_pdf input, analyze: :line (expect pdf.lines).to be_empty end + + # see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/summary#default_label_text + it 'should use fallback title for collapsible block if no title is specified' do + input = <<~'EOS' + [%collapsible] + ==== + These are the details. + ==== + EOS + + pdf = to_pdf input, analyze: true + (expect pdf.text[0][:string]).to eql %(\u25bc Details) + end + + it 'should align left margin of content of collapsible block with start of title text' do + input = <<~'EOS' + .*Spoiler* + [%collapsible] + ==== + Now you can't unsee it. + Muahahahaha. + ==== + EOS + + pdf = to_pdf input, analyze: true + (expect pdf.text[0][:x]).to eql 48.24 + (expect pdf.text[1][:x]).to be > 48.24 + (expect pdf.text[2][:x]).to be > 48.24 + (expect pdf.text[1][:x]).to eql pdf.text[2][:x] + end + + it 'should insert block margin between bottom of content and next block' do + pdf_theme = { + code_background_color: 'transparent', + code_border_radius: 0, + code_border_width: [1, 0], + } + input = <<~'EOS' + [%collapsible] + ==== + ---- + inside + ---- + ==== + + ---- + below + ---- + EOS + + lines = (to_pdf input, pdf_theme: pdf_theme, analyze: :line).lines.sort_by {|it| -it[:from][:y] } + (expect lines).to have_size 4 + (expect lines[1][:from][:y] - lines[2][:from][:y]).to eql 12.0 + end end |
