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
110 111 112 |
# File 'lib/baby_erubis.rb', line 110 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 99 100 101 102 103 |
# File 'lib/baby_erubis.rb', line 70 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 ⇒ Object
60 61 62 |
# File 'lib/baby_erubis.rb', line 60 def pattern return self.class.const_get(:PATTERN) end |
#render(context = {}) ⇒ Object
105 106 107 108 |
# File 'lib/baby_erubis.rb', line 105 def render(context={}) ctxobj = context.nil? || context.is_a?(Hash) ? new_context(context) : context return ctxobj.instance_eval(&@proc) end |