2002-11-17 19:11:40 +03:00
|
|
|
# Tiny eRuby --- ERB2
|
2003-01-20 14:32:19 +03:00
|
|
|
# Copyright (c) 1999-2000,2002,2003 Masatoshi SEKI
|
2002-11-17 19:11:40 +03:00
|
|
|
# You can redistribute it and/or modify it under the same terms as Ruby.
|
|
|
|
|
|
|
|
class ERB
|
|
|
|
Revision = '$Date$' #'
|
|
|
|
|
|
|
|
def self.version
|
2003-07-23 13:47:16 +04:00
|
|
|
"erb.rb [2.0.4 #{ERB::Revision.split[1]}]"
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# ERB::Compiler
|
|
|
|
class ERB
|
|
|
|
class Compiler
|
2003-07-26 02:03:52 +04:00
|
|
|
class PercentLine
|
|
|
|
def initialize(str)
|
|
|
|
@value = str
|
|
|
|
end
|
|
|
|
attr_reader :value
|
|
|
|
alias :to_s :value
|
|
|
|
end
|
|
|
|
|
2003-07-23 13:47:16 +04:00
|
|
|
class Scanner
|
|
|
|
SplitRegexp = /(<%%)|(%%>)|(<%=)|(<%#)|(<%)|(%>)|(\n)/
|
|
|
|
|
2003-07-31 07:42:17 +04:00
|
|
|
@scanner_map = {}
|
|
|
|
def self.regist_scanner(klass, trim_mode, percent)
|
|
|
|
@scanner_map[[trim_mode, percent]] = klass
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default_scanner=(klass)
|
|
|
|
@default_scanner = klass
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.make_scanner(src, trim_mode, percent)
|
|
|
|
klass = @scanner_map.fetch([trim_mode, percent], @default_scanner)
|
|
|
|
klass.new(src, trim_mode, percent)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(src, trim_mode, percent)
|
2003-07-23 13:47:16 +04:00
|
|
|
@src = src
|
|
|
|
@stag = nil
|
2003-01-20 14:32:19 +03:00
|
|
|
end
|
2003-07-23 13:47:16 +04:00
|
|
|
attr_accessor :stag
|
|
|
|
|
|
|
|
def scan; end
|
|
|
|
end
|
2003-01-20 14:32:19 +03:00
|
|
|
|
2003-07-23 13:47:16 +04:00
|
|
|
class TrimScanner < Scanner
|
2003-07-29 18:35:42 +04:00
|
|
|
TrimSplitRegexp = /(<%%)|(%%>)|(<%=)|(<%#)|(<%)|(%>\n)|(%>)|(\n)/
|
|
|
|
|
2003-07-26 02:03:52 +04:00
|
|
|
def initialize(src, trim_mode, percent)
|
2003-07-31 07:42:17 +04:00
|
|
|
super
|
2003-07-26 02:03:52 +04:00
|
|
|
@trim_mode = trim_mode
|
|
|
|
@percent = percent
|
2003-07-31 07:42:17 +04:00
|
|
|
if @trim_mode == '>'
|
|
|
|
@scan_line = self.method(:trim_line1)
|
|
|
|
elsif @trim_mode == '<>'
|
|
|
|
@scan_line = self.method(:trim_line2)
|
|
|
|
elsif @trim_mode == '-'
|
|
|
|
@scan_line = self.method(:explicit_trim_line)
|
2003-01-20 14:32:19 +03:00
|
|
|
else
|
2003-07-23 13:47:16 +04:00
|
|
|
@scan_line = self.method(:scan_line)
|
2003-01-20 14:32:19 +03:00
|
|
|
end
|
|
|
|
end
|
2003-07-23 13:47:16 +04:00
|
|
|
attr_accessor :stag
|
2003-01-20 14:32:19 +03:00
|
|
|
|
2003-07-23 13:47:16 +04:00
|
|
|
def scan(&block)
|
|
|
|
@stag = nil
|
|
|
|
if @percent
|
|
|
|
@src.each do |line|
|
|
|
|
percent_line(line, &block)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@src.each do |line|
|
2003-07-31 07:42:17 +04:00
|
|
|
@scan_line.call(line, &block)
|
2003-07-23 13:47:16 +04:00
|
|
|
end
|
2003-01-20 14:32:19 +03:00
|
|
|
end
|
2003-07-23 13:47:16 +04:00
|
|
|
nil
|
2003-01-20 14:32:19 +03:00
|
|
|
end
|
|
|
|
|
2003-07-23 13:47:16 +04:00
|
|
|
def percent_line(line, &block)
|
|
|
|
if @stag || line[0] != ?%
|
|
|
|
return @scan_line.call(line, &block)
|
|
|
|
end
|
2003-01-20 14:32:19 +03:00
|
|
|
|
2003-07-23 13:47:16 +04:00
|
|
|
line[0] = ''
|
|
|
|
if line[0] == ?%
|
|
|
|
@scan_line.call(line, &block)
|
2002-11-17 19:11:40 +03:00
|
|
|
else
|
2003-07-26 02:03:52 +04:00
|
|
|
yield(PercentLine.new(line.chomp))
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
2003-07-23 13:47:16 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def scan_line(line)
|
|
|
|
line.split(SplitRegexp).each do |token|
|
|
|
|
next if token.empty?
|
|
|
|
yield(token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-07-31 07:42:17 +04:00
|
|
|
def trim_line1(line)
|
|
|
|
line.split(TrimSplitRegexp).each do |token|
|
|
|
|
next if token.empty?
|
|
|
|
if token == "%>\n"
|
|
|
|
yield('%>')
|
|
|
|
yield(:cr)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
yield(token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def trim_line2(line)
|
2003-07-23 13:47:16 +04:00
|
|
|
head = nil
|
2003-07-29 18:35:42 +04:00
|
|
|
line.split(TrimSplitRegexp).each do |token|
|
2003-07-23 13:47:16 +04:00
|
|
|
next if token.empty?
|
|
|
|
head = token unless head
|
2003-07-29 18:35:42 +04:00
|
|
|
if token == "%>\n"
|
|
|
|
yield('%>')
|
2003-07-31 07:42:17 +04:00
|
|
|
if is_erb_stag?(head)
|
2003-07-29 18:35:42 +04:00
|
|
|
yield(:cr)
|
|
|
|
else
|
|
|
|
yield("\n")
|
|
|
|
end
|
2003-07-23 13:47:16 +04:00
|
|
|
break
|
|
|
|
end
|
|
|
|
yield(token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-07-31 18:49:19 +04:00
|
|
|
ExplicitTrimRegexp = /(^[ \t]*<%-)|(-%>\n?\z)|(<%-)|(-%>)|(<%%)|(%%>)|(<%=)|(<%#)|(<%)|(%>)|(\n)/
|
2003-07-31 07:42:17 +04:00
|
|
|
def explicit_trim_line(line)
|
|
|
|
line.split(ExplicitTrimRegexp).each do |token|
|
|
|
|
next if token.empty?
|
|
|
|
if @stag.nil? && /[ \t]*<%-/ =~ token
|
|
|
|
yield('<%')
|
|
|
|
elsif @stag && /-%>\n/ =~ token
|
|
|
|
yield('%>')
|
|
|
|
yield(:cr)
|
|
|
|
elsif @stag && token == '-%>'
|
|
|
|
yield('%>')
|
|
|
|
else
|
|
|
|
yield(token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-07-23 13:47:16 +04:00
|
|
|
ERB_STAG = %w(<%= <%# <%)
|
|
|
|
def is_erb_stag?(s)
|
|
|
|
ERB_STAG.member?(s)
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-07-31 07:42:17 +04:00
|
|
|
Scanner.default_scanner = TrimScanner
|
|
|
|
|
2003-07-23 13:47:16 +04:00
|
|
|
class SimpleScanner < Scanner
|
|
|
|
def scan
|
|
|
|
@src.each do |line|
|
|
|
|
line.split(SplitRegexp).each do |token|
|
|
|
|
next if token.empty?
|
|
|
|
yield(token)
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2003-07-31 07:42:17 +04:00
|
|
|
|
|
|
|
Scanner.regist_scanner(SimpleScanner, nil, false)
|
|
|
|
|
|
|
|
begin
|
|
|
|
require 'strscan'
|
|
|
|
class SimpleScanner2 < Scanner
|
|
|
|
def scan
|
2003-07-31 18:49:19 +04:00
|
|
|
stag_reg = /(.*?)(<%%|<%=|<%#|<%|\n|\z)/
|
|
|
|
etag_reg = /(.*?)(%%>|%>|\n|\z)/
|
2003-07-31 07:42:17 +04:00
|
|
|
scanner = StringScanner.new(@src)
|
|
|
|
while ! scanner.eos?
|
|
|
|
scanner.scan(@stag ? etag_reg : stag_reg)
|
|
|
|
text = scanner[1]
|
|
|
|
elem = scanner[2]
|
|
|
|
yield(text) unless text.empty?
|
|
|
|
yield(elem) unless elem.empty?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Scanner.regist_scanner(SimpleScanner2, nil, false)
|
|
|
|
|
|
|
|
class PercentScanner < Scanner
|
|
|
|
def scan
|
|
|
|
new_line = true
|
2003-07-31 18:49:19 +04:00
|
|
|
stag_reg = /(.*?)(<%%|<%=|<%#|<%|\n|\z)/
|
|
|
|
etag_reg = /(.*?)(%%>|%>|\n|\z)/
|
2003-07-31 07:42:17 +04:00
|
|
|
scanner = StringScanner.new(@src)
|
|
|
|
while ! scanner.eos?
|
|
|
|
if new_line && @stag.nil?
|
|
|
|
if scanner.scan(/%%/)
|
|
|
|
yield('%')
|
|
|
|
new_line = false
|
|
|
|
next
|
|
|
|
elsif scanner.scan(/%/)
|
2003-07-31 18:49:19 +04:00
|
|
|
yield(PercentLine.new(scanner.scan(/.*?(\n|\z)/).chomp))
|
2003-07-31 07:42:17 +04:00
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
scanner.scan(@stag ? etag_reg : stag_reg)
|
|
|
|
text = scanner[1]
|
|
|
|
elem = scanner[2]
|
|
|
|
yield(text) unless text.empty?
|
|
|
|
yield(elem) unless elem.empty?
|
|
|
|
new_line = (elem == "\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Scanner.regist_scanner(PercentScanner, nil, true)
|
|
|
|
|
|
|
|
class ExplicitScanner < Scanner
|
|
|
|
def scan
|
|
|
|
new_line = true
|
2003-07-31 18:49:19 +04:00
|
|
|
stag_reg = /(.*?)(<%%|<%=|<%#|<%-|<%|\n|\z)/
|
|
|
|
etag_reg = /(.*?)(%%>|-%>|%>|\n|\z)/
|
2003-07-31 07:42:17 +04:00
|
|
|
scanner = StringScanner.new(@src)
|
|
|
|
while ! scanner.eos?
|
|
|
|
if new_line && @stag.nil? && scanner.scan(/[ \t]*<%-/)
|
|
|
|
yield('<%')
|
|
|
|
new_line = false
|
|
|
|
next
|
|
|
|
end
|
|
|
|
scanner.scan(@stag ? etag_reg : stag_reg)
|
|
|
|
text = scanner[1]
|
|
|
|
elem = scanner[2]
|
|
|
|
new_line = (elem == "\n")
|
|
|
|
yield(text) unless text.empty?
|
|
|
|
if elem == '-%>'
|
|
|
|
yield('%>')
|
2003-07-31 18:49:19 +04:00
|
|
|
if scanner.scan(/(\n|\z)/)
|
2003-07-31 07:42:17 +04:00
|
|
|
yield(:cr)
|
|
|
|
new_line = true
|
|
|
|
end
|
|
|
|
elsif elem == '<%-'
|
|
|
|
yield('<%')
|
|
|
|
else
|
|
|
|
yield(elem) unless elem.empty?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Scanner.regist_scanner(ExplicitScanner, '-', false)
|
|
|
|
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2002-11-17 19:11:40 +03:00
|
|
|
|
2003-07-23 13:47:16 +04:00
|
|
|
class Buffer
|
|
|
|
def initialize(compiler)
|
|
|
|
@compiler = compiler
|
|
|
|
@line = []
|
|
|
|
@script = ""
|
|
|
|
@compiler.pre_cmd.each do |x|
|
|
|
|
push(x)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
attr_reader :script
|
|
|
|
|
|
|
|
def push(cmd)
|
|
|
|
@line << cmd
|
|
|
|
end
|
|
|
|
|
|
|
|
def cr
|
|
|
|
@script << (@line.join('; '))
|
|
|
|
@line = []
|
|
|
|
@script << "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
return unless @line
|
|
|
|
@compiler.post_cmd.each do |x|
|
|
|
|
push(x)
|
|
|
|
end
|
|
|
|
@script << (@line.join('; '))
|
|
|
|
@line = nil
|
2003-01-20 14:32:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2002-11-17 19:11:40 +03:00
|
|
|
def compile(s)
|
2003-07-23 13:47:16 +04:00
|
|
|
out = Buffer.new(self)
|
|
|
|
|
|
|
|
content = ''
|
|
|
|
scanner = make_scanner(s)
|
|
|
|
scanner.scan do |token|
|
|
|
|
if scanner.stag.nil?
|
|
|
|
case token
|
2003-07-26 02:03:52 +04:00
|
|
|
when PercentLine
|
|
|
|
out.push("#{@put_cmd} #{content.dump}") if content.size > 0
|
|
|
|
content = ''
|
|
|
|
out.push(token.to_s)
|
|
|
|
out.cr
|
2003-07-29 18:35:42 +04:00
|
|
|
when :cr
|
|
|
|
out.cr
|
2003-07-23 13:47:16 +04:00
|
|
|
when '<%', '<%=', '<%#'
|
|
|
|
scanner.stag = token
|
|
|
|
out.push("#{@put_cmd} #{content.dump}") if content.size > 0
|
|
|
|
content = ''
|
|
|
|
when "\n"
|
|
|
|
content << "\n"
|
|
|
|
out.push("#{@put_cmd} #{content.dump}")
|
|
|
|
out.cr
|
|
|
|
content = ''
|
|
|
|
when '<%%'
|
|
|
|
content << '<%'
|
2002-11-17 19:11:40 +03:00
|
|
|
else
|
2003-07-23 13:47:16 +04:00
|
|
|
content << token
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
|
|
|
else
|
2003-07-23 13:47:16 +04:00
|
|
|
case token
|
|
|
|
when '%>'
|
|
|
|
case scanner.stag
|
2002-11-17 19:11:40 +03:00
|
|
|
when '<%'
|
2003-07-23 13:47:16 +04:00
|
|
|
if content[-1] == ?\n
|
|
|
|
content.chop!
|
|
|
|
out.push(content)
|
|
|
|
out.cr
|
2002-11-17 19:11:40 +03:00
|
|
|
else
|
2003-07-23 13:47:16 +04:00
|
|
|
out.push(content)
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
|
|
|
when '<%='
|
2003-07-23 13:47:16 +04:00
|
|
|
out.push("#{@put_cmd}((#{content}).to_s)")
|
2002-11-17 19:11:40 +03:00
|
|
|
when '<%#'
|
2003-07-23 13:47:16 +04:00
|
|
|
# out.push("# #{content.dump}")
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
2003-07-23 13:47:16 +04:00
|
|
|
scanner.stag = nil
|
|
|
|
content = ''
|
|
|
|
when '%%>'
|
|
|
|
content << '%>'
|
2002-11-17 19:11:40 +03:00
|
|
|
else
|
2003-07-23 13:47:16 +04:00
|
|
|
content << token
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2003-07-23 13:47:16 +04:00
|
|
|
out.push("#{@put_cmd} #{content.dump}") if content.size > 0
|
|
|
|
out.close
|
|
|
|
out.script
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_trim_mode(mode)
|
|
|
|
case mode
|
|
|
|
when 1
|
|
|
|
return [false, '>']
|
|
|
|
when 2
|
|
|
|
return [false, '<>']
|
|
|
|
when 0
|
|
|
|
return [false, nil]
|
|
|
|
when String
|
|
|
|
perc = mode.include?('%')
|
2003-07-31 07:42:17 +04:00
|
|
|
if mode.include?('-')
|
|
|
|
return [perc, '-']
|
|
|
|
elsif mode.include?('<>')
|
2003-07-23 13:47:16 +04:00
|
|
|
return [perc, '<>']
|
|
|
|
elsif mode.include?('>')
|
|
|
|
return [perc, '>']
|
2002-11-17 19:11:40 +03:00
|
|
|
else
|
2003-07-23 13:47:16 +04:00
|
|
|
[perc, nil]
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
2003-07-23 13:47:16 +04:00
|
|
|
else
|
|
|
|
return [false, nil]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_scanner(src)
|
2003-07-31 07:42:17 +04:00
|
|
|
Scanner.make_scanner(src, @trim_mode, @percent)
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
|
|
|
|
2003-01-20 14:32:19 +03:00
|
|
|
def initialize(trim_mode)
|
2003-07-23 13:47:16 +04:00
|
|
|
@percent, @trim_mode = prepare_trim_mode(trim_mode)
|
2002-11-17 19:11:40 +03:00
|
|
|
@put_cmd = 'print'
|
|
|
|
@pre_cmd = []
|
|
|
|
@post_cmd = []
|
|
|
|
end
|
2003-07-23 13:47:16 +04:00
|
|
|
attr_reader :percent, :trim_mode
|
|
|
|
attr_accessor :put_cmd, :pre_cmd, :post_cmd
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# ERB
|
|
|
|
class ERB
|
|
|
|
def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
|
|
|
|
@safe_level = safe_level
|
2003-01-20 14:32:19 +03:00
|
|
|
compiler = ERB::Compiler.new(trim_mode)
|
2002-11-17 19:11:40 +03:00
|
|
|
set_eoutvar(compiler, eoutvar)
|
|
|
|
@src = compiler.compile(str)
|
2003-12-10 12:07:52 +03:00
|
|
|
@filename = nil
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
2003-12-10 12:07:52 +03:00
|
|
|
attr_reader :src
|
|
|
|
attr_accessor :filename
|
2002-11-17 19:11:40 +03:00
|
|
|
|
|
|
|
def set_eoutvar(compiler, eoutvar = '_erbout')
|
|
|
|
compiler.put_cmd = "#{eoutvar}.concat"
|
|
|
|
|
|
|
|
cmd = []
|
|
|
|
cmd.push "#{eoutvar} = ''"
|
|
|
|
|
|
|
|
compiler.pre_cmd = cmd
|
|
|
|
|
|
|
|
cmd = []
|
|
|
|
cmd.push(eoutvar)
|
|
|
|
|
|
|
|
compiler.post_cmd = cmd
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(b=TOPLEVEL_BINDING)
|
|
|
|
print self.result(b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def result(b=TOPLEVEL_BINDING)
|
|
|
|
if @safe_level
|
|
|
|
th = Thread.start {
|
|
|
|
$SAFE = @safe_level
|
2004-02-28 04:52:14 +03:00
|
|
|
eval(@src, b, (@filename || '(erb)'), 1)
|
2002-11-17 19:11:40 +03:00
|
|
|
}
|
|
|
|
return th.value
|
|
|
|
else
|
2003-12-10 12:07:52 +03:00
|
|
|
return eval(@src, b, (@filename || '(erb)'), 1)
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_method(mod, methodname, fname='(ERB)')
|
|
|
|
mod.module_eval("def #{methodname}\n" + self.src + "\nend\n", fname, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_module(methodname='erb')
|
|
|
|
mod = Module.new
|
|
|
|
def_method(mod, methodname)
|
|
|
|
mod
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_class(superklass=Object, methodname='result')
|
2002-11-21 17:39:26 +03:00
|
|
|
cls = Class.new(superklass)
|
2002-11-17 19:11:40 +03:00
|
|
|
def_method(cls, methodname)
|
|
|
|
cls
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# ERB::Util
|
|
|
|
class ERB
|
|
|
|
module Util
|
|
|
|
public
|
|
|
|
def html_escape(s)
|
|
|
|
s.to_s.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<")
|
|
|
|
end
|
|
|
|
alias h html_escape
|
|
|
|
|
|
|
|
def url_encode(s)
|
|
|
|
s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
|
|
|
|
end
|
|
|
|
alias u url_encode
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# ERB::DefMethod
|
|
|
|
class ERB
|
|
|
|
module DefMethod
|
|
|
|
public
|
|
|
|
def def_erb_method(methodname, erb)
|
|
|
|
if erb.kind_of? String
|
|
|
|
fname = erb
|
|
|
|
File.open(fname) {|f| erb = ERB.new(f.read) }
|
2003-04-22 20:29:47 +04:00
|
|
|
erb.def_method(self, methodname, fname)
|
|
|
|
else
|
|
|
|
erb.def_method(self, methodname)
|
2002-11-17 19:11:40 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :def_erb_method
|
|
|
|
end
|
|
|
|
end
|