├── LICENSE ├── MANIFEST.in ├── README.md ├── mdx_urlize.py ├── setup.cfg └── setup.py /LICENSE: -------------------------------------------------------------------------------- 1 | This is a 2-clause BSD license (http://opensource.org/licenses/BSD-2-Clause) 2 | 3 | Copyright (c) 2014 Rowan Nairn 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are 8 | met: 9 | 10 | 1. Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | 13 | 2. Redistributions in binary form must reproduce the above copyright 14 | notice, this list of conditions and the following disclaimer in the 15 | documentation and/or other materials provided with the distribution. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include *.md LICENSE 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A more liberal autolink extension for python Markdown 2 | - inspired by Django's urlize function` 3 | 4 | Requires: http://pypi.python.org/pypi/Markdown 5 | 6 | To make this extension loadable by Mardown, just drop ``mdx_urlize.py`` into your PYTHONPATH or projects root. 7 | 8 | From http://stackoverflow.com/a/6553787/352452: 9 | 10 | Once installed, you can use it in a Django template like this: 11 | 12 | {{ value|markdown:"urlize" }} 13 | 14 | Or in Python code like this: 15 | 16 | import markdown 17 | md = markdown.Markdown(safe_mode=True, extensions=['urlize']) 18 | converted_text = md.convert(text) 19 | 20 | Here is the start of the [Markdown extension docs](http://pythonhosted.org/Markdown/extensions/index.html) in case you need more info. 21 | 22 | 23 | -------------------------------------------------------------------------------- /mdx_urlize.py: -------------------------------------------------------------------------------- 1 | """A more liberal autolinker 2 | 3 | Inspired by Django's urlize function. 4 | 5 | Positive examples: 6 | 7 | >>> import markdown 8 | >>> md = markdown.Markdown(extensions=['urlize']) 9 | 10 | >>> md.convert('http://example.com/') 11 | u'
' 12 | 13 | >>> md.convert('go to http://example.com') 14 | u'go to http://example.com
' 15 | 16 | >>> md.convert('example.com') 17 | u'' 18 | 19 | >>> md.convert('example.net') 20 | u'' 21 | 22 | >>> md.convert('www.example.us') 23 | u'' 24 | 25 | >>> md.convert('(www.example.us/path/?name=val)') 26 | u'(www.example.us/path/?name=val)
' 27 | 28 | >>> md.convert('go togo to http://example.com now!
' 30 | 31 | Negative examples: 32 | 33 | >>> md.convert('del.icio.us') 34 | u'del.icio.us
' 35 | 36 | """ 37 | 38 | import markdown 39 | 40 | # Global Vars 41 | URLIZE_RE = '(%s)' % '|'.join([ 42 | r'<(?:f|ht)tps?://[^>]*>', 43 | r'\b(?:f|ht)tps?://[^)<>\s]+[^.,)<>\s]', 44 | r'\bwww\.[^)<>\s]+[^.,)<>\s]', 45 | r'[^(<\s]+\.(?:com|net|org)\b', 46 | ]) 47 | 48 | class UrlizePattern(markdown.inlinepatterns.Pattern): 49 | """ Return a link Element given an autolink (`http://example/com`). """ 50 | def handleMatch(self, m): 51 | url = m.group(2) 52 | 53 | if url.startswith('<'): 54 | url = url[1:-1] 55 | 56 | text = url 57 | 58 | if not url.split('://')[0] in ('http','https','ftp'): 59 | if '@' in url and not '/' in url: 60 | url = 'mailto:' + url 61 | else: 62 | url = 'http://' + url 63 | 64 | el = markdown.util.etree.Element("a") 65 | el.set('href', url) 66 | el.text = markdown.util.AtomicString(text) 67 | return el 68 | 69 | class UrlizeExtension(markdown.Extension): 70 | """ Urlize Extension for Python-Markdown. """ 71 | 72 | def extendMarkdown(self, md, md_globals): 73 | """ Replace autolink with UrlizePattern """ 74 | md.inlinePatterns['autolink'] = UrlizePattern(URLIZE_RE, md) 75 | 76 | def makeExtension(*args, **kwargs): 77 | return UrlizeExtension(*args, **kwargs) 78 | 79 | if __name__ == "__main__": 80 | import doctest 81 | doctest.testmod() 82 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [wheel] 2 | universal = 1 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from setuptools import setup 4 | 5 | def read(*paths): 6 | """Build a file path from *paths* and return the contents.""" 7 | with open(os.path.join(*paths), 'r') as f: 8 | return f.read() 9 | 10 | setup( 11 | name='markdown-urlize', 12 | version='0.2.0', 13 | description='A more liberal autolink extension for python Markdown', 14 | long_description=(read('README.md')), 15 | url='https://github.com/r0wb0t/markdown-urlize', 16 | license='BSD', 17 | author='Rowan Nairn', 18 | author_email='rnairn@gmail.com', 19 | py_modules=['mdx_urlize'], 20 | include_package_data=True, 21 | classifiers=[ 22 | 'Development Status :: 5 - Production/Stable', 23 | 'Intended Audience :: Developers', 24 | 'Natural Language :: English', 25 | 'License :: OSI Approved :: BSD License', 26 | 'Operating System :: OS Independent', 27 | 'Programming Language :: Python', 28 | 'Programming Language :: Python :: 2', 29 | 'Programming Language :: Python :: 2.6', 30 | 'Programming Language :: Python :: 2.7', 31 | 'Programming Language :: Python :: 3', 32 | 'Programming Language :: Python :: 3.3', 33 | 'Topic :: Software Development :: Libraries :: Python Modules', 34 | ], 35 | install_requires=[ "markdown", ] 36 | ) 37 | --------------------------------------------------------------------------------