diff --git a/lib/asciidoctor/pdf/formatted_text/source_wrap.rb b/lib/asciidoctor/pdf/formatted_text/source_wrap.rb index 0b190ee85..f1230ee90 100644 --- a/lib/asciidoctor/pdf/formatted_text/source_wrap.rb +++ b/lib/asciidoctor/pdf/formatted_text/source_wrap.rb @@ -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 diff --git a/spec/source_spec.rb b/spec/source_spec.rb index 51d0e80f1..2960de03a 100644 --- a/spec/source_spec.rb +++ b/spec/source_spec.rb @@ -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