summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2008-08-28 06:32:34 +0000
committermurphy <murphy@rubychan.de>2008-08-28 06:32:34 +0000
commit28fac2774ecfe248013f02c43ca78b90d0b20b23 (patch)
treed2ca96ed5aa8d4f3bff58f629a3aea841f3babe1
parent03ffb9431e140e09e5f1a7ae6c2d582c1895554e (diff)
downloadcoderay-28fac2774ecfe248013f02c43ca78b90d0b20b23.tar.gz
Fixed coderay/for_redcloth escaping.
-rw-r--r--lib/coderay/for_redcloth.rb22
-rwxr-xr-xtest/functional/basic.rb23
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 = {
+ '&amp;' => '&',
+ '&quot;' => '"',
+ '&gt;' => '>',
+ '&lt;' => '<',
+ }
+ 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
"<code#{pba(opts)}>#{opts[:text]}</code>"
end
@@ -40,6 +51,13 @@ module CodeRay # :nodoc:
@in_bc = nil
opts[:lang] ? '' : "</pre>\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 '<p><span lang="ruby" class="CodeRay">puts <span style="background-color:#fff0f0"><span style="color:#710">&quot;</span><span style="color:#D20">Hello, World!</span><span style="color:#710">&quot;</span></span></span></p>',
+ assert_equal '<p><span lang="ruby" class="CodeRay">puts <span style="background-color:#fff0f0"><span style="color:#710">"</span><span style="color:#D20">Hello, World!</span><span style="color:#710">"</span></span></span></p>',
RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
+ assert_equal <<-BLOCKCODE.chomp,
+<div lang="ruby" class="CodeRay">
+ <div class="code"><pre>puts <span style="background-color:#fff0f0"><span style="color:#710">&quot;</span><span style="color:#D20">Hello, World!</span><span style="color:#710">&quot;</span></span></pre></div>
+</div>
+</pre>
+BLOCKCODE
+ RedCloth.new('bc[ruby]. puts "Hello, World!"').to_html
+ end
+
+ def test_for_redcloth_escapes
+ require 'rubygems'
+ require 'coderay/for_redcloth'
+ assert_equal '<p><span lang="ruby" class="CodeRay">&gt;</span></p>',
+ RedCloth.new('@[ruby]>@').to_html
+ assert_equal <<-BLOCKCODE.chomp,
+<div lang="ruby" class="CodeRay">
+ <div class="code"><pre>&amp;</pre></div>
+</div>
+</pre>
+BLOCKCODE
+ RedCloth.new('bc[ruby]. &').to_html
end
ENCODERS_LIST = %w(