Posted By

vanne on 12/03/09


Tagged

textmate ruby


Versions (?)

linkify method


 / Published in: Other
 

  1. # list of most common tlds for pattern matching, add more if needed
  2. TLDS = /com|net|org|ly|biz|asia|edu|gov|int|mobi|jobs|name|pro|mil|tel|travel|gd/
  3.  
  4. # Returns text with arbitrary links properly linked given that the content matches the list of TLDS
  5. def linkify(content)
  6. return content if content.match(TLDS).nil?
  7. # strip the http://
  8. content.gsub!("http://","")
  9. new_content = content.split(" ").each do |lnk|
  10. # render the right type of link email or http
  11. link = lnk.match(/@/) ? "mailto:#{lnk}" : "http://#{lnk}"
  12. lnk_string = "<a href='#{link}' title='#{link}'>#{link.gsub('mailto:','')}</a>"
  13. lnk.gsub!(lnk, lnk_string) unless lnk.match(TLDS).nil?
  14. end
  15. return new_content.join(" ")
  16. end

Report this snippet  

You need to login to post a comment.