Class: BabyErubis::Template
- Inherits:
-
Object
- Object
- BabyErubis::Template
- Defined in:
- lib/baby_erubis.rb
Direct Known Subclasses
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
-
#src ⇒ Object
readonly
Returns the value of attribute src.
Instance Method Summary collapse
- #compile(src, filename = nil, linenum = 1) ⇒ Object
- #from_file(filename, encoding = 'utf-8') ⇒ Object
- #from_str(input, filename = nil, linenum = 1) ⇒ Object
-
#initialize(opts = nil) ⇒ Template
constructor
A new instance of Template.
- #new_context(hash) ⇒ Object
- #parse(input) ⇒ Object
- #pattern ⇒ Object
- #render(context = {}) ⇒ Object
Constructor Details
#initialize(opts = nil) ⇒ Template
Returns a new instance of Template.
35 36 37 38 39 40 |
# File 'lib/baby_erubis.rb', line 35 def initialize(opts=nil) @freeze = self.class.const_get(:FREEZE) if opts @freeze = (v=opts[:freeze ]) != nil ? v : @freeze end end |
Instance Attribute Details
#src ⇒ Object (readonly)
Returns the value of attribute src.
55 56 57 |
# File 'lib/baby_erubis.rb', line 55 def src @src end |
Instance Method Details
#compile(src, filename = nil, linenum = 1) ⇒ Object
64 65 66 67 68 |
# File 'lib/baby_erubis.rb', line 64 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
42 43 44 45 46 47 48 |
# File 'lib/baby_erubis.rb', line 42 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
50 51 52 53 |
# File 'lib/baby_erubis.rb', line 50 def from_str(input, filename=nil, linenum=1) compile(parse(input), filename, linenum) return self end |
#new_context(hash) ⇒ Object
105 106 107 |
# File 'lib/baby_erubis.rb', line 105 def new_context(hash) return TemplateContext.new(hash) end |
#parse(input) ⇒ Object
70 71 72 73 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 |
# File 'lib/baby_erubis.rb', line 70 def parse(input) src = "_buf = '';" # preamble pos = 0 input.scan(pattern()) do |lspace, ch, code, rspace| match = Regexp.last_match text = input[pos, match.begin(0) - pos] pos = match.end(0) src << _t(text) if ! ch # statement if lspace && rspace src << "#{lspace} #{code};#{rspace}" else src << _t(lspace) << " #{code};" << _t(rspace) end elsif ch == '=' # expression (escaping) src << _t(lspace) << " _buf << #{escaped_expr(code)};" << _t(rspace) elsif ch == '==' # expression (without escaping) src << _t(lspace) << " _buf << (#{code}).to_s;" << _t(rspace) elsif ch == '#' # comment src << _t(lspace) << ("\n" * code.count("\n")) << _t(rspace) else raise "** unreachable: ch=#{ch.inspect}" end end text = pos == 0 ? input : input[pos..-1] # or $' || input src << _t(text) src << " _buf.to_s\n" # postamble return src end |
#pattern ⇒ Object
60 61 62 |
# File 'lib/baby_erubis.rb', line 60 def pattern return self.class.const_get(:PATTERN) end |
#render(context = {}) ⇒ Object
100 101 102 103 |
# File 'lib/baby_erubis.rb', line 100 def render(context={}) ctxobj = context.nil? || context.is_a?(Hash) ? new_context(context) : context return ctxobj.instance_eval(&@proc) end |