Class: Asciidoctor::Rouge::CalloutsSubstitutor

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/rouge/callouts_substitutor.rb

Overview

A substitutor for processing callouts inside a source listing block.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#calloutsHash<Integer, Array<Integer>> (readonly)

Returns:

  • (Hash<Integer, Array<Integer>>)

13
14
15
# File 'lib/asciidoctor/rouge/callouts_substitutor.rb', line 13

def callouts
  @callouts
end

Class Method Details

.create(node) ⇒ CalloutsSubstitutor

Returns a callouts substitutor for the given node.

Parameters:

  • node (Asciidoctor::AbstractNode)

Returns:


17
18
19
# File 'lib/asciidoctor/rouge/callouts_substitutor.rb', line 17

def self.create(node)
  new(node)
end

Instance Method Details

#convert_line(line_num) ⇒ String

Converts the extracted callout markers for the specified line.

Parameters:

  • line_num (Integer)

    the line number (1-based).

Returns:

  • (String)

    converted callout markers for the line_num, or an empty string if there are no callouts for that line.


51
52
53
54
55
56
57
# File 'lib/asciidoctor/rouge/callouts_substitutor.rb', line 51

def convert_line(line_num)
  return '' unless @callouts.key? line_num

  @callouts[line_num]
    .map { |num| convert_callout(num) }
    .join(' ')
end

#extract(text) ⇒ String

Extracts and stashes callout markers from the given text for reinsertion after processing.

This should be used prior passing the source to a code highlighter.

Parameters:

  • text (#each_line)

    source of the listing block.

Returns:

  • (String)

    a copy of the text with callout marks removed.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/asciidoctor/rouge/callouts_substitutor.rb', line 28

def extract(text)
  escape_char = ::Asciidoctor::Substitutors::RS
  @callouts.clear

  text.each_line.with_index(1).map { |line, ln|
    line.gsub(@callout_rx) do
      match = $LAST_MATCH_INFO
      if match[1] == escape_char
        # We have to use sub since we aren't sure it's the first char.
        match[0].sub(escape_char, '')
      else
        (@callouts[ln] ||= []) << match[3].to_i
        nil
      end
    end
  }.join
end