comparison core/mdexts/urlize.py @ 848:32ebe22f0cad

For issue #79 update to Markdown 2.5.1. This commit follows new Markdown conventions for extensions.
author Brian Neal <bgneal@gmail.com>
date Tue, 28 Oct 2014 19:33:14 -0500
parents tools/mdx_urlize.py@2adf01661ac5
children
comparison
equal deleted inserted replaced
847:ba9216950eb1 848:32ebe22f0cad
1 """
2 This is a Python Markdown extension to automatically urlize text.
3 Originally found here:
4
5 https://github.com/r0wb0t/markdown-urlize
6
7 Modified by Brian Neal to update doctest for Python Markdown 2.5.x (extension
8 parameter changes).
9
10 Copyright (c) 2014 Rowan Nairn
11 All rights reserved.
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are
14 met:
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 A more liberal autolinker
33
34 Inspired by Django's urlize function.
35
36 Positive examples:
37
38 >>> import markdown
39 >>> md = markdown.Markdown(extensions=[UrlizeExtension()])
40
41 >>> md.convert('http://example.com/')
42 u'<p><a href="http://example.com/">http://example.com/</a></p>'
43
44 >>> md.convert('go to http://example.com')
45 u'<p>go to <a href="http://example.com">http://example.com</a></p>'
46
47 >>> md.convert('example.com')
48 u'<p><a href="http://example.com">example.com</a></p>'
49
50 >>> md.convert('example.net')
51 u'<p><a href="http://example.net">example.net</a></p>'
52
53 >>> md.convert('www.example.us')
54 u'<p><a href="http://www.example.us">www.example.us</a></p>'
55
56 >>> md.convert('(www.example.us/path/?name=val)')
57 u'<p>(<a href="http://www.example.us/path/?name=val">www.example.us/path/?name=val</a>)</p>'
58
59 >>> md.convert('go to <http://example.com> now!')
60 u'<p>go to <a href="http://example.com">http://example.com</a> now!</p>'
61
62 Negative examples:
63
64 >>> md.convert('del.icio.us')
65 u'<p>del.icio.us</p>'
66
67 """
68
69 import markdown
70
71 # Global Vars
72 URLIZE_RE = '(%s)' % '|'.join([
73 r'<(?:f|ht)tps?://[^>]*>',
74 r'\b(?:f|ht)tps?://[^)<>\s]+[^.,)<>\s]',
75 r'\bwww\.[^)<>\s]+[^.,)<>\s]',
76 r'[^(<\s]+\.(?:com|net|org)\b',
77 ])
78
79 class UrlizePattern(markdown.inlinepatterns.Pattern):
80 """ Return a link Element given an autolink (`http://example/com`). """
81 def handleMatch(self, m):
82 url = m.group(2)
83
84 if url.startswith('<'):
85 url = url[1:-1]
86
87 text = url
88
89 if not url.split('://')[0] in ('http','https','ftp'):
90 if '@' in url and not '/' in url:
91 url = 'mailto:' + url
92 else:
93 url = 'http://' + url
94
95 el = markdown.util.etree.Element("a")
96 el.set('href', url)
97 el.text = markdown.util.AtomicString(text)
98 return el
99
100 class UrlizeExtension(markdown.Extension):
101 """ Urlize Extension for Python-Markdown. """
102
103 def extendMarkdown(self, md, md_globals):
104 """ Replace autolink with UrlizePattern """
105 md.inlinePatterns['autolink'] = UrlizePattern(URLIZE_RE, md)
106
107 def makeExtension(configs=None):
108 return UrlizeExtension(configs=configs)
109
110 if __name__ == "__main__":
111 import doctest
112 doctest.testmod()