Auto Link Plain Text URLs in Python

14 Aug 2009

I seem to always write this python script to automatically convert urls found in a paragraph of text into clickable link tags. I am posting it now so I can easily find it later.

Why is this so ugly in python?

import re

def auto_link(self, text):
      """Make links for urls in plain text. Ported from http://snippets.dzone.com/posts/show/7455"""
      generic_URL_regexp = re.compile( r'(?P^|[\n ])(?P[\w]+?://[\w]+[^ \"\n\r\t<]*)', re.I | re.M )
      starts_with_www_regexp = re.compile( r'(?P^|[\n ])(?P(www)\.[^ \"\t\n\r<]*)', re.I | re.M )
      starts_with_ftp_regexp = re.compile( r'(?P^|[\n ])(?P(ftp)\.[^ \"\t\n\r<]*)', re.I | re.M )
      email_regexp = re.compile( r'(?P^|[\n ])(?P[a-z0-9&\-_\.]+?)@(?P[\w\-]+\.([\w\-\.]+\.)*[\w]+)', re.I )
      
      s = text or ''
      s = str(s)
      
      s = generic_URL_regexp.subn(r'\g<a href="\g">\g', s)[0]        
      s = starts_with_www_regexp.subn(r'\g<a href="http://\g">\g', s)[0]
      s = starts_with_ftp_regexp.subn(r'\g<a href="ftp://\g">\g', s)[0]
      s = email_regexp.subn(r'\g<a href="mailto:\g@\g">\g@\g', s)[0]
      return s