Created
March 13, 2011 21:13
-
-
Save fguillen/868449 to your computer and use it in GitHub Desktop.
active all the detected links in text block (for Ruby)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Not work very well but enough for prototiping | |
# TODO: not very clear url expression | |
URL_REGEX = /([\S]+\.[\S]+)/ | |
def self.activate_links( text ) | |
text.gsub!( URL_REGEX ) do |url| | |
clean_url = url.gsub( /http:\/\//i, '' ) | |
"<a href=\"http://#{clean_url}\">#{url}</a>" | |
end | |
return text | |
end | |
# test | |
def test_activate_links | |
assert_equal( | |
'text with <a href="http://link.com">link.com</a> and more text.', | |
activate_links( 'text with link.com and more text.' ) | |
) | |
assert_equal( | |
'text with <a href="http://link.com/Image.jpg">link.com/Image.jpg</a> and more text.', | |
activate_links( 'text with link.com/Image.jpg and more text.' ) | |
) | |
assert_equal( | |
'text with <a href="http://link.com">http://link.com</a> and more text.', | |
activate_links( 'text with http://link.com and more text.' ) | |
) | |
assert_equal( | |
'text with <a href="http://link.com">link.com</a> and <a href="http://another.link.com.">another.link.com.</a>', | |
activate_links( 'text with link.com and another.link.com.' ) | |
) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment