From 28fac2774ecfe248013f02c43ca78b90d0b20b23 Mon Sep 17 00:00:00 2001 From: murphy Date: Thu, 28 Aug 2008 06:32:34 +0000 Subject: Fixed coderay/for_redcloth escaping. --- lib/coderay/for_redcloth.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'lib/coderay') diff --git a/lib/coderay/for_redcloth.rb b/lib/coderay/for_redcloth.rb index dd8d22f..e2f13d1 100644 --- a/lib/coderay/for_redcloth.rb +++ b/lib/coderay/for_redcloth.rb @@ -18,7 +18,16 @@ module CodeRay # :nodoc: raise 'CodeRay.for_redcloth needs RedCloth 4.0.3 or later.' unless RedCloth::VERSION.to_s >= '4.0.3' RedCloth::TextileDoc.send :include, ForRedCloth::TextileDoc RedCloth::Formatters::HTML.module_eval do - undef_method :code, :bc_open, :bc_close + def unescape(html) + replacements = { + '&' => '&', + '"' => '"', + '>' => '>', + '<' => '<', + } + html.gsub(/&(?:amp|quot|[gl]t);/) { |entity| replacements[entity] } + end + undef_method :code, :bc_open, :bc_close, :escape_pre def code(opts) # :nodoc: opts[:block] = true if opts[:lang] && !filter_coderay @@ -26,7 +35,9 @@ module CodeRay # :nodoc: @in_bc ||= nil format = @in_bc ? :div : :span highlighted_code = CodeRay.encode opts[:text], opts[:lang], format, :stream => true - highlighted_code.sub(/\A<(span|div)/) { |m| m + pba(@in_bc || opts) } + highlighted_code.sub!(/\A<(span|div)/) { |m| m + pba(@in_bc || opts) } + highlighted_code = unescape(highlighted_code) unless @in_bc + highlighted_code else "#{opts[:text]}" end @@ -40,6 +51,13 @@ module CodeRay # :nodoc: @in_bc = nil opts[:lang] ? '' : "\n" end + def escape_pre(text) + if @in_bc ||= nil + text + else + html_esc(text, :html_escape_preformatted) + end + end end end -- cgit v1.2.1