Class Erubis::Engine
In: erubis/engine.rb
Parent: Object

.[abstract] base engine class

Methods

Constants

DEFAULT_REGEXP = /(.*?)(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m

Attributes

filename  [RW] 
src  [R] 

Public Class methods

load file and create engine object

[Source]

# File erubis/engine.rb, line 93
    def self.load_file(filename, properties={})
      input = File.open(filename, 'rb') { |f| f.read }
      input.untaint   # is it ok?
      properties[:filename] = filename
      engine = self.new(input, properties)
      return engine
    end

[Source]

# File erubis/engine.rb, line 79
    def initialize(input, properties={})
      #@input    = input
      @pattern   = properties[:pattern]
      @filename  = properties[:filename]
      @trim      = properties[:trim] != false
      @preamble  = properties[:preamble]
      @postamble = properties[:postamble]
      @escape    = properties[:escape]
      @src       = compile(input) if input
    end

Public Instance methods

compile input string into target language

[Source]

# File erubis/engine.rb, line 132
    def compile(input)
      src = ""
      @preamble.nil? ? add_preamble(src) : (@preamble && (src << @preamble))
      regexp = pattern_regexp(@pattern)
      input.scan(regexp) do |text, lspace, indicator, code, rspace|
        add_text(src, text)
        ## * when '<%= %>', do nothing
        ## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>'
        if !indicator               # <% %>
          if @trim && lspace && rspace
            add_stmt(src, "#{lspace}#{code}#{rspace}")
          else
            add_text(src, lspace) if lspace
            add_stmt(src, code)
            add_text(src, rspace) if rspace
          end
        elsif indicator[0] == ?\#   # <%# %>
          n = code.count("\n") + (rspace ? 1 : 0)
          if @trim && lspace && rspace
            add_stmt(src, "\n" * n)
          else
            add_text(src, lspace) if lspace
            add_stmt(src, "\n" * n)
            add_text(src, rspace) if rspace
          end
          #flag_trim = @trim && lspace && rspace
          #add_text(src, lspace) if !flag_trim && lspace
          #n = code.count("\n") + (rspace ? 1 : 0)
          #add_stmt(src, "\n" * n)
          #add_text(src, rspace) if !flag_trim && rspace
        else                        # <%= %>
          add_text(src, lspace) if lspace
          add_expr(src, code, indicator)
          add_text(src, rspace) if rspace
        end
        #if indicator && indicator[0] == ?=
        #  flag_trim = false
        #else
        #  flag_trim = @trim && lspace && rspace
        #end
        ##flag_trim = @trim && !(indicator && indicator[0]==?=) && lspace && rspace
        #add_text(src, text)
        #add_text(src, lspace) if !flag_trim && lspace
        #if !indicator             # <% %>
        #  code = "#{lspace}#{code}#{rspace}" if flag_trim
        #  add_stmt(src, code)
        #elsif indicator[0] == ?\# # <%# %>
        #  n = code.count("\n") + (rspace ? 1 : 0)
        #  add_stmt(src, "\n" * n)
        #else                      # <%= %>
        #  add_expr(src, code, indicator)
        #end
        #add_text(src, rspace) if !flag_trim && rspace
      end
      rest = $' || input     # add input when no matched
      add_text(src, rest)
      @postamble.nil? ? add_postamble(src) : (@postamble && (src << @postamble))
      return src
    end

compile input string and set it to @src

[Source]

# File erubis/engine.rb, line 193
    def compile!(input)
      @src = compile(input)
    end

invoke context.instance_eval(@src)

[Source]

# File erubis/engine.rb, line 113
    def evaluate(context=Context.new)
      context = Context.new(context) if context.is_a?(Hash)
      return context.instance_eval(@src, (@filename || '(erubis)'))
    end

eval(@src) with binding object

[Source]

# File erubis/engine.rb, line 102
    def result(_binding_or_hash=TOPLEVEL_BINDING)
      _arg = _binding_or_hash
      if _arg.is_a?(Hash)
        ## load _context data as local variables by eval
        eval _arg.keys.inject("") { |s, k| s << "#{k.to_s} = _arg[#{k.inspect}];" }
        _arg = binding()
      end
      return eval(@src, _arg, (@filename || '(erubis)'))
    end

Protected Instance methods

add expression code to src

[Source]

# File erubis/engine.rb, line 237
    def add_expr(src, code, indicator)
      case indicator
      when '='
        add_expr_literal(src, code)
      when '=='
        add_expr_escaped(src, code)
      when '==='
        add_expr_debug(src, code)
      end
    end

.[abstract] add expression code to src for debug. this is called by add_expr().

[Source]

# File erubis/engine.rb, line 259
    def add_expr_debug(src, code)
      not_implemented
    end

.[abstract] add escaped expression code to src. this is called by add_expr().

[Source]

# File erubis/engine.rb, line 254
    def add_expr_escaped(src, code)
      not_implemented
    end

.[abstract] add expression literal code to src. this is called by add_expr().

[Source]

# File erubis/engine.rb, line 249
    def add_expr_literal(src, code)
      not_implemented
    end

.[abstract] add @postamble to src

[Source]

# File erubis/engine.rb, line 264
    def add_postamble(src)
      not_implemented
    end

.[abstract] add @preamble to src

[Source]

# File erubis/engine.rb, line 222
    def add_preamble(src)
      not_implemented
    end

.[abstract] add statement code to src

[Source]

# File erubis/engine.rb, line 232
    def add_stmt(src, code)
      not_implemented
    end

.[abstract] add text string to src

[Source]

# File erubis/engine.rb, line 227
    def add_text(src, text)
      not_implemented
    end

.[abstract] escape text string

ex.

  def escape_text(text)
    return text.dump
    # or return "'" + text.gsub(/['\\]/, '\\\\\&') + "'"
  end

[Source]

# File erubis/engine.rb, line 206
    def escape_text(text)
      not_implemented
    end

.[abstract] return escaped expression code

ex.

  def escaped_expr(code)
    @escape ||= 'escape'
    return "#{@escape}(#{code.strip})"
  end

[Source]

# File erubis/engine.rb, line 217
    def escaped_expr(code)
      not_implemented
    end

return regexp of pattern to parse eRuby script

[Source]

# File erubis/engine.rb, line 121
    def pattern_regexp(pattern=@pattern)
      if pattern.nil? || pattern == '<% %>'
        return DEFAULT_REGEXP
      else
        prefix, postfix = pattern.split()
        return /(.*?)(^[ \t]*)?#{prefix}(=+|\#)?(.*?)-?#{postfix}([ \t]*\r?\n)?/m
      end
    end

[Validate]