Merge remote branch 'ed0h/336-markdown-for-status-messages'

Conflicts:
	pkg/ubuntu/dist
This commit is contained in:
Raphael 2010-11-02 11:45:16 -07:00
commit 2b128d88ec
2 changed files with 89 additions and 5 deletions

View file

@ -18,14 +18,44 @@ 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!(/\[([^\[]+)\]\(([^ ]+) \&quot;(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|(&quot[^;]))+)\&quot;\)/, '<a href="\2" title="\3">\1</a>')
message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/, '<a href="\2">\1</a>')
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!(/(<a href=")?(https|http|ftp):\/\/([^ ]+)/) do |m|
if $1 == '<a href="'
m
else
res = %{<a target="_blank" href="#{$2}://#{$3}">#{$3}</a>}
res.gsub!(/(\*|_)/) { |m| "\\#{$1}" }
res
end
end
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

View file

@ -68,5 +68,59 @@ 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>"
message = "___some text___"
make_links(message).should == "<em><strong>some text</strong></em>"
end
end
describe "links" do
it "should be recognized without title attribute" do
message = "[link text](http://someurl.com) [link text](http://someurl.com)"
make_links(message).should == '<a href="http://someurl.com">link text</a> <a href="http://someurl.com">link text</a>'
end
it "should be recognized with title attribute" do
message = '[link text](http://someurl.com "some title") [link text](http://someurl.com "some title")'
make_links(message).should == '<a href="http://someurl.com" title="some title">link text</a> <a href="http://someurl.com" title="some title">link text</a>'
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