Module: I18nContextGenerator::XmlScanner

Defined in:
lib/i18n_context_generator/xml_scanner.rb

Overview

Small stateful XML scanning helpers for line-oriented diff and location code. This is intentionally not an XML parser; it only removes comments while preserving the remaining text and line boundaries.

Class Method Summary collapse

Class Method Details

.without_comments(line, state) ⇒ Object



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
35
36
# File 'lib/i18n_context_generator/xml_scanner.rb', line 10

def without_comments(line, state)
  remaining = line.to_s
  visible = +''
  contained_comment = false

  loop do
    if state[:inside_comment]
      contained_comment = true
      comment_end = remaining.index('-->')
      return [visible, contained_comment] unless comment_end

      state[:inside_comment] = false
      remaining = remaining[(comment_end + 3)..].to_s
    else
      comment_start = remaining.index('<!--')
      unless comment_start
        visible << remaining
        return [visible, contained_comment]
      end

      visible << remaining[0...comment_start]
      contained_comment = true
      state[:inside_comment] = true
      remaining = remaining[(comment_start + 4)..].to_s
    end
  end
end