bgneal@658: """A more liberal autolinker bgneal@356: bgneal@356: Inspired by Django's urlize function. bgneal@356: bgneal@356: Positive examples: bgneal@356: bgneal@356: >>> import markdown bgneal@356: >>> md = markdown.Markdown(extensions=['urlize']) bgneal@356: bgneal@356: >>> md.convert('http://example.com/') bgneal@356: u'
' bgneal@356: bgneal@356: >>> md.convert('go to http://example.com') bgneal@356: u'go to http://example.com
' bgneal@356: bgneal@356: >>> md.convert('example.com') bgneal@356: u'' bgneal@356: bgneal@356: >>> md.convert('example.net') bgneal@356: u'' bgneal@356: bgneal@356: >>> md.convert('www.example.us') bgneal@356: u'' bgneal@356: bgneal@356: >>> md.convert('(www.example.us/path/?name=val)') bgneal@356: u'(www.example.us/path/?name=val)
' bgneal@356: bgneal@356: >>> md.convert('go togo to http://example.com now!
' bgneal@356: bgneal@356: Negative examples: bgneal@356: bgneal@356: >>> md.convert('del.icio.us') bgneal@356: u'del.icio.us
' bgneal@356: bgneal@356: """ bgneal@356: bgneal@356: import markdown bgneal@356: bgneal@356: # Global Vars bgneal@356: URLIZE_RE = '(%s)' % '|'.join([ bgneal@356: r'<(?:f|ht)tps?://[^>]*>', bgneal@356: r'\b(?:f|ht)tps?://[^)<>\s]+[^.,)<>\s]', bgneal@356: r'\bwww\.[^)<>\s]+[^.,)<>\s]', bgneal@356: r'[^(<\s]+\.(?:com|net|org)\b', bgneal@356: ]) bgneal@356: bgneal@356: class UrlizePattern(markdown.inlinepatterns.Pattern): bgneal@356: """ Return a link Element given an autolink (`http://example/com`). """ bgneal@356: def handleMatch(self, m): bgneal@356: url = m.group(2) bgneal@356: bgneal@356: if url.startswith('<'): bgneal@356: url = url[1:-1] bgneal@356: bgneal@356: text = url bgneal@356: bgneal@356: if not url.split('://')[0] in ('http','https','ftp'): bgneal@356: if '@' in url and not '/' in url: bgneal@356: url = 'mailto:' + url bgneal@356: else: bgneal@356: url = 'http://' + url bgneal@356: bgneal@658: el = markdown.util.etree.Element("a") bgneal@356: el.set('href', url) bgneal@658: el.text = markdown.util.AtomicString(text) bgneal@356: return el bgneal@356: bgneal@356: class UrlizeExtension(markdown.Extension): bgneal@356: """ Urlize Extension for Python-Markdown. """ bgneal@356: bgneal@356: def extendMarkdown(self, md, md_globals): bgneal@356: """ Replace autolink with UrlizePattern """ bgneal@356: md.inlinePatterns['autolink'] = UrlizePattern(URLIZE_RE, md) bgneal@356: bgneal@356: def makeExtension(configs=None): bgneal@356: return UrlizeExtension(configs=configs) bgneal@356: bgneal@356: if __name__ == "__main__": bgneal@356: import doctest bgneal@356: doctest.testmod()