In Files

  • libraries/erb.rb

Class/Module Index [+]

Quicksearch

ERB::Compiler

Attributes

percent[R]
post_cmd[RW]
pre_cmd[RW]
put_cmd[RW]
trim_mode[R]

Public Class Methods

new(trim_mode) click to toggle source
 
               # File libraries/erb.rb, line 391
def initialize(trim_mode)
  @percent, @trim_mode = prepare_trim_mode(trim_mode)
  @put_cmd = 'print'
  @pre_cmd = []
  @post_cmd = []
end
            

Public Instance Methods

compile(s) click to toggle source
 
               # File libraries/erb.rb, line 303
def compile(s)
  out = make_buffer

  content = ''
  scanner = make_scanner(s)
  scanner.scan do |token|
    if scanner.stag.nil?
      case token
      when PercentLine
        out.push("#{@put_cmd} #{content.dump}") if content.size > 0
        content = ''
        out.push(token.to_s)
        out.cr
      when :cr
        out.cr
      when '<%', '<%=', '<%#'
        scanner.stag = token
        out.push("#{@put_cmd} #{content.dump}") if content.size > 0
        content = ''
      when "\n"
        content << "\n"
        out.push("#{@put_cmd} #{content.dump}")
        out.cr
        content = ''
      when '<%%'
        content << '<%'
      else
        content << token
      end
    else
      case token
      when '%>'
        case scanner.stag
        when '<%'
          if content[-1] == ?\n
            content.chop!
            out.push(content)
            out.cr
          else
            out.push(content)
          end
        when '<%='
          out.push("#{@put_cmd}((#{content}).to_s)")
        when '<%#'
          # out.push("# #{content.dump}")
        end
        scanner.stag = nil
        content = ''
      when '%%>'
        content << '%>'
      else
        content << token
      end
    end
  end
  out.push("#{@put_cmd} #{content.dump}") if content.size > 0
  out.close
  out.script
end
            
make_buffer() click to toggle source
 
               # File libraries/erb.rb, line 295
def make_buffer
  if RUBY_VERSION > '1.8'
    Buffer.new(self)
  else
    Buffer16.new(self)
  end
end
            
make_scanner(src) click to toggle source
 
               # File libraries/erb.rb, line 387
def make_scanner(src)
  Scanner.make_scanner(src, @trim_mode, @percent)
end
            
prepare_trim_mode(mode) click to toggle source
 
               # File libraries/erb.rb, line 363
def prepare_trim_mode(mode)
  case mode
  when 1
    return [false, '>']
  when 2
    return [false, '<>']
  when 0
    return [false, nil]
  when String
    perc = mode.include?('%')
    if mode.include?('-')
      return [perc, '-']
    elsif mode.include?('<>')
      return [perc, '<>']
    elsif mode.include?('>')
      return [perc, '>']
    else
      [perc, nil]
    end
  else
    return [false, nil]
  end
end