/External.LCA_RESTRICTED/Languages/Ruby/ruby19/lib/ruby/gems/1.9.1/gems/erubis-2.6.6/contrib/erubis
Ruby | 3330 lines | 1932 code | 614 blank | 784 comment | 276 complexity | 02f7cd7e95faaa90def31c2c42c06c8a MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
Large files files are truncated, but you can click here to view the full file
- #!/usr/bin/env ruby
- ###
- ### $Release: 2.6.6 $
- ### copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ###
- #--begin of require 'erubis/main'
- ###
- ### $Release: 2.6.6 $
- ### copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ###
- require 'yaml'
- #--begin of require 'erubis'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- ##
- ## an implementation of eRuby
- ##
- ## ex.
- ## input = <<'END'
- ## <ul>
- ## <% for item in @list %>
- ## <li><%= item %>
- ## <%== item %></li>
- ## <% end %>
- ## </ul>
- ## END
- ## list = ['<aaa>', 'b&b', '"ccc"']
- ## eruby = Erubis::Eruby.new(input)
- ## puts "--- code ---"
- ## puts eruby.src
- ## puts "--- result ---"
- ## context = Erubis::Context.new() # or new(:list=>list)
- ## context[:list] = list
- ## puts eruby.evaluate(context)
- ##
- ## result:
- ## --- source ---
- ## _buf = ''; _buf << '<ul>
- ## '; for item in @list
- ## _buf << ' <li>'; _buf << ( item ).to_s; _buf << '
- ## '; _buf << ' '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</li>
- ## '; end
- ## _buf << '</ul>
- ## ';
- ## _buf.to_s
- ## --- result ---
- ## <ul>
- ## <li><aaa>
- ## <aaa></li>
- ## <li>b&b
- ## b&b</li>
- ## <li>"ccc"
- ## "ccc"</li>
- ## </ul>
- ##
- module Erubis
- VERSION = ('$Release: 2.6.6 $' =~ /([.\d]+)/) && $1
- end
- #--begin of require 'erubis/engine'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- #--begin of require 'erubis/generator'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- #--begin of require 'abstract'
- ##
- ## $Rev: 1 $
- ## $Release: 0.1.0 $
- ## copyright(c) 2006 kuwata-lab.com all rights reserved.
- ##
- ##
- ## helper to define abstract method in Ruby.
- ##
- ##
- ## example1. (shorter notation)
- ##
- ## require 'abstract'
- ## class Foo
- ## abstract_method 'arg1, arg2=""', :method1, :method2, :method3
- ## end
- ##
- ##
- ## example2. (RDoc friendly notation)
- ##
- ## require 'abstract'
- ## class Bar
- ## # ... method1 description ...
- ## def method1(arg1, arg2="")
- ## not_implemented
- ## end
- ##
- ## # ... method2 description ...
- ## def method2(arg1, arg2="")
- ## not_implemented
- ## end
- ## end
- ##
- ##
- class Module
- ##
- ## define abstract methods
- ##
- def abstract_method args_str, *method_names
- method_names.each do |name|
- module_eval <<-END
- def #{name}(#{args_str})
- mesg = "class \#{self.class.name} must implement abstract method `#{self.name}##{name}()'."
- #mesg = "\#{self.class.name}##{name}() is not implemented."
- err = NotImplementedError.new mesg
- err.set_backtrace caller()
- raise err
- end
- END
- end
- end
- end
- ##
- module Kernel
- ##
- ## raise NotImplementedError
- ##
- def not_implemented #:doc:
- backtrace = caller()
- method_name = (backtrace.shift =~ /`(\w+)'$/) && $1
- mesg = "class #{self.class.name} must implement abstract method '#{method_name}()'."
- #mesg = "#{self.class.name}##{method_name}() is not implemented."
- err = NotImplementedError.new mesg
- err.set_backtrace backtrace
- raise err
- end
- private :not_implemented
- end
- #--end of require 'abstract'
- module Erubis
- ##
- ## code generator, called by Converter module
- ##
- module Generator
- def self.supported_properties() # :nodoc:
- return [
- [:escapefunc, nil, "escape function name"],
- ]
- end
- attr_accessor :escapefunc
- def init_generator(properties={})
- @escapefunc = properties[:escapefunc]
- end
- ## (abstract) escape text string
- ##
- ## ex.
- ## def escape_text(text)
- ## return text.dump
- ## # or return "'" + text.gsub(/['\\]/, '\\\\\&') + "'"
- ## end
- def escape_text(text)
- not_implemented
- end
- ## return escaped expression code (ex. 'h(...)' or 'htmlspecialchars(...)')
- def escaped_expr(code)
- code.strip!
- return "#{@escapefunc}(#{code})"
- end
- ## (abstract) add @preamble to src
- def add_preamble(src)
- not_implemented
- end
- ## (abstract) add text string to src
- def add_text(src, text)
- not_implemented
- end
- ## (abstract) add statement code to src
- def add_stmt(src, code)
- not_implemented
- end
- ## (abstract) add expression literal code to src. this is called by add_expr().
- def add_expr_literal(src, code)
- not_implemented
- end
- ## (abstract) add escaped expression code to src. this is called by add_expr().
- def add_expr_escaped(src, code)
- not_implemented
- end
- ## (abstract) add expression code to src for debug. this is called by add_expr().
- def add_expr_debug(src, code)
- not_implemented
- end
- ## (abstract) add @postamble to src
- def add_postamble(src)
- not_implemented
- end
- end
- end
- #--end of require 'erubis/generator'
- #--begin of require 'erubis/converter'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- #--already included require 'abstract'
- module Erubis
- ##
- ## convert
- ##
- module Converter
- attr_accessor :preamble, :postamble, :escape
- def self.supported_properties # :nodoc:
- return [
- [:preamble, nil, "preamble (no preamble when false)"],
- [:postamble, nil, "postamble (no postamble when false)"],
- [:escape, nil, "escape expression or not in default"],
- ]
- end
- def init_converter(properties={})
- @preamble = properties[:preamble]
- @postamble = properties[:postamble]
- @escape = properties[:escape]
- end
- ## convert input string into target language
- def convert(input)
- codebuf = "" # or []
- @preamble.nil? ? add_preamble(codebuf) : (@preamble && (codebuf << @preamble))
- convert_input(codebuf, input)
- @postamble.nil? ? add_postamble(codebuf) : (@postamble && (codebuf << @postamble))
- @_proc = nil # clear cached proc object
- return codebuf # or codebuf.join()
- end
- protected
- ##
- ## detect spaces at beginning of line
- ##
- def detect_spaces_at_bol(text, is_bol)
- lspace = nil
- if text.empty?
- lspace = "" if is_bol
- elsif text[-1] == ?\n
- lspace = ""
- else
- rindex = text.rindex(?\n)
- if rindex
- s = text[rindex+1..-1]
- if s =~ /\A[ \t]*\z/
- lspace = s
- #text = text[0..rindex]
- text[rindex+1..-1] = ''
- end
- else
- if is_bol && text =~ /\A[ \t]*\z/
- #lspace = text
- #text = nil
- lspace = text.dup
- text[0..-1] = ''
- end
- end
- end
- return lspace
- end
- ##
- ## (abstract) convert input to code
- ##
- def convert_input(codebuf, input)
- not_implemented
- end
- end
- module Basic
- end
- ##
- ## basic converter which supports '<% ... %>' notation.
- ##
- module Basic::Converter
- include Erubis::Converter
- def self.supported_properties # :nodoc:
- return [
- [:pattern, '<% %>', "embed pattern"],
- [:trim, true, "trim spaces around <% ... %>"],
- ]
- end
- attr_accessor :pattern, :trim
- def init_converter(properties={})
- super(properties)
- @pattern = properties[:pattern]
- @trim = properties[:trim] != false
- end
- protected
- ## return regexp of pattern to parse eRuby script
- def pattern_regexp(pattern)
- @prefix, @postfix = pattern.split() # '<% %>' => '<%', '%>'
- #return /(.*?)(^[ \t]*)?#{@prefix}(=+|\#)?(.*?)-?#{@postfix}([ \t]*\r?\n)?/m
- #return /(^[ \t]*)?#{@prefix}(=+|\#)?(.*?)-?#{@postfix}([ \t]*\r?\n)?/m
- return /#{@prefix}(=+|-|\#|%)?(.*?)([-=])?#{@postfix}([ \t]*\r?\n)?/m
- end
- module_function :pattern_regexp
- #DEFAULT_REGEXP = /(.*?)(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
- #DEFAULT_REGEXP = /(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
- #DEFAULT_REGEXP = /<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
- DEFAULT_REGEXP = pattern_regexp('<% %>')
- public
- def convert_input(src, input)
- pat = @pattern
- regexp = pat.nil? || pat == '<% %>' ? DEFAULT_REGEXP : pattern_regexp(pat)
- pos = 0
- is_bol = true # is beginning of line
- input.scan(regexp) do |indicator, code, tailch, rspace|
- match = Regexp.last_match()
- len = match.begin(0) - pos
- text = input[pos, len]
- pos = match.end(0)
- ch = indicator ? indicator[0] : nil
- lspace = ch == ?= ? nil : detect_spaces_at_bol(text, is_bol)
- is_bol = rspace ? true : false
- add_text(src, text) if text && !text.empty?
- ## * when '<%= %>', do nothing
- ## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>'
- if ch == ?= # <%= %>
- rspace = nil if tailch && !tailch.empty?
- add_text(src, lspace) if lspace
- add_expr(src, code, indicator)
- add_text(src, rspace) if rspace
- elsif ch == ?\# # <%# %>
- n = code.count("\n") + (rspace ? 1 : 0)
- if @trim && lspace && rspace
- add_stmt(src, "\n" * n)
- else
- add_text(src, lspace) if lspace
- add_stmt(src, "\n" * n)
- add_text(src, rspace) if rspace
- end
- elsif ch == ?% # <%% %>
- s = "#{lspace}#{@prefix||='<%'}#{code}#{tailch}#{@postfix||='%>'}#{rspace}"
- add_text(src, s)
- else # <% %>
- if @trim && lspace && rspace
- add_stmt(src, "#{lspace}#{code}#{rspace}")
- else
- add_text(src, lspace) if lspace
- add_stmt(src, code)
- add_text(src, rspace) if rspace
- end
- end
- end
- #rest = $' || input # ruby1.8
- rest = pos == 0 ? input : input[pos..-1] # ruby1.9
- add_text(src, rest)
- end
- ## add expression code to src
- def add_expr(src, code, indicator)
- case indicator
- when '='
- @escape ? add_expr_escaped(src, code) : add_expr_literal(src, code)
- when '=='
- @escape ? add_expr_literal(src, code) : add_expr_escaped(src, code)
- when '==='
- add_expr_debug(src, code)
- end
- end
- end
- module PI
- end
- ##
- ## Processing Instructions (PI) converter for XML.
- ## this class converts '<?rb ... ?>' and '${...}' notation.
- ##
- module PI::Converter
- include Erubis::Converter
- def self.desc # :nodoc:
- "use processing instructions (PI) instead of '<% %>'"
- end
- def self.supported_properties # :nodoc:
- return [
- [:trim, true, "trim spaces around <% ... %>"],
- [:pi, 'rb', "PI (Processing Instrunctions) name"],
- [:embchar, '@', "char for embedded expression pattern('@{...}@')"],
- [:pattern, '<% %>', "embed pattern"],
- ]
- end
- attr_accessor :pi, :prefix
- def init_converter(properties={})
- super(properties)
- @trim = properties.fetch(:trim, true)
- @pi = properties[:pi] if properties[:pi]
- @embchar = properties[:embchar] || '@'
- @pattern = properties[:pattern]
- @pattern = '<% %>' if @pattern.nil? #|| @pattern == true
- end
- def convert(input)
- code = super(input)
- return @header || @footer ? "#{@header}#{code}#{@footer}" : code
- end
- protected
- def convert_input(codebuf, input)
- unless @regexp
- @pi ||= 'e'
- ch = Regexp.escape(@embchar)
- if @pattern
- left, right = @pattern.split(' ')
- @regexp = /<\?#{@pi}(?:-(\w+))?(\s.*?)\?>([ \t]*\r?\n)?|#{ch}(!*)?\{(.*?)\}#{ch}|#{left}(=+)(.*?)#{right}/m
- else
- @regexp = /<\?#{@pi}(?:-(\w+))?(\s.*?)\?>([ \t]*\r?\n)?|#{ch}(!*)?\{(.*?)\}#{ch}/m
- end
- end
- #
- is_bol = true
- pos = 0
- input.scan(@regexp) do |pi_arg, stmt, rspace,
- indicator1, expr1, indicator2, expr2|
- match = Regexp.last_match
- len = match.begin(0) - pos
- text = input[pos, len]
- pos = match.end(0)
- lspace = stmt ? detect_spaces_at_bol(text, is_bol) : nil
- is_bol = stmt && rspace ? true : false
- add_text(codebuf, text) # unless text.empty?
- #
- if stmt
- if @trim && lspace && rspace
- add_pi_stmt(codebuf, "#{lspace}#{stmt}#{rspace}", pi_arg)
- else
- add_text(codebuf, lspace) if lspace
- add_pi_stmt(codebuf, stmt, pi_arg)
- add_text(codebuf, rspace) if rspace
- end
- else
- add_pi_expr(codebuf, expr1 || expr2, indicator1 || indicator2)
- end
- end
- #rest = $' || input # ruby1.8
- rest = pos == 0 ? input : input[pos..-1] # ruby1.9
- add_text(codebuf, rest)
- end
- #--
- #def convert_input(codebuf, input)
- # parse_stmts(codebuf, input)
- # #parse_stmts2(codebuf, input)
- #end
- #
- #def parse_stmts(codebuf, input)
- # #regexp = pattern_regexp(@pattern)
- # @pi ||= 'e'
- # @stmt_pattern ||= /<\?#{@pi}(?:-(\w+))?(\s.*?)\?>([ \t]*\r?\n)?/m
- # is_bol = true
- # pos = 0
- # input.scan(@stmt_pattern) do |pi_arg, code, rspace|
- # match = Regexp.last_match
- # len = match.begin(0) - pos
- # text = input[pos, len]
- # pos = match.end(0)
- # lspace = detect_spaces_at_bol(text, is_bol)
- # is_bol = rspace ? true : false
- # parse_exprs(codebuf, text) # unless text.empty?
- # if @trim && lspace && rspace
- # add_pi_stmt(codebuf, "#{lspace}#{code}#{rspace}", pi_arg)
- # else
- # add_text(codebuf, lspace)
- # add_pi_stmt(codebuf, code, pi_arg)
- # add_text(codebuf, rspace)
- # end
- # end
- # rest = $' || input
- # parse_exprs(codebuf, rest)
- #end
- #
- #def parse_exprs(codebuf, input)
- # unless @expr_pattern
- # ch = Regexp.escape(@embchar)
- # if @pattern
- # left, right = @pattern.split(' ')
- # @expr_pattern = /#{ch}(!*)?\{(.*?)\}#{ch}|#{left}(=+)(.*?)#{right}/
- # else
- # @expr_pattern = /#{ch}(!*)?\{(.*?)\}#{ch}/
- # end
- # end
- # pos = 0
- # input.scan(@expr_pattern) do |indicator1, code1, indicator2, code2|
- # indicator = indicator1 || indicator2
- # code = code1 || code2
- # match = Regexp.last_match
- # len = match.begin(0) - pos
- # text = input[pos, len]
- # pos = match.end(0)
- # add_text(codebuf, text) # unless text.empty?
- # add_pi_expr(codebuf, code, indicator)
- # end
- # rest = $' || input
- # add_text(codebuf, rest)
- #end
- #++
- def add_pi_stmt(codebuf, code, pi_arg) # :nodoc:
- case pi_arg
- when nil ; add_stmt(codebuf, code)
- when 'header' ; @header = code
- when 'footer' ; @footer = code
- when 'comment'; add_stmt(codebuf, "\n" * code.count("\n"))
- when 'value' ; add_expr_literal(codebuf, code)
- else ; add_stmt(codebuf, code)
- end
- end
- def add_pi_expr(codebuf, code, indicator) # :nodoc:
- case indicator
- when nil, '', '==' # @{...}@ or <%== ... %>
- @escape == false ? add_expr_literal(codebuf, code) : add_expr_escaped(codebuf, code)
- when '!', '=' # @!{...}@ or <%= ... %>
- @escape == false ? add_expr_escaped(codebuf, code) : add_expr_literal(codebuf, code)
- when '!!', '===' # @!!{...}@ or <%=== ... %>
- add_expr_debug(codebuf, code)
- else
- # ignore
- end
- end
- end
- end
- #--end of require 'erubis/converter'
- #--begin of require 'erubis/evaluator'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- #--begin of require 'erubis/error'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- module Erubis
- ##
- ## base error class
- ##
- class ErubisError < StandardError
- end
- ##
- ## raised when method or function is not supported
- ##
- class NotSupportedError < ErubisError
- end
- end
- #--end of require 'erubis/error'
- #--begin of require 'erubis/context'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- module Erubis
- ##
- ## context object for Engine#evaluate
- ##
- ## ex.
- ## template = <<'END'
- ## Hello <%= @user %>!
- ## <% for item in @list %>
- ## - <%= item %>
- ## <% end %>
- ## END
- ##
- ## context = Erubis::Context.new(:user=>'World', :list=>['a','b','c'])
- ## # or
- ## # context = Erubis::Context.new
- ## # context[:user] = 'World'
- ## # context[:list] = ['a', 'b', 'c']
- ##
- ## eruby = Erubis::Eruby.new(template)
- ## print eruby.evaluate(context)
- ##
- class Context
- include Enumerable
- def initialize(hash=nil)
- hash.each do |name, value|
- self[name] = value
- end if hash
- end
- def [](key)
- return instance_variable_get("@#{key}")
- end
- def []=(key, value)
- return instance_variable_set("@#{key}", value)
- end
- def keys
- return instance_variables.collect { |name| name[1..-1] }
- end
- def each
- instance_variables.each do |name|
- key = name[1..-1]
- value = instance_variable_get(name)
- yield(key, value)
- end
- end
- def to_hash
- hash = {}
- self.keys.each { |key| hash[key] = self[key] }
- return hash
- end
- def update(context_or_hash)
- arg = context_or_hash
- if arg.is_a?(Hash)
- arg.each do |key, val|
- self[key] = val
- end
- else
- arg.instance_variables.each do |varname|
- key = varname[1..-1]
- val = arg.instance_variable_get(varname)
- self[key] = val
- end
- end
- end
- end
- end
- #--end of require 'erubis/context'
- module Erubis
- EMPTY_BINDING = binding()
- ##
- ## evaluate code
- ##
- module Evaluator
- def self.supported_properties # :nodoc:
- return []
- end
- attr_accessor :src, :filename
- def init_evaluator(properties)
- @filename = properties[:filename]
- end
- def result(*args)
- raise NotSupportedError.new("evaluation of code except Ruby is not supported.")
- end
- def evaluate(*args)
- raise NotSupportedError.new("evaluation of code except Ruby is not supported.")
- end
- end
- ##
- ## evaluator for Ruby
- ##
- module RubyEvaluator
- include Evaluator
- def self.supported_properties # :nodoc:
- list = Evaluator.supported_properties
- return list
- end
- ## eval(@src) with binding object
- def result(_binding_or_hash=TOPLEVEL_BINDING)
- _arg = _binding_or_hash
- if _arg.is_a?(Hash)
- _b = binding()
- eval _arg.collect{|k,v| "#{k} = _arg[#{k.inspect}]; "}.join, _b
- elsif _arg.is_a?(Binding)
- _b = _arg
- elsif _arg.nil?
- _b = binding()
- else
- raise ArgumentError.new("#{self.class.name}#result(): argument should be Binding or Hash but passed #{_arg.class.name} object.")
- end
- return eval(@src, _b, (@filename || '(erubis'))
- end
- ## invoke context.instance_eval(@src)
- def evaluate(_context=Context.new)
- _context = Context.new(_context) if _context.is_a?(Hash)
- #return _context.instance_eval(@src, @filename || '(erubis)')
- #@_proc ||= eval("proc { #{@src} }", Erubis::EMPTY_BINDING, @filename || '(erubis)')
- @_proc ||= eval("proc { #{@src} }", binding(), @filename || '(erubis)')
- return _context.instance_eval(&@_proc)
- end
- ## if object is an Class or Module then define instance method to it,
- ## else define singleton method to it.
- def def_method(object, method_name, filename=nil)
- m = object.is_a?(Module) ? :module_eval : :instance_eval
- object.__send__(m, "def #{method_name}; #{@src}; end", filename || @filename || '(erubis)')
- end
- end
- end
- #--end of require 'erubis/evaluator'
- #--already included require 'erubis/context'
- module Erubis
- ##
- ## (abstract) abstract engine class.
- ## subclass must include evaluator and converter module.
- ##
- class Engine
- #include Evaluator
- #include Converter
- #include Generator
- def initialize(input=nil, properties={})
- #@input = input
- init_generator(properties)
- init_converter(properties)
- init_evaluator(properties)
- @src = convert(input) if input
- end
- ##
- ## convert input string and set it to @src
- ##
- def convert!(input)
- @src = convert(input)
- end
- ##
- ## load file, write cache file, and return engine object.
- ## this method create code cache file automatically.
- ## cachefile name can be specified with properties[:cachename],
- ## or filname + 'cache' is used as default.
- ##
- def self.load_file(filename, properties={})
- cachename = properties[:cachename] || (filename + '.cache')
- properties[:filename] = filename
- if test(?f, cachename) && File.mtime(filename) <= File.mtime(cachename)
- engine = self.new(nil, properties)
- engine.src = File.read(cachename)
- else
- input = File.open(filename, 'rb') {|f| f.read }
- engine = self.new(input, properties)
- File.open(cachename, 'wb') do |f|
- f.flock(File::LOCK_EX)
- f.write(engine.src)
- f.flush()
- end
- end
- engine.src.untaint # ok?
- return engine
- end
- ##
- ## helper method to convert and evaluate input text with context object.
- ## context may be Binding, Hash, or Object.
- ##
- def process(input, context=nil, filename=nil)
- code = convert(input)
- filename ||= '(erubis)'
- if context.is_a?(Binding)
- return eval(code, context, filename)
- else
- context = Context.new(context) if context.is_a?(Hash)
- return context.instance_eval(code, filename)
- end
- end
- ##
- ## helper method evaluate Proc object with contect object.
- ## context may be Binding, Hash, or Object.
- ##
- def process_proc(proc_obj, context=nil, filename=nil)
- if context.is_a?(Binding)
- filename ||= '(erubis)'
- return eval(proc_obj, context, filename)
- else
- context = Context.new(context) if context.is_a?(Hash)
- return context.instance_eval(&proc_obj)
- end
- end
- end # end of class Engine
- ##
- ## (abstract) base engine class for Eruby, Eperl, Ejava, and so on.
- ## subclass must include generator.
- ##
- class Basic::Engine < Engine
- include Evaluator
- include Basic::Converter
- include Generator
- end
- class PI::Engine < Engine
- include Evaluator
- include PI::Converter
- include Generator
- end
- end
- #--end of require 'erubis/engine'
- #require 'erubis/generator'
- #require 'erubis/converter'
- #require 'erubis/evaluator'
- #require 'erubis/error'
- #require 'erubis/context'
- #--begin of require 'erubis/helper'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- module Erubis
- ##
- ## helper for xml
- ##
- module XmlHelper
- module_function
- ESCAPE_TABLE = {
- '&' => '&',
- '<' => '<',
- '>' => '>',
- '"' => '"',
- "'" => ''',
- }
- def escape_xml(value)
- value.to_s.gsub(/[&<>"]/) { |s| ESCAPE_TABLE[s] } # or /[&<>"']/
- #value.to_s.gsub(/[&<>"]/) { ESCAPE_TABLE[$&] }
- end
- def escape_xml2(value)
- return value.to_s.gsub(/\&/,'&').gsub(/</,'<').gsub(/>/,'>').gsub(/"/,'"')
- end
- alias h escape_xml
- alias html_escape escape_xml
- def url_encode(str)
- return str.gsub(/[^-_.a-zA-Z0-9]+/) { |s|
- s.unpack('C*').collect { |i| "%%%02X" % i }.join
- }
- end
- alias u url_encode
- end
- end
- #--end of require 'erubis/helper'
- #--begin of require 'erubis/enhancer'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- module Erubis
- ##
- ## switch '<%= ... %>' to escaped and '<%== ... %>' to unescaped
- ##
- ## ex.
- ## class XmlEruby < Eruby
- ## include EscapeEnhancer
- ## end
- ##
- ## this is language-indenedent.
- ##
- module EscapeEnhancer
- def self.desc # :nodoc:
- "switch '<%= %>' to escaped and '<%== %>' to unescaped"
- end
- #--
- #def self.included(klass)
- # klass.class_eval <<-END
- # alias _add_expr_literal add_expr_literal
- # alias _add_expr_escaped add_expr_escaped
- # alias add_expr_literal _add_expr_escaped
- # alias add_expr_escaped _add_expr_literal
- # END
- #end
- #++
- def add_expr(src, code, indicator)
- case indicator
- when '='
- @escape ? add_expr_literal(src, code) : add_expr_escaped(src, code)
- when '=='
- @escape ? add_expr_escaped(src, code) : add_expr_literal(src, code)
- when '==='
- add_expr_debug(src, code)
- end
- end
- end
- #--
- ## (obsolete)
- #module FastEnhancer
- #end
- #++
- ##
- ## use $stdout instead of string
- ##
- ## this is only for Eruby.
- ##
- module StdoutEnhancer
- def self.desc # :nodoc:
- "use $stdout instead of array buffer or string buffer"
- end
- def add_preamble(src)
- src << "_buf = $stdout;"
- end
- def add_postamble(src)
- src << "\n''\n"
- end
- end
- ##
- ## use print statement instead of '_buf << ...'
- ##
- ## this is only for Eruby.
- ##
- module PrintOutEnhancer
- def self.desc # :nodoc:
- "use print statement instead of '_buf << ...'"
- end
- def add_preamble(src)
- end
- def add_text(src, text)
- src << " print '" << escape_text(text) << "';" unless text.empty?
- end
- def add_expr_literal(src, code)
- src << ' print((' << code << ').to_s);'
- end
- def add_expr_escaped(src, code)
- src << ' print ' << escaped_expr(code) << ';'
- end
- def add_postamble(src)
- src << "\n" unless src[-1] == ?\n
- end
- end
- ##
- ## enable print function
- ##
- ## Notice: use Eruby#evaluate() and don't use Eruby#result()
- ## to be enable print function.
- ##
- ## this is only for Eruby.
- ##
- module PrintEnabledEnhancer
- def self.desc # :nodoc:
- "enable to use print function in '<% %>'"
- end
- def add_preamble(src)
- src << "@_buf = "
- super
- end
- def print(*args)
- args.each do |arg|
- @_buf << arg.to_s
- end
- end
- def evaluate(context=nil)
- _src = @src
- if context.is_a?(Hash)
- context.each do |key, val| instance_variable_set("@#{key}", val) end
- elsif context
- context.instance_variables.each do |name|
- instance_variable_set(name, context.instance_variable_get(name))
- end
- end
- return instance_eval(_src, (@filename || '(erubis)'))
- end
- end
- ##
- ## return array instead of string
- ##
- ## this is only for Eruby.
- ##
- module ArrayEnhancer
- def self.desc # :nodoc:
- "return array instead of string"
- end
- def add_preamble(src)
- src << "_buf = [];"
- end
- def add_postamble(src)
- src << "\n" unless src[-1] == ?\n
- src << "_buf\n"
- end
- end
- ##
- ## use an Array object as buffer (included in Eruby by default)
- ##
- ## this is only for Eruby.
- ##
- module ArrayBufferEnhancer
- def self.desc # :nodoc:
- "use an Array object for buffering (included in Eruby class)"
- end
- def add_preamble(src)
- src << "_buf = [];"
- end
- def add_postamble(src)
- src << "\n" unless src[-1] == ?\n
- src << "_buf.join\n"
- end
- end
- ##
- ## use String class for buffering
- ##
- ## this is only for Eruby.
- ##
- module StringBufferEnhancer
- def self.desc # :nodoc:
- "use a String object for buffering"
- end
- def add_preamble(src)
- src << "_buf = '';"
- end
- def add_postamble(src)
- src << "\n" unless src[-1] == ?\n
- src << "_buf.to_s\n"
- end
- end
- ##
- ## use StringIO class for buffering
- ##
- ## this is only for Eruby.
- ##
- module StringIOEnhancer # :nodoc:
- def self.desc # :nodoc:
- "use a StringIO object for buffering"
- end
- def add_preamble(src)
- src << "_buf = StringIO.new;"
- end
- def add_postamble(src)
- src << "\n" unless src[-1] == ?\n
- src << "_buf.string\n"
- end
- end
- ##
- ## set buffer variable name to '_erbout' as well as '_buf'
- ##
- ## this is only for Eruby.
- ##
- module ErboutEnhancer
- def self.desc # :nodoc:
- "set '_erbout = _buf = \"\";' to be compatible with ERB."
- end
- def add_preamble(src)
- src << "_erbout = _buf = '';"
- end
- def add_postamble(src)
- src << "\n" unless src[-1] == ?\n
- src << "_buf.to_s\n"
- end
- end
- ##
- ## remove text and leave code, especially useful when debugging.
- ##
- ## ex.
- ## $ erubis -s -E NoText file.eruby | more
- ##
- ## this is language independent.
- ##
- module NoTextEnhancer
- def self.desc # :nodoc:
- "remove text and leave code (useful when debugging)"
- end
- def add_text(src, text)
- src << ("\n" * text.count("\n"))
- if text[-1] != ?\n
- text =~ /^(.*?)\z/
- src << (' ' * $1.length)
- end
- end
- end
- ##
- ## remove code and leave text, especially useful when validating HTML tags.
- ##
- ## ex.
- ## $ erubis -s -E NoCode file.eruby | tidy -errors
- ##
- ## this is language independent.
- ##
- module NoCodeEnhancer
- def self.desc # :nodoc:
- "remove code and leave text (useful when validating HTML)"
- end
- def add_preamble(src)
- end
- def add_postamble(src)
- end
- def add_text(src, text)
- src << text
- end
- def add_expr(src, code, indicator)
- src << "\n" * code.count("\n")
- end
- def add_stmt(src, code)
- src << "\n" * code.count("\n")
- end
- end
- ##
- ## get convert faster, but spaces around '<%...%>' are not trimmed.
- ##
- ## this is language-independent.
- ##
- module SimplifyEnhancer
- def self.desc # :nodoc:
- "get convert faster but leave spaces around '<% %>'"
- end
- #DEFAULT_REGEXP = /(^[ \t]*)?<%(=+|\#)?(.*?)-?%>([ \t]*\r?\n)?/m
- SIMPLE_REGEXP = /<%(=+|\#)?(.*?)-?%>/m
- def convert(input)
- src = ""
- add_preamble(src)
- #regexp = pattern_regexp(@pattern)
- pos = 0
- input.scan(SIMPLE_REGEXP) do |indicator, code|
- match = Regexp.last_match
- index = match.begin(0)
- text = input[pos, index - pos]
- pos = match.end(0)
- add_text(src, text)
- if !indicator # <% %>
- add_stmt(src, code)
- elsif indicator[0] == ?\# # <%# %>
- n = code.count("\n")
- add_stmt(src, "\n" * n)
- else # <%= %>
- add_expr(src, code, indicator)
- end
- end
- #rest = $' || input # ruby1.8
- rest = pos == 0 ? input : input[pos..-1] # ruby1.9
- add_text(src, rest)
- add_postamble(src)
- return src
- end
- end
- ##
- ## enable to use other embedded expression pattern (default is '\[= =\]').
- ##
- ## notice! this is an experimental. spec may change in the future.
- ##
- ## ex.
- ## input = <<END
- ## <% for item in list %>
- ## <%= item %> : <%== item %>
- ## [= item =] : [== item =]
- ## <% end %>
- ## END
- ##
- ## class BiPatternEruby
- ## include BiPatternEnhancer
- ## end
- ## eruby = BiPatternEruby.new(input, :bipattern=>'\[= =\]')
- ## list = ['<a>', 'b&b', '"c"']
- ## print eruby.result(binding())
- ##
- ## ## output
- ## <a> : <a>
- ## <a> : <a>
- ## b&b : b&b
- ## b&b : b&b
- ## "c" : "c"
- ## "c" : "c"
- ##
- ## this is language independent.
- ##
- module BiPatternEnhancer
- def self.desc # :nodoc:
- "another embedded expression pattern (default '\[= =\]')."
- end
- def initialize(input, properties={})
- self.bipattern = properties[:bipattern] # or '\$\{ \}'
- super
- end
- ## when pat is nil then '\[= =\]' is used
- def bipattern=(pat) # :nodoc:
- @bipattern = pat || '\[= =\]'
- pre, post = @bipattern.split()
- @bipattern_regexp = /(.*?)#{pre}(=*)(.*?)#{post}/m
- end
- def add_text(src, text)
- return unless text
- m = nil
- text.scan(@bipattern_regexp) do |txt, indicator, code|
- m = Regexp.last_match
- super(src, txt)
- add_expr(src, code, '=' + indicator)
- end
- #rest = $' || text # ruby1.8
- rest = m ? text[m.end(0)..-1] : text # ruby1.9
- super(src, rest)
- end
- end
- ##
- ## regards lines starting with '%' as program code
- ##
- ## this is for compatibility to eruby and ERB.
- ##
- ## this is language-independent.
- ##
- module PercentLineEnhancer
- def self.desc # :nodoc:
- "regard lines starting with '%' as program code"
- end
- def add_text(src, text)
- pos = 0
- text2 = ''
- text.scan(/^\%(.*?\r?\n)/) do
- line = $1
- match = Regexp.last_match
- len = match.begin(0) - pos
- str = text[pos, len]
- pos = match.end(0)
- if text2.empty?
- text2 = str
- else
- text2 << str
- end
- if line[0] == ?%
- text2 << line
- else
- super(src, text2)
- text2 = ''
- add_stmt(src, line)
- end
- end
- #rest = pos == 0 ? text : $' # ruby1.8
- rest = pos == 0 ? text : text[pos..-1] # ruby1.9
- unless text2.empty?
- text2 << rest if rest
- rest = text2
- end
- super(src, rest)
- end
- end
- ##
- ## [experimental] allow header and footer in eRuby script
- ##
- ## ex.
- ## ====================
- ## ## without header and footer
- ## $ cat ex1.eruby
- ## <% def list_items(list) %>
- ## <% for item in list %>
- ## <li><%= item %></li>
- ## <% end %>
- ## <% end %>
- ##
- ## $ erubis -s ex1.eruby
- ## _buf = []; def list_items(list)
- ## ; for item in list
- ## ; _buf << '<li>'; _buf << ( item ).to_s; _buf << '</li>
- ## '; end
- ## ; end
- ## ;
- ## _buf.join
- ##
- ## ## with header and footer
- ## $ cat ex2.eruby
- ## <!--#header:
- ## def list_items(list)
- ## #-->
- ## <% for item in list %>
- ## <li><%= item %></li>
- ## <% end %>
- ## <!--#footer:
- ## end
- ## #-->
- ##
- ## $ erubis -s -c HeaderFooterEruby ex4.eruby
- ##
- ## def list_items(list)
- ## _buf = []; _buf << '
- ## '; for item in list
- ## ; _buf << '<li>'; _buf << ( item ).to_s; _buf << '</li>
- ## '; end
- ## ; _buf << '
- ## ';
- ## _buf.join
- ## end
- ##
- ## ====================
- ##
- ## this is language-independent.
- ##
- module HeaderFooterEnhancer
- def self.desc # :nodoc:
- "allow header/footer in document (ex. '<!--#header: #-->')"
- end
- HEADER_FOOTER_PATTERN = /(.*?)(^[ \t]*)?<!--\#(\w+):(.*?)\#-->([ \t]*\r?\n)?/m
- def add_text(src, text)
- m = nil
- text.scan(HEADER_FOOTER_PATTERN) do |txt, lspace, word, content, rspace|
- m = Regexp.last_match
- flag_trim = @trim && lspace && rspace
- super(src, txt)
- content = "#{lspace}#{content}#{rspace}" if flag_trim
- super(src, lspace) if !flag_trim && lspace
- instance_variable_set("@#{word}", content)
- super(src, rspace) if !flag_trim && rspace
- end
- #rest = $' || text # ruby1.8
- rest = m ? text[m.end(0)..-1] : text # ruby1.9
- super(src, rest)
- end
- attr_accessor :header, :footer
- def convert(input)
- source = super
- return @src = "#{@header}#{source}#{@footer}"
- end
- end
- ##
- ## delete indentation of HTML.
- ##
- ## this is language-independent.
- ##
- module DeleteIndentEnhancer
- def self.desc # :nodoc:
- "delete indentation of HTML."
- end
- def convert_input(src, input)
- input = input.gsub(/^[ \t]+</, '<')
- super(src, input)
- end
- end
- ##
- ## convert "<h1><%=title%></h1>" into "_buf << %Q`<h1>#{title}</h1>`"
- ##
- ## this is only for Eruby.
- ##
- module InterpolationEnhancer
- def self.desc # :nodoc:
- "convert '<p><%=text%></p>' into '_buf << %Q`<p>\#{text}</p>`'"
- end
- def convert_input(src, input)
- pat = @pattern
- regexp = pat.nil? || pat == '<% %>' ? Basic::Converter::DEFAULT_REGEXP : pattern_regexp(pat)
- pos = 0
- is_bol = true # is beginning of line
- str = ''
- input.scan(regexp) do |indicator, code, tailch, rspace|
- match = Regexp.last_match()
- len = match.begin(0) - pos
- text = input[pos, len]
- pos = match.end(0)
- ch = indicator ? indicator[0] : nil
- lspace = ch == ?= ? nil : detect_spaces_at_bol(text, is_bol)
- is_bol = rspace ? true : false
- _add_text_to_str(str, text)
- ## * when '<%= %>', do nothing
- ## * when '<% %>' or '<%# %>', delete spaces iff only spaces are around '<% %>'
- if ch == ?= # <%= %>
- rspace = nil if tailch && !tailch.empty?
- str << lspace if lspace
- add_expr(str, code, indicator)
- str << rspace if rspace
- elsif ch == ?\# # <%# %>
- n = code.count("\n") + (rspace ? 1 : 0)
- if @trim && lspace && rspace
- add_text(src, str)
- str = ''
- add_stmt(src, "\n" * n)
- else
- str << lspace if lspace
- add_text(src, str)
- str = ''
- add_stmt(src, "\n" * n)
- str << rspace if rspace
- end
- else # <% %>
- if @trim && lspace && rspace
- add_text(src, str)
- str = ''
- add_stmt(src, "#{lspace}#{code}#{rspace}")
- else
- str << lspace if lspace
- add_text(src, str)
- str = ''
- add_stmt(src, code)
- str << rspace if rspace
- end
- end
- end
- #rest = $' || input # ruby1.8
- rest = pos == 0 ? input : input[pos..-1] # ruby1.9
- _add_text_to_str(str, rest)
- add_text(src, str)
- end
- def add_text(src, text)
- return if !text || text.empty?
- #src << " _buf << %Q`" << text << "`;"
- if text[-1] == ?\n
- text[-1] = "\\n"
- src << " _buf << %Q`" << text << "`\n"
- else
- src << " _buf << %Q`" << text << "`;"
- end
- end
- def _add_text_to_str(str, text)
- return if !text || text.empty?
- text.gsub!(/[`\#\\]/, '\\\\\&')
- str << text
- end
- def add_expr_escaped(str, code)
- str << "\#{#{escaped_expr(code)}}"
- end
- def add_expr_literal(str, code)
- str << "\#{#{code}}"
- end
- end
- end
- #--end of require 'erubis/enhancer'
- #require 'erubis/tiny'
- #--begin of require 'erubis/engine/eruby'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- #--already included require 'erubis/engine'
- #--already included require 'erubis/enhancer'
- module Erubis
- ##
- ## code generator for Ruby
- ##
- module RubyGenerator
- include Generator
- #include ArrayBufferEnhancer
- include StringBufferEnhancer
- def init_generator(properties={})
- super
- @escapefunc ||= "Erubis::XmlHelper.escape_xml"
- end
- def self.supported_properties() # :nodoc:
- return []
- end
- def escape_text(text)
- text.gsub(/['\\]/, '\\\\\&') # "'" => "\\'", '\\' => '\\\\'
- end
- def escaped_expr(code)
- return "#{@escapefunc}(#{code})"
- end
- #--
- #def add_preamble(src)
- # src << "_buf = [];"
- #end
- #++
- def add_text(src, text)
- src << " _buf << '" << escape_text(text) << "';" unless text.empty?
- end
- def add_stmt(src, code)
- #src << code << ';'
- src << code
- src << ';' unless code[-1] == ?\n
- end
- def add_expr_literal(src, code)
- src << ' _buf << (' << code << ').to_s;'
- end
- def add_expr_escaped(src, code)
- src << ' _buf << ' << escaped_expr(code) << ';'
- end
- def add_expr_debug(src, code)
- code.strip!
- s = (code.dump =~ /\A"(.*)"\z/) && $1
- src << ' $stderr.puts("*** debug: ' << s << '=#{(' << code << ').inspect}");'
- end
- #--
- #def add_postamble(src)
- # src << "\n_buf.join\n"
- #end
- #++
- end
- ##
- ## engine for Ruby
- ##
- class Eruby < Basic::Engine
- include RubyEvaluator
- include RubyGenerator
- end
- ##
- ## fast engine for Ruby
- ##
- class FastEruby < Eruby
- include InterpolationEnhancer
- end
- ##
- ## swtich '<%= %>' to escaped and '<%== %>' to not escaped
- ##
- class EscapedEruby < Eruby
- include EscapeEnhancer
- end
- ##
- ## sanitize expression (<%= ... %>) by default
- ##
- ## this is equivalent to EscapedEruby and is prepared only for compatibility.
- ##
- class XmlEruby < Eruby
- include EscapeEnhancer
- end
- class PI::Eruby < PI::Engine
- include RubyEvaluator
- include RubyGenerator
- def init_converter(properties={})
- @pi = 'rb'
- super(properties)
- end
- end
- end
- #--end of require 'erubis/engine/eruby'
- #require 'erubis/engine/enhanced' # enhanced eruby engines
- #require 'erubis/engine/optimized' # generates optimized ruby code
- #require 'erubis/engine/ephp'
- #require 'erubis/engine/ec'
- #require 'erubis/engine/ejava'
- #require 'erubis/engine/escheme'
- #require 'erubis/engine/eperl'
- #require 'erubis/engine/ejavascript'
- #--begin of require 'erubis/local-setting'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- ##
- ## you can add site-local settings here.
- ## this files is required by erubis.rb
- ##
- #--end of require 'erubis/local-setting'
- #--end of require 'erubis'
- #--begin of require 'erubis/tiny'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- module Erubis
- ##
- ## tiny and the simplest implementation of eRuby
- ##
- ## ex.
- ## eruby = TinyEruby.new(File.read('example.rhtml'))
- ## print eruby.src # print ruby code
- ## print eruby.result(binding()) # eval ruby code with Binding object
- ## print eruby.evalute(context) # eval ruby code with context object
- ##
- class TinyEruby
- def initialize(input=nil)
- @src = convert(input) if input
- end
- attr_reader :src
- EMBEDDED_PATTERN = /<%(=+|\#)?(.*?)-?%>/m
- def convert(input)
- src = "_buf = '';" # preamble
- pos = 0
- input.scan(EMBEDDED_PATTERN) do |indicator, code|
- m = Regexp.last_match
- text = input[pos...m.begin(0)]
- pos = m.end(0)
- #src << " _buf << '" << escape_text(text) << "';"
- text.gsub!(/['\\]/, '\\\\\&')
- src << " _buf << '" << text << "';" unless text.empty?
- if !indicator # <% %>
- src << code << ";"
- elsif indicator == '#' # <%# %>
- src << ("\n" * code.count("\n"))
- else # <%= %>
- src << " _buf << (" << code << ").to_s;"
- end
- end
- #rest = $' || input # ruby1.8
- rest = pos == 0 ? input : input[pos..-1] # ruby1.9
- #src << " _buf << '" << escape_text(rest) << "';"
- rest.gsub!(/['\\]/, '\\\\\&')
- src << " _buf << '" << rest << "';" unless rest.empty?
- src << "\n_buf.to_s\n" # postamble
- return src
- end
- #def escape_text(text)
- # return text.gsub!(/['\\]/, '\\\\\&') || text
- #end
- def result(_binding=TOPLEVEL_BINDING)
- eval @src, _binding
- end
- 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
- end
- module PI
- end
- class PI::TinyEruby
- def initialize(input=nil, options={})
- @escape = options[:escape] || 'Erubis::XmlHelper.escape_xml'
- @src = convert(input) if input
- end
- attr_reader :src
- EMBEDDED_PATTERN = /(^[ \t]*)?<\?rb(\s.*?)\?>([ \t]*\r?\n)?|@(!+)?\{(.*?)\}@/m
- def convert(input)
- src = "_buf = '';" # preamble
- pos = 0
- input.scan(EMBEDDED_PATTERN) do |lspace, stmt, rspace, indicator, expr|
- match = Regexp.last_match
- len = match.begin(0) - pos
- text = input[pos, len]
- pos = match.end(0)
- #src << " _buf << '" << escape_text(text) << "';"
- text.gsub!(/['\\]/, '\\\\\&')
- src << " _buf << '" << text << "';" unless text.empty?
- if stmt # <?rb ... ?>
- if lspace && rspace
- src << "#{lspace}#{stmt}#{rspace}"
- else
- src << " _buf << '" << lspace << "';" if lspace
- src << stmt << ";"
- src << " _buf << '" << rspace << "';" if rspace
- end
- else # ${...}, $!{...}
- if !indicator
- src << " _buf << " << @escape << "(" << expr << ");"
- elsif indicator == '!'
- src << " _buf << (" << expr << ").to_s;"
- end
- end
- end
- #rest = $' || input # ruby1.8
- rest = pos == 0 ? input : input[pos..-1] # ruby1.9
- #src << " _buf << '" << escape_text(rest) << "';"
- rest.gsub!(/['\\]/, '\\\\\&')
- src << " _buf << '" << rest << "';" unless rest.empty?
- src << "\n_buf.to_s\n" # postamble
- return src
- end
- #def escape_text(text)
- # return text.gsub!(/['\\]/, '\\\\\&') || text
- #end
- def result(_binding=TOPLEVEL_BINDING)
- eval @src, _binding
- end
- 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
- end
- end
- #--end of require 'erubis/tiny'
- #--begin of require 'erubis/engine/enhanced'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- #--already included require 'erubis/enhancer'
- #--already included require 'erubis/engine/eruby'
- module Erubis
- #--
- ## moved to engine/ruby.rb
- #class EscapedEruby < Eruby
- # include EscapeEnhancer
- #end
- #++
- #--
- ### (obsolete)
- #class FastEruby < Eruby
- # include FastEnhancer
- #end
- #++
- class StdoutEruby < Eruby
- include StdoutEnhancer
- end
- class PrintOutEruby < Eruby
- include PrintOutEnhancer
- end
- class PrintEnabledEruby < Eruby
- include PrintEnabledEnhancer
- end
- class ArrayEruby < Eruby
- include ArrayEnhancer
- end
- class ArrayBufferEruby < Eruby
- include ArrayBufferEnhancer
- end
- class StringBufferEruby < Eruby
- include StringBufferEnhancer
- end
- class StringIOEruby < Eruby
- include StringIOEnhancer
- end
- class ErboutEruby < Eruby
- include ErboutEnhancer
- end
- class NoTextEruby < Eruby
- include NoTextEnhancer
- end
- class NoCodeEruby < Eruby
- include NoCodeEnhancer
- end
- class SimplifiedEruby < Eruby
- include SimplifyEnhancer
- end
- class StdoutSimplifiedEruby < Eruby
- include StdoutEnhancer
- include SimplifyEnhancer
- end
- class PrintOutSimplifiedEruby < Eruby
- include PrintOutEnhancer
- include SimplifyEnhancer
- end
- class BiPatternEruby < Eruby
- include BiPatternEnhancer
- end
- class PercentLineEruby < Eruby
- include PercentLineEnhancer
- end
- class HeaderFooterEruby < Eruby
- include HeaderFooterEnhancer
- end
- class DeleteIndentEruby < Eruby
- include DeleteIndentEnhancer
- end
- class InterpolationEruby < Eruby
- include InterpolationEnhancer
- end
- end
- #--end of require 'erubis/engine/enhanced'
- #--begin of require 'erubis/engine/optimized'
- ##
- ## $Release: 2.6.6 $
- ## copyright(c) 2006-2010 kuwata-lab.com all rights reserved.
- ##
- #--already included require 'erubis/engine/eruby'
- module Erubis
- module OptimizedGenerator
- include Generator
- def self.supported_properties() # :nodoc:
- return []
- end
- def init_generator(properties={})
- super
- @escapefunc ||= "Erubis::XmlHelper.escape_xml"
- @initialized = false
- @prev_is_expr = false
- end
- protected
- def escape_text(text)
- text.gsub(/['\\]/, '\\\\\&') # "'" => "\\'", '\\' => '\\\\'
- end
- def escaped_expr(code)
- @escapefunc ||= 'Erubis::XmlHelper.escape_xml'
- return "#{@escapefunc}(#{code})"
- end
- def switch_to_expr(src)
- return if @prev_is_expr
- @prev_is_expr = true
- src << ' _buf'
- end
- def switch_to_stmt(src)
- return unless @prev_is_expr
- @prev_is_expr = false
- src << ';'
- end
- def add_preamble(src)
- #@initialized = false
- #@prev_is_expr = false
- end
- def add_text(src, text)
- return if text.empty?
- if @initialized
- switch_to_expr(src)
- src << " << '" << escape_text(text) << "'"
- else
- src << "_buf = '" << escape_text(text) << "';"
- @initialized = true
- end
- end
- def add_stmt(src, code)
- switch_to_stmt(src) if @initialized
- #super
- src << code
- src << ';' unless code[-1] == ?\n
- end
- def add_expr_literal(src, code)
- unless @initialized; src << "_buf = ''"; @initialized = true; end
- switch_to_expr(src)
- src << " << (" << code << ").to_s"
- end
- def add_expr_escaped(src, code)
- unless @initialized; src << "_buf = ''"; @initialized = true; end
- switch_to_expr(src)
- src << " << " << escaped_expr(code)
- end
- def add_expr_debug(src, code)
- code.strip!
- s = (code.dump =~ /\A"(.*)"\z/) && $1
- src << ' $stderr.puts("*** deb…
Large files files are truncated, but you can click here to view the full file