Skip to content

Commit

Permalink
Allow extensions that gsub! the input string for backward compatibili…
Browse files Browse the repository at this point in the history
…ty. [#2 state:resolved]
  • Loading branch information
Jason Garber authored and Jason Garber committed Jun 19, 2008
1 parent 5a319ab commit 5512498
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/redcloth.rb
Expand Up @@ -80,7 +80,11 @@ def initialize( string, restrictions = [] )
super( string )
end

def to_html
def to_html( *rules )
rules.each do |r|
method(r).call(self) if self.respond_to?(r)
end

to(RedCloth::Formatters::HTML)
end

Expand Down
31 changes: 31 additions & 0 deletions test/test_extensions.rb
@@ -0,0 +1,31 @@
#!/usr/bin/env ruby

require 'helper'

class TestExtensions < Test::Unit::TestCase

require 'redcloth'

# http://www.ralree.info/2006/9/13/extending-redcloth
module RedClothSmileyExtension
def refs_smiley(text)
text.gsub!(/(\s)~(:P|:D|:O|:o|:S|:\||;\)|:'\(|:\)|:\()/) do |m|
bef,ma = $~[1..2]
filename = "/images/emoticons/"+(ma.split(//).collect{|l| l[0] }.join('_'))+".png"
"#{bef}<img src='#{filename}' title='#{ma}' class='smiley' />"
end
end
end

RedCloth.send(:include, RedClothSmileyExtension)

def test_smiley
input = %Q{You're so silly! ~:P}

str = RedCloth.new(input).to_html(:textile, :refs_smiley)

html = %Q{<p>You&#8217;re so silly! <img src='/images/emoticons/58_80.png' title=':P' class='smiley' /></p>}

assert_equal(html, str)
end
end

0 comments on commit 5512498

Please sign in to comment.