class BabyErubis::Template

Constants

FREEZE
PATTERN

PATTERN = /(^[ t]*)?<%(#)?(==?)?(.*?)%>([ t]*r?n)?/m

Attributes

src[R]

Public Class Methods

new(opts=nil) click to toggle source
# File lib/baby_erubis.rb, line 38
def initialize(opts=nil)
  @freeze    = self.class.const_get(:FREEZE)
  if opts
    @freeze = (v=opts[:freeze   ]) != nil ? v : @freeze
  end
end

Public Instance Methods

compile(src, filename=nil, linenum=1) click to toggle source
# File lib/baby_erubis.rb, line 67
def compile(src, filename=nil, linenum=1)
  @src = src
  @proc = eval("proc { #{src} }", empty_binding(), filename || '(eRuby)', linenum)
  return self
end
from_file(filename, encoding='utf-8') click to toggle source
# File lib/baby_erubis.rb, line 45
def from_file(filename, encoding='utf-8')
  mode = "rb:#{encoding}"
  mode = "rb" if RUBY_VERSION < '1.9'
  input = File.open(filename, mode) {|f| f.read() }
  compile(parse(input), filename, 1)
  return self
end
from_str(input, filename=nil, linenum=1) click to toggle source
# File lib/baby_erubis.rb, line 53
def from_str(input, filename=nil, linenum=1)
  compile(parse(input), filename, linenum)
  return self
end
new_context(hash) click to toggle source
# File lib/baby_erubis.rb, line 113
def new_context(hash)
  return TemplateContext.new(hash)
end
parse(input) click to toggle source
# File lib/baby_erubis.rb, line 73
def parse(input)
  src = ""
  add_preamble(src)            # preamble
  spc = ""
  pos = 0
  input.scan(pattern()) do |lspace, sharp, ch, code, rspace|
    match = Regexp.last_match
    text  = input[pos, match.begin(0) - pos]
    pos   = match.end(0)
    if sharp                   # comment
      code = ("\n" * code.count("\n"))
      if ! ch && lspace && rspace   # trimmed statement
        add_text(src, "#{spc}#{text}"); add_stmt(src, "#{code}#{rspace}")
        rspace = ""
      else                          # other statement or expression
        add_text(src, "#{spc}#{text}#{lspace}"); add_stmt(src, code)
      end
    else
      if ch                    # expression
        add_text(src, "#{spc}#{text}#{lspace}"); add_expr(src, code, ch)
      elsif lspace && rspace   # statement (trimming)
        add_text(src, "#{spc}#{text}"); add_stmt(src, "#{lspace} #{code};#{rspace}")
        rspace = ""
      else                     # statement (without trimming)
        add_text(src, "#{spc}#{text}#{lspace}"); add_stmt(src, " #{code};")
      end
    end
    spc = rspace
  end
  text = pos == 0 ? input : input[pos..-1]   # or $' || input
  add_text(src, "#{spc}#{text}")
  add_postamble(src)           # postamble
  return src
end
pattern() click to toggle source
# File lib/baby_erubis.rb, line 63
def pattern
  return self.class.const_get(:PATTERN)
end
render(context={}) click to toggle source
# File lib/baby_erubis.rb, line 108
def render(context={})
  ctxobj = context.nil? || context.is_a?(Hash) ? new_context(context) : context
  return ctxobj.instance_eval(&@proc)
end

Protected Instance Methods

add_expr(src, expr, indicator) click to toggle source
# File lib/baby_erubis.rb, line 139
def add_expr(src, expr, indicator)
  return if !expr || expr.empty?
  if expr_has_block(expr)
    src << " _buf << #{expr}"
  elsif indicator == '='        # escaping
    src << " _buf << #{escaped_expr(expr)};"
  else                          # without escaping
    src << " _buf << (#{expr}).to_s;"
  end
end
add_postamble(src) click to toggle source
# File lib/baby_erubis.rb, line 123
def add_postamble(src)
  src << " _buf.to_s\n"
end
add_preamble(src) click to toggle source
# File lib/baby_erubis.rb, line 119
def add_preamble(src)
  src << "_buf = '';"
end
add_stmt(src, stmt) click to toggle source
# File lib/baby_erubis.rb, line 134
def add_stmt(src, stmt)
  return if !stmt || stmt.empty?
  src << stmt
end
add_text(src, text) click to toggle source
# File lib/baby_erubis.rb, line 127
def add_text(src, text)
  return if !text || text.empty?
  freeze = @freeze ? '.freeze' : ''
  text.gsub!(/['\\]/, '\\\\\&')
  src << " _buf << '#{text}'#{freeze};"
end
escaped_expr(code) click to toggle source
# File lib/baby_erubis.rb, line 150
def escaped_expr(code)
  return "(#{code}).to_s"
end
expr_has_block(expr) click to toggle source
# File lib/baby_erubis.rb, line 154
def expr_has_block(expr)
  return expr =~ /(\bdo|\{)\s*(\|[^|]*?\|\s*)?\z/
end

Private Instance Methods

empty_binding() click to toggle source
# File lib/baby_erubis.rb, line 160
def empty_binding
  return binding()
end