| Class | Erubis::PI::TinyEruby |
| In: |
lib/erubis/tiny.rb
|
| Parent: | Object |
| EMBEDDED_PATTERN | = | /(^[ \t]*)?<\?rb(\s.*?)\?>([ \t]*\r?\n)?|@(!+)?\{(.*?)\}@/m |
| src | [R] |
# File lib/erubis/tiny.rb, line 81
81: def initialize(input=nil, options={})
82: @escape = options[:escape] || 'Erubis::XmlHelper.escape_xml'
83: @src = convert(input) if input
84: end
# File lib/erubis/tiny.rb, line 90
90: def convert(input)
91: src = "_buf = '';" # preamble
92: pos = 0
93: input.scan(EMBEDDED_PATTERN) do |lspace, stmt, rspace, indicator, expr|
94: match = Regexp.last_match
95: len = match.begin(0) - pos
96: text = input[pos, len]
97: pos = match.end(0)
98: #src << " _buf << '" << escape_text(text) << "';"
99: text.gsub!(/['\\]/, '\\\\\&')
100: src << " _buf << '" << text << "';" unless text.empty?
101: if stmt # <?rb ... ?>
102: if lspace && rspace
103: src << "#{lspace}#{stmt}#{rspace}"
104: else
105: src << " _buf << '" << lspace << "';" if lspace
106: src << stmt << ";"
107: src << " _buf << '" << rspace << "';" if rspace
108: end
109: else # ${...}, $!{...}
110: if !indicator
111: src << " _buf << " << @escape << "(" << expr << ");"
112: elsif indicator == '!'
113: src << " _buf << (" << expr << ").to_s;"
114: end
115: end
116: end
117: #rest = $' || input # ruby1.8
118: rest = pos == 0 ? input : input[pos..-1] # ruby1.9
119: #src << " _buf << '" << escape_text(rest) << "';"
120: rest.gsub!(/['\\]/, '\\\\\&')
121: src << " _buf << '" << rest << "';" unless rest.empty?
122: src << "\n_buf.to_s\n" # postamble
123: return src
124: end
# File lib/erubis/tiny.rb, line 134
134: def evaluate(_context=Object.new)
135: if _context.is_a?(Hash)
136: _obj = Object.new
137: _context.each do |k, v| _obj.instance_variable_set("@#{k}", v) end
138: _context = _obj
139: end
140: _context.instance_eval @src
141: end