Class: RBlade::CompilesVerbatim

Inherits:
Object
  • Object
show all
Defined in:
lib/rblade/compiler/compiles_verbatim.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.nullify_verbatim(source) ⇒ Object

Replaces verbatim statements with spaces of the same length. Used in comment offset calculation for source maps.



37
38
39
40
41
# File 'lib/rblade/compiler/compiles_verbatim.rb', line 37

def self.nullify_verbatim(source)
  source.gsub(/\s?(?<!\w)@verbatim(?!\w)\s?(?<contents>(?:[^@\s]++|[@\s])+?)\s?(?<!\w)@end_?verbatim(?!\w)\s?/i) do |match|
    " " * match.length
  end
end

Instance Method Details

#compile!(tokens) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rblade/compiler/compiles_verbatim.rb', line 5

def compile!(tokens)
  tokens.map! do |token|
    next(token) if token.type != :unprocessed

    current_match_id = nil
    segments = []
    token.value.split(/\s?(?<!\w)@verbatim(?!\w)\s?(?<contents>(?:[^@\s]++|[@\s])+?)\s?(?<!\w)@end_?verbatim(?!\w)\s?/i) do |before_match|
      next if current_match_id == $~.object_id
      current_match_id = $~.object_id

      # Add the current string to the segment list
      unless before_match == ""
        RBlade::Utility.append_unprocessed_string_segment!(token, segments, before_match)
      end
      next if $~.nil?

      if $~[:contents].present?
        start_offset = segments.last&.end_offset || token.start_offset
        segments << Token.new(
          type: :raw_text,
          value: $~[:contents],
          start_offset: start_offset,
          end_offset: start_offset + $&.length,
        )
      end
    end

    segments
  end.flatten!
end