From 75ca02da51c796ee83115692fe53e53787de17ec Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Sat, 21 May 2022 12:39:16 -0600 Subject: [PATCH] resolves #2198 break and wrap long contiguous text in source block when linenums are enabled (PR #2199) --- CHANGELOG.adoc | 1 + .../pdf/formatted_text/source_wrap.rb | 10 ++++++-- spec/source_spec.rb | 25 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index cd693bdad..46b7cf744 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -11,6 +11,7 @@ Bug Fixes:: * scale inline image to fit within available height of page, accounting for the top padding of the line and the bottom gutter (#2193) * short-circuit formatted_text routine and log error if fragments in first line cannot fit on a new page +* break and wrap long contiguous text in source block when linenums are enabled (#2198) == 2.0.0 (2022-05-18) - @mojavelinux diff --git a/lib/asciidoctor/pdf/formatted_text/source_wrap.rb b/lib/asciidoctor/pdf/formatted_text/source_wrap.rb index c73b26007..0b190ee85 100644 --- a/lib/asciidoctor/pdf/formatted_text/source_wrap.rb +++ b/lib/asciidoctor/pdf/formatted_text/source_wrap.rb @@ -11,6 +11,7 @@ module SourceWrap def wrap array return super unless array[0][:linenum] # sanity check initialize_wrap array + @line_wrap.extend SourceLineWrap highlight_line = stop = nil unconsumed = @arranger.unconsumed until stop @@ -18,8 +19,7 @@ def wrap array linenum_text = first_fragment[:text] linenum_spacer ||= { text: (NoBreakSpace.encode linenum_text.encoding) + (' ' * (linenum_text.length - 1)) } highlight_line = (second_fragment = unconsumed[1])[:highlight] ? second_fragment.dup : nil - else - # NOTE: a wrapped line + else # wrapped line first_fragment[:text] = first_fragment[:text].lstrip @arranger.unconsumed.unshift highlight_line if highlight_line @arranger.unconsumed.unshift linenum_spacer.dup @@ -43,6 +43,12 @@ def wrap array @arranger.unconsumed end end + + module SourceLineWrap + def update_line_status_based_on_last_output + @arranger.current_format_state[:linenum] ? nil : super + end + end end end end diff --git a/spec/source_spec.rb b/spec/source_spec.rb index 307bed538..51d0e80f1 100644 --- a/spec/source_spec.rb +++ b/spec/source_spec.rb @@ -905,6 +905,31 @@ class Type; end (expect (pdf.find_unique_text %r/^\s*70\s*$/)[:page_number]).to be 2 (expect formatted_text_box_extensions_count).to be 0 end + + it 'should break and wrap numbered line if text does not fit on a single line' do + input = <<~EOS + :source-highlighter: rouge + + [%linenums,text] + ---- + y#{'o' * 100} + ---- + EOS + + pdf = to_pdf input, analyze: true + (expect pdf.pages).to have_size 1 + linenum_text = pdf.find_unique_text '1 ' + (expect linenum_text).not_to be_nil + first_line_text = pdf.find_unique_text %r/^yo/ + wrapped_text = pdf.find_unique_text %r/^ooo/ + (expect linenum_text[:x]).to be < first_line_text[:x] + (expect linenum_text[:y]).to eql first_line_text[:y] + (expect linenum_text[:x]).to be < wrapped_text[:x] + (expect wrapped_text[:x]).to eql first_line_text[:x] + (expect linenum_text[:y]).to be > wrapped_text[:y] + lines = (to_pdf input, pdf_theme: { code_border_radius: 0, code_border_width: [1, 0] }, analyze: :line).lines + (expect (lines[0][:from][:y] - lines[1][:from][:y]).abs).to (be_within 2).of 50 + end end context 'CodeRay' do