Skip to content

Instantly share code, notes, and snippets.

@seki
Created November 28, 2010 14:41
Show Gist options
  • Save seki/718984 to your computer and use it in GitHub Desktop.
Save seki/718984 to your computer and use it in GitHub Desktop.
require 'erb'
class ERB
class ERBString < String
def to_s; self; end
def erb_concat(s)
if self.class === s
concat(s)
else
concat(erb_quote(s))
end
end
def erb_quote(s); s; end
end
end
class ERB4Html < ERB
def self.quoted(s)
HtmlString.new(s)
end
class HtmlString < ERB::ERBString
def erb_quote(s)
ERB::Util::html_escape(s)
end
end
def set_eoutvar(compiler, eoutvar = '_erbout')
compiler.put_cmd = "#{eoutvar}.concat"
compiler.insert_cmd = "#{eoutvar}.erb_concat"
compiler.pre_cmd = ["#{eoutvar} = ERB4Html.quoted('')"]
compiler.post_cmd = [eoutvar]
end
end
if __FILE__ == $0
str = '<%= str %>'
puts str
erb = ERB4Html.new(str)
str = erb.result(binding)
puts str
puts erb.result(binding)
str = String.new(str)
puts erb.result(binding)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment