Skip to content

Commit

Permalink
resolves #2198 break and wrap long contiguous text in source block wh…
Browse files Browse the repository at this point in the history
…en linenums are enabled (PR #2199)
  • Loading branch information
mojavelinux authored May 21, 2022
1 parent 6d287bf commit 75ca02d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions lib/asciidoctor/pdf/formatted_text/source_wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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
if (first_fragment = unconsumed[0])[:linenum]
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
Expand All @@ -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
25 changes: 25 additions & 0 deletions spec/source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 75ca02d

Please sign in to comment.