This commit is contained in:
Toks
2014-11-18 15:13:43 -05:00
parent e6348bcd29
commit e6e6b2fe1c
2 changed files with 25 additions and 10 deletions

View File

@@ -58,19 +58,24 @@ class DText
end
def self.parse_links(str)
str.gsub(/("[^"]+":(https?:\/\/|\/)[^\s\r\n<>]+|https?:\/\/[^\s\r\n<>]+)+/) do |url|
if url =~ /^"([^"]+)":(.+)$/
str.gsub(/("[^"]+":(https?:\/\/|\/)[^\s\r\n<>]+|https?:\/\/[^\s\r\n<>]+|"[^"]+":\[(https?:\/\/|\/)[^\s\r\n<>\]]+\])+/) do |url|
ch = ""
if url =~ /^"([^"]+)":\[(.+)\]$/
text = $1
url = $2
else
text = url
end
if url =~ /^"([^"]+)":(.+)$/
text = $1
url = $2
else
text = url
end
if url =~ /([;,.!?\)\]<>])$/
url.chop!
ch = $1
else
ch = ""
if url =~ /([;,.!?\)\]<>])$/
url.chop!
ch = $1
end
end
'<a href="' + url + '">' + text + '</a>' + ch

View File

@@ -105,7 +105,7 @@ class DTextTest < ActiveSupport::TestCase
assert_equal('<p>a (<a href="http://test.com">http://test.com</a>) b</p>', p('a (http://test.com) b'))
end
def test_old_syle_links
def test_old_style_links
assert_equal('<p><a href="http://test.com">test</a></p>', p('"test":http://test.com'))
end
@@ -113,6 +113,16 @@ class DTextTest < ActiveSupport::TestCase
assert_equal('<p>"1" <a href="http://three.com">2 &amp; 3</a></p>', p('"1" "2 & 3":http://three.com'))
end
def test_new_style_links
assert_equal('<p><a href="http://test.com">test</a></p>', p('"test":[http://test.com]'))
end
def test_new_style_links_with_parentheses
assert_equal('<p><a href="http://test.com/(parentheses)">test</a></p>', p('"test":[http://test.com/(parentheses)]'))
assert_equal('<p>(<a href="http://test.com/(parentheses)">test</a>)</p>', p('("test":[http://test.com/(parentheses)])'))
assert_equal('<p>[<a href="http://test.com/(parentheses)">test</a>]</p>', p('["test":[http://test.com/(parentheses)]]'))
end
def test_lists_1
assert_equal('<ul><li>a</li></ul>', p('* a'))
end