| Class | Erubis::PI::TinyEruby |
| In: |
erubis/tiny.rb
|
| Parent: | Object |
| EMBEDDED_PATTERN | = | /(^[ \t]*)?<\?rb(\s.*?)\?>([ \t]*\r?\n)?|\$(!*)?\{(.*?)\}/ |
| src | [R] |
# File erubis/tiny.rb, line 77 def initialize(input=nil, options={}) @escape = options[:escape] || 'Erubis::XmlHelper.escape_xml' @src = convert(input) if input end
# File erubis/tiny.rb, line 86 def convert(input) src = "_buf = [];" # preamble pos = 0 input.scan(EMBEDDED_PATTERN) do |lspace, stmtcode, rspace, indicator, exprcode| match = Regexp.last_match index = match.begin(0) text = input[pos, index - pos] pos = match.end(0) if stmtcode # <?rb ... ?> code = stmtcode src << " _buf << '" << escape_text(text) << "';" if lspace && rspace src << "#{lspace}#{code}#{rspace}" else src << " _buf << '#{lspace}';" if lspace src << code << ";" src << " _buf << '#{rspace}';" if rspace end else # ${...}, $!{...} code = exprcode if indicator.nil? || indicator.empty? src << " _buf << #{@escape}(" << code << ");" elsif indicator == '!' src << " _buf << (" << code << ").to_s;" end end end rest = $' || input src << " _buf << '" << escape_text(rest) << "';" src << "\n_buf.join\n" # postamble return src end
# File erubis/tiny.rb, line 119 def escape_text(text) return text.gsub!(/['\\]/, '\\\\\&') || text end
# File erubis/tiny.rb, line 127 def evaluate(context=Object.new) if context.is_a?(Hash) obj = Object.new context.each do |k, v| obj.instance_variable_set("@#{k}", v) end context = obj end context.instance_eval @src end