Skip to content

Instantly share code, notes, and snippets.

@ubermajestix
Created January 13, 2009 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ubermajestix/46550 to your computer and use it in GitHub Desktop.
Save ubermajestix/46550 to your computer and use it in GitHub Desktop.
def add_url_markup(text)
urls = []
users =[]
# find urls
text.split.each{|a| a=~/(http\:\/\/\S+)/; urls << $1}
# find twitter usernames (@someusername)
text.split.each{|a| a=~/(\@\S+)/; users << $1}
# add markup
users.compact.each{|user| text.gsub!(user, "<a href='http://twitter.com/#{user.gsub('@','')}'>#{user}</a>")}
urls.compact.each{|url| text.gsub!(url, "<a href='#{url}'>#{url}</a>")}
text
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment