add support for markdown emphasis in status messages
This commit is contained in:
parent
b913444f2a
commit
112088ace3
2 changed files with 69 additions and 5 deletions
|
|
@ -18,14 +18,38 @@ module StatusMessagesHelper
|
|||
|
||||
# next line is important due to XSS! (h is rail's make_html_safe-function)
|
||||
message = h(message).html_safe
|
||||
message.gsub!(/( |^)(www\.[^ ]+\.[^ ])/, '\1http://\2')
|
||||
message.gsub!(/( |^)http:\/\/www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/, '\1youtube.com::\2')
|
||||
message.gsub!(/(https|http|ftp):\/\/([^ ]+)/, '<a target="_blank" href="\1://\2">\2</a>')
|
||||
|
||||
while youtube = message.match(/youtube\.com::([A-Za-z0-9_]+)/)
|
||||
|
||||
message.gsub!(/( |^)(www\.[^ ]+\.[^ ])/) do |m|
|
||||
res = "#{$1}http://#{$2}"
|
||||
res.gsub!(/^(\*|_)$/) { |m| "\\#{$1}" }
|
||||
res
|
||||
end
|
||||
message.gsub!(/( |^)http:\/\/www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/) do |m|
|
||||
res = "#{$1}youtube.com::#{$2}"
|
||||
res.gsub!(/(\*|_)/) { |m| "\\#{$1}" }
|
||||
res
|
||||
end
|
||||
message.gsub!(/(https|http|ftp):\/\/([^ ]+)/) do |m|
|
||||
res = %{<a target="_blank" href="#{$1}://#{$2}">#{$2}</a>}
|
||||
res.gsub!(/(\*|_)/) { |m| "\\#{$1}" }
|
||||
res
|
||||
end
|
||||
|
||||
# markdown
|
||||
message.gsub!(/([^\\]|^)\*\*(([^*]|([^*]\*[^*]))*[^\\])\*\*/, '\1<strong>\2</strong>')
|
||||
message.gsub!(/([^\\]|^)__(([^_]|([^_]_[^_]))*[^\\])__/, '\1<strong>\2</strong>')
|
||||
message.gsub!(/([^\\]|^)\*([^*]*[^\\])\*/, '\1<em>\2</em>')
|
||||
message.gsub!(/([^\\]|^)_([^_]*[^\\])_/, '\1<em>\2</em>')
|
||||
message.gsub!(/([^\\]|^)\*/, '\1')
|
||||
message.gsub!(/([^\\]|^)_/, '\1')
|
||||
message.gsub!("\\*", "*")
|
||||
message.gsub!("\\_", "_")
|
||||
|
||||
while youtube = message.match(/youtube\.com::([A-Za-z0-9_\\]+)/)
|
||||
videoid = youtube[1]
|
||||
message.gsub!('youtube.com::'+videoid, '<a onclick="openVideo(\'youtube.com\', \'' + videoid + '\', this)" href="#video">Youtube: ' + youtube_title(videoid) + '</a>')
|
||||
end
|
||||
|
||||
return message
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -68,5 +68,45 @@ describe StatusMessagesHelper do
|
|||
make_links(url).should == "<a target=\"_blank\" href=\"http://"+url+"\">"+url+"</a>"
|
||||
end
|
||||
|
||||
describe "markdown" do
|
||||
describe "weak emphasis" do
|
||||
it "should be recognized (1/2)" do
|
||||
message = "*some text* some text *some text* some text"
|
||||
make_links(message).should == "<em>some text</em> some text <em>some text</em> some text"
|
||||
end
|
||||
|
||||
it "should be recognized (2/2)" do
|
||||
message = "_some text_ some text _some text_ some text"
|
||||
make_links(message).should == "<em>some text</em> some text <em>some text</em> some text"
|
||||
end
|
||||
end
|
||||
|
||||
describe "strong emphasis" do
|
||||
it "should be recognized (1/2)" do
|
||||
message = "**some text** some text **some text** some text"
|
||||
make_links(message).should == "<strong>some text</strong> some text <strong>some text</strong> some text"
|
||||
end
|
||||
|
||||
it "should be recognized (2/2)" do
|
||||
message = "__some text__ some text __some text__ some text"
|
||||
make_links(message).should == "<strong>some text</strong> some text <strong>some text</strong> some text"
|
||||
end
|
||||
end
|
||||
|
||||
describe "imbricated weak and strong emphasis" do
|
||||
it "should be rendered correctly" do
|
||||
message = "__this is _some_ text__"
|
||||
make_links(message).should == "<strong>this is <em>some</em> text</strong>"
|
||||
message = "*this is **some** text*"
|
||||
make_links(message).should == "<em>this is <strong>some</strong> text</em>"
|
||||
end
|
||||
end
|
||||
|
||||
it "should allow escaping" do
|
||||
message = '*some text* \\*some text* \\**some text* _some text_ \\_some text_ \\__some text_'
|
||||
make_links(message).should == "<em>some text</em> *some text<em> *</em>some text <em>some text</em> _some text<em> _</em>some text"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue