Published in: Other
Expand |
Embed | Plain Text
# list of most common tlds for pattern matching, add more if needed TLDS = /com|net|org|ly|biz|asia|edu|gov|int|mobi|jobs|name|pro|mil|tel|travel|gd/ # Returns text with arbitrary links properly linked given that the content matches the list of TLDS def linkify(content) return content if content.match(TLDS).nil? # strip the http:// content.gsub!("http://","") new_content = content.split(" ").each do |lnk| # render the right type of link email or http link = lnk.match(/@/) ? "mailto:#{lnk}" : "http://#{lnk}" lnk_string = "<a href='#{link}' title='#{link}'>#{link.gsub('mailto:','')}</a>" lnk.gsub!(lnk, lnk_string) unless lnk.match(TLDS).nil? end return new_content.join(" ") end
You need to login to post a comment.
