This is a little bit more readable, while still being somewhat compact.
It also gets plain email addresses:
class String
def hyperlink
# define url parts:
chars = Regexp.escape “/!#$\%&'()*+,.:;=?@~-"
url = “https?:\/\/[#{chars}[:alnum:]]+”
email = "(?:mailto:)?[^\s@]+@[[:alnum:].-]+\.[[:alnum:]._-]+”
# do the substitution:
gsub(/#{url}|#{email}/) do |link|
if link =~ /[1]+:/
%Q(#{link})
else
%Q(#{link})
end
end
end
end
Note that this will not get all email addresses, and it will let a
some urls spill through. But it should get most stuff. Getting it exact
would require more lines of code than I want to look at tonight
···
On Feb 15, 2004, at 1:27 AM, Ruby Baby wrote:
Ugly:
class String
def hyperlink
gsub(/(http://|https://|mailto:)([[:alnum:]/!#$%&'()+,.:;=?
@~-]+)([[:alnum:]/!#$%&'()+:;=?@~-])/, ‘\1\2\3’)
end
end
… but it generally works, from my few tests.
Anyone got a better version, or care to improve on this one?
Just trying to hyperlink URLs in text (and in future, email addresses,
without needing mailto:).