diff --git a/cookbook/helper/mdx_urlize.py b/cookbook/helper/mdx_urlize.py index 7df06430b..92bcc98be 100644 --- a/cookbook/helper/mdx_urlize.py +++ b/cookbook/helper/mdx_urlize.py @@ -1,4 +1,5 @@ -"""A more liberal autolinker +""" +A more liberal autolinker Inspired by Django's urlize function. @@ -45,27 +46,30 @@ URLIZE_RE = '(%s)' % '|'.join([ r'[^(<\s]+\.(?:com|net|org)\b', ]) + class UrlizePattern(markdown.inlinepatterns.Pattern): """ Return a link Element given an autolink (`http://example/com`). """ + def handleMatch(self, m): url = m.group(2) - + if url.startswith('<'): url = url[1:-1] - + text = url - - if not url.split('://')[0] in ('http','https','ftp'): - if '@' in url and not '/' in url: + + if not url.split('://')[0] in ('http', 'https', 'ftp'): + if '@' in url and '/' not in url: url = 'mailto:' + url else: url = 'http://' + url - + el = markdown.util.etree.Element("a") el.set('href', url) el.text = markdown.util.AtomicString(text) return el + class UrlizeExtension(markdown.Extension): """ Urlize Extension for Python-Markdown. """ @@ -73,9 +77,12 @@ class UrlizeExtension(markdown.Extension): """ Replace autolink with UrlizePattern """ md.inlinePatterns['autolink'] = UrlizePattern(URLIZE_RE, md) + def makeExtension(*args, **kwargs): return UrlizeExtension(*args, **kwargs) + if __name__ == "__main__": import doctest + doctest.testmod()