made linking more robust, this fixes #732

This commit is contained in:
MrZYX 2010-12-19 12:45:10 +01:00
parent bb0375d2f8
commit 1b6f341348
2 changed files with 19 additions and 3 deletions

View file

@ -190,12 +190,23 @@ module ApplicationHelper
def process_links(message)
message.gsub!(/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|(&quot[^;]))+)\"\)/) do |m|
escape = "\\"
res = "<a target=\"#{escape}_blank\" href=\"#{$2}\" title=\"#{$3}\">#{$1}</a>"
link = $1
url = $2
title = $3
url.gsub!("_", "\\_")
url.gsub!("*", "\\*")
protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
res = "<a target=\"#{escape}_blank\" href=\"#{protocol}#{url}\" title=\"#{title}\">#{link}</a>"
res
end
message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/) do |m|
escape = "\\"
res = "<a target=\"#{escape}_blank\" href=\"#{$2}\">#{$1}</a>"
link = $1
url = $2
url.gsub!("_", "\\_")
url.gsub!("*", "\\*")
protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
res = "<a target=\"#{escape}_blank\" href=\"#{protocol}#{url}\">#{link}</a>"
res
end

View file

@ -166,12 +166,17 @@ describe ApplicationHelper do
message = '[link text](http://someurl.com "some title") [link text](http://someurl.com "some title")'
markdownify(message).should == '<a target="_blank" href="http://someurl.com" title="some title">link text</a> <a target="_blank" href="http://someurl.com" title="some title">link text</a>'
end
it "should have a robust link parsing" do
message = "This [*text*](http://en.wikipedia.org/wiki/Text_(literary_theory)) with many [links](google.com) tests [_http_](http://google.com/search?q=with_multiple__underscores*and**asterisks), [___FTP___](ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4 \"File Transfer Protocol\"), [**any protocol**](foo://bar.example.org/yes_it*makes*no_sense)"
markdownify(message).should == 'This <a target="_blank" href="http://en.wikipedia.org/wiki/Text_(literary_theory)"><em>text</em></a> with many <a target="_blank" href="http://google.com">links</a> tests <a target="_blank" href="http://google.com/search?q=with_multiple__underscores*and**asterisks"><em>http</em></a>, <a target="_blank" href="ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4" title="File Transfer Protocol"><em><strong>FTP</strong></em></a>, <a target="_blank" href="foo://bar.example.org/yes_it*makes*no_sense"><strong>any protocol</strong></a>'
end
end
describe "nested emphasis and links tags" do
it "should be rendered correctly" do
message = '[**some *link* text**](someurl.com "some title")'
markdownify(message).should == '<a target="_blank" href="someurl.com" title="some title"><strong>some <em>link</em> text</strong></a>'
markdownify(message).should == '<a target="_blank" href="http://someurl.com" title="some title"><strong>some <em>link</em> text</strong></a>'
end
end