general search and replace in markdownify (#1042)

This commit is contained in:
MrZYX 2011-05-03 17:42:02 +02:00
parent c20cd5381e
commit a92b28b4bf
2 changed files with 33 additions and 9 deletions

View file

@ -169,21 +169,17 @@ module ApplicationHelper
def markdownify(message, options = {})
message = h(message).html_safe
if !options.has_key?(:newlines)
options[:newlines] = true
end
options[:newlines] = true if !options.has_key?(:newlines)
options[:emoticons] = true if !options.has_key?(:emoticons)
message = process_links(message)
message = process_autolinks(message)
message = process_emphasis(message)
message = process_youtube(message, options[:youtube_maps])
message = process_vimeo(message, options[:vimeo_maps])
message = process_emoticons(message) if options[:emoticons]
message.gsub!(/<3/, "♥")
if options[:newlines]
message.gsub!(/\n+/, '<br />')
end
message.gsub!(/\n+/, '<br />') if options[:newlines]
return message
end
@ -279,6 +275,24 @@ module ApplicationHelper
return processed_message
end
def process_emoticons(message)
map = {
"&lt;3" => "&hearts;",
":(" => "&#9785;",
":-(" => "&#9785;",
":)" => "&#9786;",
":-)" => "&#9786;",
"-&gt;" => "&rarr;",
"&lt;-" => "&larr;",
"..." => "&hellip;"
}
map.each do |search, replace|
message.gsub!(search, replace)
end
message
end
def info_text(text)
image_tag 'icons/monotone_question.png', :class => 'what_is_this', :title => text
end

View file

@ -161,11 +161,21 @@ describe ApplicationHelper do
end
end
describe "hearts" do
describe "emoticons" do
it "replaces &lt;3 with &hearts;" do
message = "i <3 you"
markdownify(message).should == "i &hearts; you"
end
it "replaces various things with (their) HTML entities" do
message = ":) :-) :( :-( ... -> <-"
markdownify(message).should == "&#9786; &#9786; &#9785; &#9785; &hellip; &rarr; &larr;"
end
it "skips doing it if you say so" do
message = ":) :-) :( :-( ... -> <-"
markdownify(message, :emoticons => false).should == ":) :-) :( :-( ... -&gt; &lt;-"
end
end
describe "weak emphasis" do