Class: BabyErubis::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/baby_erubis.rb

Direct Known Subclasses

HtmlTemplate, RailsTemplate

Constant Summary collapse

FREEZE =

Ruby 2.1 feature

(''.freeze).equal?(''.freeze)
PATTERN =

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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = nil) ⇒ Template

Returns a new instance of Template.



39
40
41
42
43
44
# File 'lib/baby_erubis.rb', line 39

def initialize(opts=nil)
  @freeze    = self.class.const_get(:FREEZE)
  if opts
    @freeze = (v=opts[:freeze   ]) != nil ? v : @freeze
  end
end

Instance Attribute Details

#srcObject (readonly)

Returns the value of attribute src.



59
60
61
# File 'lib/baby_erubis.rb', line 59

def src
  @src
end

Instance Method Details

#compile(src, filename = nil, linenum = 1) ⇒ Object



68
69
70
71
72
# File 'lib/baby_erubis.rb', line 68

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') ⇒ Object



46
47
48
49
50
51
52
# File 'lib/baby_erubis.rb', line 46

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) ⇒ Object



54
55
56
57
# File 'lib/baby_erubis.rb', line 54

def from_str(input, filename=nil, linenum=1)
  compile(parse(input), filename, linenum)
  return self
end

#new_context(hash) ⇒ Object



114
115
116
# File 'lib/baby_erubis.rb', line 114

def new_context(hash)
  return TemplateContext.new(hash)
end

#parse(input) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/baby_erubis.rb', line 74

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

#patternObject



64
65
66
# File 'lib/baby_erubis.rb', line 64

def pattern
  return self.class.const_get(:PATTERN)
end

#render(context = {}) ⇒ Object



109
110
111
112
# File 'lib/baby_erubis.rb', line 109

def render(context={})
  ctxobj = context.nil? || context.is_a?(Hash) ? new_context(context) : context
  return ctxobj.instance_eval(&@proc)
end