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 ++++++++++++++++++++-- test/functional/basic.rb | 23 ++++++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) 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 diff --git a/test/functional/basic.rb b/test/functional/basic.rb index 2880c77..9862f77 100755 --- a/test/functional/basic.rb +++ b/test/functional/basic.rb @@ -37,8 +37,29 @@ class BasicTest < Test::Unit::TestCase def test_for_redcloth require 'rubygems' require 'coderay/for_redcloth' - assert_equal '

puts "Hello, World!"

', + assert_equal '

puts "Hello, World!"

', RedCloth.new('@[ruby]puts "Hello, World!"@').to_html + assert_equal <<-BLOCKCODE.chomp, +
+
puts "Hello, World!"
+
+ +BLOCKCODE + RedCloth.new('bc[ruby]. puts "Hello, World!"').to_html + end + + def test_for_redcloth_escapes + require 'rubygems' + require 'coderay/for_redcloth' + assert_equal '

>

', + RedCloth.new('@[ruby]>@').to_html + assert_equal <<-BLOCKCODE.chomp, +
+
&
+
+ +BLOCKCODE + RedCloth.new('bc[ruby]. &').to_html end ENCODERS_LIST = %w( -- cgit v1.2.1