Skip to content

Commit

Permalink
fixed markdown urlize
Browse files Browse the repository at this point in the history
  • Loading branch information
vabene1111 committed Apr 26, 2020
1 parent 52946a8 commit c7046bc
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
81 changes: 81 additions & 0 deletions cookbook/helper/mdx_urlize.py
Original file line number Diff line number Diff line change
@@ -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'<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(*args, **kwargs):
return UrlizeExtension(*args, **kwargs)

if __name__ == "__main__":
import doctest
doctest.testmod()
2 changes: 1 addition & 1 deletion cookbook/templates/recipe_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ <h4 class="card-title">{% trans 'Ingredients' %}</h4>

<div style="font-size: large">
{% if recipe.instructions %}
{{ recipe.instructions | urlize | markdown | safe }}
{{ recipe.instructions | markdown | safe }}
{% endif %}
</div>

Expand Down
3 changes: 2 additions & 1 deletion cookbook/templatetags/custom_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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()
Expand All @@ -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)

0 comments on commit c7046bc

Please sign in to comment.