diff --git a/ChangeLog b/ChangeLog index c15ca5a889..32136aea00 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed Dec 5 00:05:47 2012 Masatoshi SEKI + + * lib/erb.rb (make_compiler, add_put_cmd, add_insert_cmd): extract + methods. + Tue Dec 4 18:21:04 2012 Naohisa Goto * test/ruby/memory_status.rb (Memory): use fiddle/types if available. diff --git a/lib/erb.rb b/lib/erb.rb index 934d83aa48..b2d6d36f13 100644 --- a/lib/erb.rb +++ b/lib/erb.rb @@ -583,6 +583,14 @@ class ERB end end + def add_put_cmd(out, content) + out.push("#{@put_cmd} #{content_dump(content)}") + end + + def add_insert_cmd(out, content) + out.push("#{@insert_cmd}((#{content}).to_s)") + end + # Compiles an ERB template into Ruby code. Returns an array of the code # and encoding like ["code", Encoding]. def compile(s) @@ -600,7 +608,7 @@ class ERB if scanner.stag.nil? case token when PercentLine - out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0 + add_put_cmd(out, content) if content.size > 0 content = '' out.push(token.to_s) out.cr @@ -608,11 +616,11 @@ class ERB out.cr when '<%', '<%=', '<%#' scanner.stag = token - out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0 + add_put_cmd(out, content) if content.size > 0 content = '' when "\n" content << "\n" - out.push("#{@put_cmd} #{content_dump(content)}") + add_put_cmd(out, content) content = '' when '<%%' content << '<%' @@ -632,7 +640,7 @@ class ERB out.push(content) end when '<%=' - out.push("#{@insert_cmd}((#{content}).to_s)") + add_insert_cmd(out, content) when '<%#' # out.push("# #{content_dump(content)}") end @@ -645,7 +653,7 @@ class ERB end end end - out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0 + add_put_cmd(out, content) if content.size > 0 out.close return out.script, enc end @@ -785,12 +793,16 @@ class ERB # def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout') @safe_level = safe_level - compiler = ERB::Compiler.new(trim_mode) + compiler = make_compiler(trim_mode) set_eoutvar(compiler, eoutvar) @src, @enc = *compiler.compile(str) @filename = nil end + def make_compiler(trim_mode) + ERB::Compiler.new(trim_mode) + end + # The Ruby code generated by ERB attr_reader :src @@ -806,16 +818,8 @@ class ERB def set_eoutvar(compiler, eoutvar = '_erbout') compiler.put_cmd = "#{eoutvar}.concat" compiler.insert_cmd = "#{eoutvar}.concat" - - cmd = [] - cmd.push "#{eoutvar} = ''" - - compiler.pre_cmd = cmd - - cmd = [] - cmd.push("#{eoutvar}.force_encoding(__ENCODING__)") - - compiler.post_cmd = cmd + compiler.pre_cmd = ["#{eoutvar} = ''"] + compiler.post_cmd = ["#{eoutvar}.force_encoding(__ENCODING__)"] end # Generate results and print them. (see ERB#result)