Class: Erb::Stripper

Inherits:
Object show all
Defined in:
lib/erb/stripper.rb

Instance Method Summary collapse

Instance Method Details

#to_ruby(source) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/erb/stripper.rb', line 24

def to_ruby(source)
  result = ''
  comment = false
  scanner = ERB::Compiler.new(nil).make_scanner(source)
  scanner.scan do |token|
    comment = true if token == '<%#'
    if scanner.stag.nil?
      result << to_whitespace(token)
      scanner.stag = token if ['<%', '<%%', '<%-', '<%=', '<%%=', '<%#'].include?(token.strip)
    elsif ['%>', '%%>', '-%>'].include?(token.strip)
      result << to_whitespace(token.gsub(/>/, ';'))
      scanner.stag = nil
    else
      result << (comment ? to_whitespace(token) : token)
      comment = false
    end
  end
  result
end

#to_whitespace(str) ⇒ Object



44
45
46
# File 'lib/erb/stripper.rb', line 44

def to_whitespace(str)
  str.gsub(/[^\s;]/, ' ')
end