mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
fix: % in <%..%>
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3372 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c1d00ec453
commit
9f22034ae0
1 changed files with 74 additions and 44 deletions
118
lib/erb.rb
118
lib/erb.rb
|
@ -1,24 +1,56 @@
|
||||||
# Tiny eRuby --- ERB2
|
# Tiny eRuby --- ERB2
|
||||||
# Copyright (c) 1999-2000,2002 Masatoshi SEKI
|
# Copyright (c) 1999-2000,2002,2003 Masatoshi SEKI
|
||||||
# You can redistribute it and/or modify it under the same terms as Ruby.
|
# You can redistribute it and/or modify it under the same terms as Ruby.
|
||||||
|
|
||||||
class ERB
|
class ERB
|
||||||
Revision = '$Date$' #'
|
Revision = '$Date$' #'
|
||||||
|
|
||||||
def self.version
|
def self.version
|
||||||
"erb.rb [2.0.1 #{ERB::Revision.split[1]}]"
|
"erb.rb [2.0.2 #{ERB::Revision.split[1]}]"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# ERB::Compiler
|
# ERB::Compiler
|
||||||
class ERB
|
class ERB
|
||||||
class Compiler
|
class Compiler
|
||||||
ERbTag = "<%% %%> <%= <%# <% %>".split
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
class ParcentLine
|
||||||
|
def initialize(compiler, str)
|
||||||
|
@compiler = compiler
|
||||||
|
@line = str
|
||||||
|
end
|
||||||
|
|
||||||
|
def expand(list)
|
||||||
|
str = @line.dup
|
||||||
|
str[0] = ''
|
||||||
|
if /^%%/ === @line
|
||||||
|
list.unshift("\n")
|
||||||
|
list.unshift(str)
|
||||||
|
else
|
||||||
|
list.unshift('%>')
|
||||||
|
list.unshift(str)
|
||||||
|
list.unshift('<%')
|
||||||
|
end
|
||||||
|
list
|
||||||
|
end
|
||||||
|
|
||||||
|
def expand_in_script(list)
|
||||||
|
ary = []
|
||||||
|
@compiler.push_line(ary, @line)
|
||||||
|
ary.reverse_each do |x|
|
||||||
|
list.unshift(x)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ERbTag = "<%% %%> <%= <%# <% %>".split
|
||||||
def is_erb_tag?(s)
|
def is_erb_tag?(s)
|
||||||
ERbTag.member?(s)
|
ERbTag.member?(s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
SplitRegexp = /(<%%)|(%%>)|(<%=)|(<%#)|(<%)|(%>)|(\n)/
|
||||||
|
|
||||||
def prepare_trim_mode(mode)
|
def prepare_trim_mode(mode)
|
||||||
case mode
|
case mode
|
||||||
when 1
|
when 1
|
||||||
|
@ -41,47 +73,41 @@ class ERB
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
SplitRegexp = /(<%%)|(%%>)|(<%=)|(<%#)|(<%)|(%>)|(\n)/
|
def pre_compile(s)
|
||||||
|
|
||||||
public
|
|
||||||
def pre_compile(s, trim_mode)
|
|
||||||
perc, trim_mode = prepare_trim_mode(trim_mode)
|
|
||||||
re = SplitRegexp
|
re = SplitRegexp
|
||||||
if trim_mode.nil? && !perc
|
if @trim_mode.nil? && !@perc
|
||||||
list = s.split(re)
|
list = s.split(re)
|
||||||
else
|
else
|
||||||
list = []
|
list = []
|
||||||
has_cr = (s[-1] == ?\n)
|
has_cr = (s[-1] == ?\n)
|
||||||
s.each do |line|
|
s.each do |line|
|
||||||
line = line.chomp
|
line = line.chomp
|
||||||
if perc && (/^(%{1,2})/ =~ line)
|
if @perc && (/^%/ =~ line)
|
||||||
line[0] = ''
|
list.push(ParcentLine.new(self, line))
|
||||||
if $1 == '%%'
|
|
||||||
list.push(line)
|
|
||||||
list.push("\n")
|
|
||||||
else
|
else
|
||||||
list.push('<%')
|
push_line(list, line)
|
||||||
list.push(line)
|
|
||||||
list.push('%>')
|
|
||||||
end
|
|
||||||
else
|
|
||||||
line = line.split(re)
|
|
||||||
line.shift if line[0]==''
|
|
||||||
list += line
|
|
||||||
unless ((trim_mode == '>' && line[-1] == '%>') ||
|
|
||||||
(trim_mode == '<>' && (is_erb_tag?(line[0])) &&
|
|
||||||
line[-1] == '%>'))
|
|
||||||
list.push("\n")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
list.pop if list[-1] == "\n" && !has_cr
|
||||||
list.pop unless has_cr
|
|
||||||
end
|
end
|
||||||
list
|
list
|
||||||
end
|
end
|
||||||
|
|
||||||
|
public
|
||||||
|
def push_line(list, line)
|
||||||
|
re = SplitRegexp
|
||||||
|
line = line.split(re)
|
||||||
|
line.shift if line[0]==''
|
||||||
|
list.concat(line)
|
||||||
|
unless ((@trim_mode == '>' && line[-1] == '%>') ||
|
||||||
|
(@trim_mode == '<>' && (is_erb_tag?(line[0])) &&
|
||||||
|
line[-1] == '%>'))
|
||||||
|
list.push("\n")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def compile(s)
|
def compile(s)
|
||||||
list = pre_compile(s, @trim_mode)
|
list = pre_compile(s)
|
||||||
cmd = []
|
cmd = []
|
||||||
cmd.concat(@pre_cmd)
|
cmd.concat(@pre_cmd)
|
||||||
|
|
||||||
|
@ -98,16 +124,19 @@ class ERB
|
||||||
if stag.nil?
|
if stag.nil?
|
||||||
if ['<%', '<%=', '<%#'].include?(token)
|
if ['<%', '<%=', '<%#'].include?(token)
|
||||||
stag = token
|
stag = token
|
||||||
str = content.join
|
str = content.join('')
|
||||||
if str.size > 0
|
if str.size > 0
|
||||||
cmd.push("#{@put_cmd} #{str.dump}")
|
cmd.push("#{@put_cmd} #{str.dump}")
|
||||||
end
|
end
|
||||||
content = []
|
content = []
|
||||||
elsif token == "\n"
|
elsif token == "\n"
|
||||||
content.push("\n")
|
content.push("\n")
|
||||||
cmd.push("#{@put_cmd} #{content.join.dump}")
|
cmd.push("#{@put_cmd} #{content.join('').dump}")
|
||||||
cmd.push(:cr)
|
cmd.push(:cr)
|
||||||
content = []
|
content = []
|
||||||
|
elsif ParcentLine === token
|
||||||
|
token.expand(list)
|
||||||
|
next
|
||||||
else
|
else
|
||||||
content.push(token)
|
content.push(token)
|
||||||
end
|
end
|
||||||
|
@ -115,7 +144,7 @@ class ERB
|
||||||
if token == '%>'
|
if token == '%>'
|
||||||
case stag
|
case stag
|
||||||
when '<%'
|
when '<%'
|
||||||
str = content.join
|
str = content.join('')
|
||||||
if str[-1] == ?\n
|
if str[-1] == ?\n
|
||||||
str.chop!
|
str.chop!
|
||||||
cmd.push(str)
|
cmd.push(str)
|
||||||
|
@ -124,19 +153,22 @@ class ERB
|
||||||
cmd.push(str)
|
cmd.push(str)
|
||||||
end
|
end
|
||||||
when '<%='
|
when '<%='
|
||||||
cmd.push("#{@put_cmd}((#{content.join}).to_s)")
|
cmd.push("#{@put_cmd}((#{content.join('')}).to_s)")
|
||||||
when '<%#'
|
when '<%#'
|
||||||
# cmd.push("# #{content.dump}")
|
# cmd.push("# #{content.dump}")
|
||||||
end
|
end
|
||||||
stag = nil
|
stag = nil
|
||||||
content = []
|
content = []
|
||||||
|
elsif ParcentLine === token
|
||||||
|
token.expand_in_script(list)
|
||||||
|
next
|
||||||
else
|
else
|
||||||
content.push(token)
|
content.push(token)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if content.size > 0
|
if content.size > 0
|
||||||
cmd.push("#{@put_cmd} #{content.join.dump}")
|
cmd.push("#{@put_cmd} #{content.join('').dump}")
|
||||||
end
|
end
|
||||||
cmd.push(:cr)
|
cmd.push(:cr)
|
||||||
cmd.concat(@post_cmd)
|
cmd.concat(@post_cmd)
|
||||||
|
@ -151,19 +183,19 @@ class ERB
|
||||||
ary.push('; ')
|
ary.push('; ')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ary.join
|
ary.join('')
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize
|
def initialize(trim_mode)
|
||||||
@trim_mode = nil
|
@perc, @trim_mode = prepare_trim_mode(trim_mode)
|
||||||
@put_cmd = 'print'
|
@put_cmd = 'print'
|
||||||
@pre_cmd = []
|
@pre_cmd = []
|
||||||
@post_cmd = []
|
@post_cmd = []
|
||||||
end
|
end
|
||||||
attr :trim_mode, true
|
|
||||||
attr :put_cmd, true
|
attr_accessor(:put_cmd)
|
||||||
attr :pre_cmd, true
|
attr_accessor(:pre_cmd)
|
||||||
attr :post_cmd, true
|
attr_accessor(:post_cmd)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -171,8 +203,7 @@ end
|
||||||
class ERB
|
class ERB
|
||||||
def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
|
def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
|
||||||
@safe_level = safe_level
|
@safe_level = safe_level
|
||||||
compiler = ERB::Compiler.new
|
compiler = ERB::Compiler.new(trim_mode)
|
||||||
compiler.trim_mode = trim_mode
|
|
||||||
set_eoutvar(compiler, eoutvar)
|
set_eoutvar(compiler, eoutvar)
|
||||||
@src = compiler.compile(str)
|
@src = compiler.compile(str)
|
||||||
end
|
end
|
||||||
|
@ -255,4 +286,3 @@ class ERB
|
||||||
module_function :def_erb_method
|
module_function :def_erb_method
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue