Auto Link Plain Text URLs in Python
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
Possibly related posts:
- Install Python Enabled Vim on OS X
- Do a Simple Python Error Check (with pyflakes) Every Time You Save in Textmate
- Send To Tumblr And Track With Bitly Google Reader Custom Link
You should follow me on Twitter here.
Subscribe to labs via email here.
About this entry
You’re currently reading “Auto Link Plain Text URLs in Python,” an entry on experiments and essays
- Published:
- 8.14.09 / 2pm
- Category:
- work

Comments
Jump to comment form | comments rss [?]