123456789_123456789_123456789_123456789_123456789_

Class: ERB::Compiler::TrimScanner

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Scanner
Instance Chain:
self, Scanner
Inherits: ERB::Compiler::Scanner
Defined in: lib/erb/compiler.rb

Constant Summary

Scanner - Inherited

DEFAULT_ETAGS, DEFAULT_STAGS

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

Scanner - Inherited

Instance Method Summary

Constructor Details

.new(src, trim_mode, percent) ⇒ TrimScanner

[ GitHub ]

  
# File 'lib/erb/compiler.rb', line 115

def initialize(src, trim_mode, percent)
  super
  @trim_mode = trim_mode
  @percent = percent
  if @trim_mode == '>'
    @scan_reg  = /(.*?)(%>\r?\n|#{(stags + etags).join('|')}|\n|\z)/m
    @scan_line = self.method(:trim_line1)
  elsif @trim_mode == '<>'
    @scan_reg  = /(.*?)(%>\r?\n|#{(stags + etags).join('|')}|\n|\z)/m
    @scan_line = self.method(:trim_line2)
  elsif @trim_mode == '-'
    @scan_reg  = /(.*?)(^[ \t]*<%\-|<%\-|-%>\r?\n|-%>|#{(stags + etags).join('|')}|\z)/m
    @scan_line = self.method(:explicit_trim_line)
  else
    @scan_reg  = /(.*?)(#{(stags + etags).join('|')}|\n|\z)/m
    @scan_line = self.method(:scan_line)
  end
end

Instance Method Details

#explicit_trim_line(line)

[ GitHub ]

  
# File 'lib/erb/compiler.rb', line 204

def explicit_trim_line(line)
  line.scan(@scan_reg) do |tokens|
    tokens.each do |token|
      next if token.empty?
      if @stag.nil? && /[ \t]*<%-/ =~ token
        yield('<%')
      elsif @stag && (token == "-%>\n" || token == "-%>\r\n")
        yield('%>')
        yield(:cr)
      elsif @stag && token == '-%>'
        yield('%>')
      else
        yield(token)
      end
    end
  end
end

#is_erb_stag?(s) ⇒ Boolean

[ GitHub ]

  
# File 'lib/erb/compiler.rb', line 223

def is_erb_stag?(s)
  ERB_STAG.member?(s)
end

#percent_line(line, &block)

[ GitHub ]

  
# File 'lib/erb/compiler.rb', line 146

def percent_line(line, &block)
  if @stag || line[0] != ?%
    return @scan_line.call(line, &block)
  end

  line[0] = ''
  if line[0] == ?%
    @scan_line.call(line, &block)
  else
    yield(PercentLine.new(line.chomp))
  end
end

#scan(&block)

[ GitHub ]

  
# File 'lib/erb/compiler.rb', line 134

def scan(&block)
  @stag = nil
  if @percent
    @src.each_line do |line|
      percent_line(line, &block)
    end
  else
    @scan_line.call(@src, &block)
  end
  nil
end

#scan_line(line)

[ GitHub ]

  
# File 'lib/erb/compiler.rb', line 159

def scan_line(line)
  line.scan(@scan_reg) do |tokens|
    tokens.each do |token|
      next if token.empty?
      yield(token)
    end
  end
end

#trim_line1(line)

[ GitHub ]

  
# File 'lib/erb/compiler.rb', line 168

def trim_line1(line)
  line.scan(@scan_reg) do |tokens|
    tokens.each do |token|
      next if token.empty?
      if token == "%>\n" || token == "%>\r\n"
        yield('%>')
        yield(:cr)
      else
        yield(token)
      end
    end
  end
end

#trim_line2(line)

[ GitHub ]

  
# File 'lib/erb/compiler.rb', line 182

def trim_line2(line)
  head = nil
  line.scan(@scan_reg) do |tokens|
    tokens.each do |token|
      next if token.empty?
      head = token unless head
      if token == "%>\n" || token == "%>\r\n"
        yield('%>')
        if is_erb_stag?(head)
          yield(:cr)
        else
          yield("\n")
        end
        head = nil
      else
        yield(token)
        head = nil if token == "\n"
      end
    end
  end
end