annotate tools/mdx_urlize.py @ 731:796103a2215b

Fix messages master checkbox behavior. See also 6ba401391cd5.
author Brian Neal <bgneal@gmail.com>
date Wed, 09 Oct 2013 20:52:48 -0500
parents 2adf01661ac5
children
rev   line source
bgneal@658 1 """A more liberal autolinker
bgneal@356 2
bgneal@356 3 Inspired by Django's urlize function.
bgneal@356 4
bgneal@356 5 Positive examples:
bgneal@356 6
bgneal@356 7 >>> import markdown
bgneal@356 8 >>> md = markdown.Markdown(extensions=['urlize'])
bgneal@356 9
bgneal@356 10 >>> md.convert('http://example.com/')
bgneal@356 11 u'<p><a href="http://example.com/">http://example.com/</a></p>'
bgneal@356 12
bgneal@356 13 >>> md.convert('go to http://example.com')
bgneal@356 14 u'<p>go to <a href="http://example.com">http://example.com</a></p>'
bgneal@356 15
bgneal@356 16 >>> md.convert('example.com')
bgneal@356 17 u'<p><a href="http://example.com">example.com</a></p>'
bgneal@356 18
bgneal@356 19 >>> md.convert('example.net')
bgneal@356 20 u'<p><a href="http://example.net">example.net</a></p>'
bgneal@356 21
bgneal@356 22 >>> md.convert('www.example.us')
bgneal@356 23 u'<p><a href="http://www.example.us">www.example.us</a></p>'
bgneal@356 24
bgneal@356 25 >>> md.convert('(www.example.us/path/?name=val)')
bgneal@356 26 u'<p>(<a href="http://www.example.us/path/?name=val">www.example.us/path/?name=val</a>)</p>'
bgneal@356 27
bgneal@356 28 >>> md.convert('go to <http://example.com> now!')
bgneal@356 29 u'<p>go to <a href="http://example.com">http://example.com</a> now!</p>'
bgneal@356 30
bgneal@356 31 Negative examples:
bgneal@356 32
bgneal@356 33 >>> md.convert('del.icio.us')
bgneal@356 34 u'<p>del.icio.us</p>'
bgneal@356 35
bgneal@356 36 """
bgneal@356 37
bgneal@356 38 import markdown
bgneal@356 39
bgneal@356 40 # Global Vars
bgneal@356 41 URLIZE_RE = '(%s)' % '|'.join([
bgneal@356 42 r'<(?:f|ht)tps?://[^>]*>',
bgneal@356 43 r'\b(?:f|ht)tps?://[^)<>\s]+[^.,)<>\s]',
bgneal@356 44 r'\bwww\.[^)<>\s]+[^.,)<>\s]',
bgneal@356 45 r'[^(<\s]+\.(?:com|net|org)\b',
bgneal@356 46 ])
bgneal@356 47
bgneal@356 48 class UrlizePattern(markdown.inlinepatterns.Pattern):
bgneal@356 49 """ Return a link Element given an autolink (`http://example/com`). """
bgneal@356 50 def handleMatch(self, m):
bgneal@356 51 url = m.group(2)
bgneal@356 52
bgneal@356 53 if url.startswith('<'):
bgneal@356 54 url = url[1:-1]
bgneal@356 55
bgneal@356 56 text = url
bgneal@356 57
bgneal@356 58 if not url.split('://')[0] in ('http','https','ftp'):
bgneal@356 59 if '@' in url and not '/' in url:
bgneal@356 60 url = 'mailto:' + url
bgneal@356 61 else:
bgneal@356 62 url = 'http://' + url
bgneal@356 63
bgneal@658 64 el = markdown.util.etree.Element("a")
bgneal@356 65 el.set('href', url)
bgneal@658 66 el.text = markdown.util.AtomicString(text)
bgneal@356 67 return el
bgneal@356 68
bgneal@356 69 class UrlizeExtension(markdown.Extension):
bgneal@356 70 """ Urlize Extension for Python-Markdown. """
bgneal@356 71
bgneal@356 72 def extendMarkdown(self, md, md_globals):
bgneal@356 73 """ Replace autolink with UrlizePattern """
bgneal@356 74 md.inlinePatterns['autolink'] = UrlizePattern(URLIZE_RE, md)
bgneal@356 75
bgneal@356 76 def makeExtension(configs=None):
bgneal@356 77 return UrlizeExtension(configs=configs)
bgneal@356 78
bgneal@356 79 if __name__ == "__main__":
bgneal@356 80 import doctest
bgneal@356 81 doctest.testmod()