Skip to content

Commit

Permalink
follow-up to #2198 hide linenum spacer in source block from wrap by c…
Browse files Browse the repository at this point in the history
…har logic
  • Loading branch information
mojavelinux committed May 21, 2022
1 parent 75ca02d commit f2e6483
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/asciidoctor/pdf/formatted_text/source_wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def wrap array
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)) }
linenum_spacer ||= { text: (NoBreakSpace.encode linenum_text.encoding) + (' ' * (linenum_text.length - 1)), linenum: :spacer }
highlight_line = (second_fragment = unconsumed[1])[:highlight] ? second_fragment.dup : nil
else # wrapped line
first_fragment[:text] = first_fragment[:text].lstrip
Expand Down
51 changes: 51 additions & 0 deletions spec/source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,57 @@ class Type; end
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

it 'should break and wrap numbered line if indented text does not fit on a single line' do
input = <<~EOS
:source-highlighter: rouge
[%linenums,text]
----
before
#{' ' * 2}y#{'o' * 100}
after
----
EOS

pdf = to_pdf input, analyze: true
(expect pdf.pages).to have_size 1
text_lines = pdf.lines pdf.text
# FIXME: we lose the indentation on the second line, but that's true of plain listing blocks too
expected_lines = <<~EOS.chomp.split ?\n
1 before
2
\u00a0 yooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
\u00a0 ooooooooooooooooo
3 after
EOS
(expect text_lines).to eql expected_lines
end

it 'should break and wrap numbered line if text wraps but still does not fit on a single line' do
input = <<~EOS
:source-highlighter: rouge
[%linenums,text]
----
one
two and then s#{'o' * 100}me
three
----
EOS

pdf = to_pdf input, analyze: true
(expect pdf.pages).to have_size 1
text_lines = pdf.lines pdf.text
expected_lines = <<~EOS.chomp.split ?\n
1 one
2 two and then
\u00a0 sooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
\u00a0 ooooooooooooooooome
3 three
EOS
(expect text_lines).to eql expected_lines
end
end

context 'CodeRay' do
Expand Down

0 comments on commit f2e6483

Please sign in to comment.