annotate tools/mdx_urlize.py @ 631:f36d1a168be7

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