Module: Asciidoctor::PDF::FormattedText::SourceWrap

Defined in:
lib/asciidoctor/pdf/formatted_text/source_wrap.rb

Constant Summary collapse

NoBreakSpace =
?\u00a0

Instance Method Summary collapse

Instance Method Details

#wrap(array) ⇒ Object

Override Prawn::Text::Formatted::Box#wrap method to add line numbers in source blocks. Note that this implementation assumes that the :single_line option is falsy.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/asciidoctor/pdf/formatted_text/source_wrap.rb', line 11

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)), linenum: :spacer }
      highlight_line = (second_fragment = unconsumed[1])[:highlight] ? second_fragment.dup : nil
    else # wrapped line
      first_fragment[:text] = first_fragment[:text].lstrip
      @arranger.unconsumed.unshift highlight_line if highlight_line
      @arranger.unconsumed.unshift linenum_spacer.dup
    end
    @line_wrap.wrap_line \
      document: @document,
      kerning: @kerning,
      width: @width,
      arranger: @arranger,
      disable_wrap_by_char: @disable_wrap_by_char
    if enough_height_for_this_line?
      move_baseline_down
      print_line
      stop = @arranger.finished?
    else
      stop = true
    end
  end
  @text = @printed_lines.join ?\n
  @everything_printed = @arranger.finished?
  @arranger.unconsumed
end