Mercurial > public > sg101
changeset 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 | ba9216950eb1 |
children | ff645a692791 |
files | core/markup.py core/mdexts/__init__.py core/mdexts/deleted.py core/mdexts/urlize.py tools/mdx_del.py tools/mdx_urlize.py |
diffstat | 5 files changed, 139 insertions(+), 101 deletions(-) [+] |
line wrap: on
line diff
--- a/core/markup.py Mon Oct 27 18:53:07 2014 -0500 +++ b/core/markup.py Tue Oct 28 19:33:14 2014 -0500 @@ -1,10 +1,13 @@ """ Markup related utitlities useful for the entire project. + """ import markdown as _markdown from django.utils.encoding import force_unicode from smiley import SmilifyMarkdown +from core.mdexts.urlize import UrlizeExtension +from core.mdexts.deleted import DelExtension class Markdown(object): """ @@ -13,7 +16,11 @@ """ def __init__(self, safe_mode='escape'): self.md = _markdown.Markdown(safe_mode=safe_mode, - extensions=['urlize', 'nl2br', 'del']) + extensions=[ + UrlizeExtension(), + 'markdown.extensions.nl2br', + DelExtension(), + ]) def convert(self, s): return self.md.convert(force_unicode(s))
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/mdexts/deleted.py Tue Oct 28 19:33:14 2014 -0500 @@ -0,0 +1,19 @@ +""" +A python-markdown extension to add support for <del>. +The Markdown syntax is --this is deleted text--. + +""" +import markdown + + +DEL_RE = r'(---)(.*?)---' + +class DelExtension(markdown.Extension): + + def extendMarkdown(self, md, md_globals): + del_tag = markdown.inlinepatterns.SimpleTagPattern(DEL_RE, 'del') + md.inlinePatterns.add('del', del_tag, '>not_strong') + + +def makeExtension(configs=None): + return DelExtension(configs)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/mdexts/urlize.py Tue Oct 28 19:33:14 2014 -0500 @@ -0,0 +1,112 @@ +""" +This is a Python Markdown extension to automatically urlize text. +Originally found here: + +https://github.com/r0wb0t/markdown-urlize + +Modified by Brian Neal to update doctest for Python Markdown 2.5.x (extension +parameter changes). + +Copyright (c) 2014 Rowan Nairn +All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +A more liberal autolinker + +Inspired by Django's urlize function. + +Positive examples: + +>>> import markdown +>>> md = markdown.Markdown(extensions=[UrlizeExtension()]) + +>>> md.convert('http://example.com/') +u'<p><a href="http://example.com/">http://example.com/</a></p>' + +>>> md.convert('go to http://example.com') +u'<p>go to <a href="http://example.com">http://example.com</a></p>' + +>>> md.convert('example.com') +u'<p><a href="http://example.com">example.com</a></p>' + +>>> md.convert('example.net') +u'<p><a href="http://example.net">example.net</a></p>' + +>>> md.convert('www.example.us') +u'<p><a href="http://www.example.us">www.example.us</a></p>' + +>>> md.convert('(www.example.us/path/?name=val)') +u'<p>(<a href="http://www.example.us/path/?name=val">www.example.us/path/?name=val</a>)</p>' + +>>> md.convert('go to <http://example.com> now!') +u'<p>go to <a href="http://example.com">http://example.com</a> now!</p>' + +Negative examples: + +>>> md.convert('del.icio.us') +u'<p>del.icio.us</p>' + +""" + +import markdown + +# Global Vars +URLIZE_RE = '(%s)' % '|'.join([ + r'<(?:f|ht)tps?://[^>]*>', + r'\b(?:f|ht)tps?://[^)<>\s]+[^.,)<>\s]', + r'\bwww\.[^)<>\s]+[^.,)<>\s]', + 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: + 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. """ + + def extendMarkdown(self, md, md_globals): + """ Replace autolink with UrlizePattern """ + md.inlinePatterns['autolink'] = UrlizePattern(URLIZE_RE, md) + +def makeExtension(configs=None): + return UrlizeExtension(configs=configs) + +if __name__ == "__main__": + import doctest + doctest.testmod()
--- a/tools/mdx_del.py Mon Oct 27 18:53:07 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,19 +0,0 @@ -""" -A python-markdown extension to add support for <del>. -The Markdown syntax is --this is deleted text--. - -""" -import markdown - - -DEL_RE = r'(---)(.*?)---' - -class DelExtension(markdown.Extension): - - def extendMarkdown(self, md, md_globals): - del_tag = markdown.inlinepatterns.SimpleTagPattern(DEL_RE, 'del') - md.inlinePatterns.add('del', del_tag, '>not_strong') - - -def makeExtension(configs=None): - return DelExtension(configs)
--- a/tools/mdx_urlize.py Mon Oct 27 18:53:07 2014 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +0,0 @@ -"""A more liberal autolinker - -Inspired by Django's urlize function. - -Positive examples: - ->>> import markdown ->>> md = markdown.Markdown(extensions=['urlize']) - ->>> md.convert('http://example.com/') -u'<p><a href="http://example.com/">http://example.com/</a></p>' - ->>> md.convert('go to http://example.com') -u'<p>go to <a href="http://example.com">http://example.com</a></p>' - ->>> md.convert('example.com') -u'<p><a href="http://example.com">example.com</a></p>' - ->>> md.convert('example.net') -u'<p><a href="http://example.net">example.net</a></p>' - ->>> md.convert('www.example.us') -u'<p><a href="http://www.example.us">www.example.us</a></p>' - ->>> md.convert('(www.example.us/path/?name=val)') -u'<p>(<a href="http://www.example.us/path/?name=val">www.example.us/path/?name=val</a>)</p>' - ->>> md.convert('go to <http://example.com> now!') -u'<p>go to <a href="http://example.com">http://example.com</a> now!</p>' - -Negative examples: - ->>> md.convert('del.icio.us') -u'<p>del.icio.us</p>' - -""" - -import markdown - -# Global Vars -URLIZE_RE = '(%s)' % '|'.join([ - r'<(?:f|ht)tps?://[^>]*>', - r'\b(?:f|ht)tps?://[^)<>\s]+[^.,)<>\s]', - r'\bwww\.[^)<>\s]+[^.,)<>\s]', - 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: - 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. """ - - def extendMarkdown(self, md, md_globals): - """ Replace autolink with UrlizePattern """ - md.inlinePatterns['autolink'] = UrlizePattern(URLIZE_RE, md) - -def makeExtension(configs=None): - return UrlizeExtension(configs=configs) - -if __name__ == "__main__": - import doctest - doctest.testmod()