From c7046bc705c2653c3c2b988a84222a91d69805e9 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sun, 26 Apr 2020 15:52:07 +0200 Subject: [PATCH] fixed markdown urlize --- cookbook/helper/mdx_urlize.py | 81 ++++++++++++++++++++++++++++ cookbook/templates/recipe_view.html | 2 +- cookbook/templatetags/custom_tags.py | 3 +- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 cookbook/helper/mdx_urlize.py diff --git a/cookbook/helper/mdx_urlize.py b/cookbook/helper/mdx_urlize.py new file mode 100644 index 00000000..7df06430 --- /dev/null +++ b/cookbook/helper/mdx_urlize.py @@ -0,0 +1,81 @@ +"""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'

http://example.com/

' + +>>> md.convert('go to http://example.com') +u'

go to http://example.com

' + +>>> md.convert('example.com') +u'

example.com

' + +>>> md.convert('example.net') +u'

example.net

' + +>>> md.convert('www.example.us') +u'

www.example.us

' + +>>> md.convert('(www.example.us/path/?name=val)') +u'

(www.example.us/path/?name=val)

' + +>>> md.convert('go to now!') +u'

go to http://example.com now!

' + +Negative examples: + +>>> md.convert('del.icio.us') +u'

del.icio.us

' + +""" + +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(*args, **kwargs): + return UrlizeExtension(*args, **kwargs) + +if __name__ == "__main__": + import doctest + doctest.testmod() diff --git a/cookbook/templates/recipe_view.html b/cookbook/templates/recipe_view.html index eff5a7f1..6467244e 100644 --- a/cookbook/templates/recipe_view.html +++ b/cookbook/templates/recipe_view.html @@ -202,7 +202,7 @@
{% if recipe.instructions %} - {{ recipe.instructions | urlize | markdown | safe }} + {{ recipe.instructions | markdown | safe }} {% endif %}
diff --git a/cookbook/templatetags/custom_tags.py b/cookbook/templatetags/custom_tags.py index e4b3764e..9f96792a 100644 --- a/cookbook/templatetags/custom_tags.py +++ b/cookbook/templatetags/custom_tags.py @@ -5,6 +5,7 @@ from bleach_whitelist import markdown_tags, markdown_attrs, all_styles, print_at from django.urls import reverse from cookbook.helper.mdx_attributes import MarkdownFormatExtension +from cookbook.helper.mdx_urlize import UrlizeExtension from cookbook.models import get_model_name register = template.Library() @@ -28,5 +29,5 @@ def delete_url(model, pk): @register.filter() def markdown(value): tags = markdown_tags + ['pre', 'table', 'td', 'tr', 'th', 'tbody', 'style', 'thead'] - parsed_md = md.markdown(value, extensions=['markdown.extensions.fenced_code', 'tables', MarkdownFormatExtension()]) + parsed_md = md.markdown(value, extensions=['markdown.extensions.fenced_code', 'tables', UrlizeExtension(), MarkdownFormatExtension()]) return bleach.clean(parsed_md, tags, markdown_attrs)