|
1 | 1 | # Copyright The IETF Trust 2007, All Rights Reserved |
2 | 2 |
|
3 | | -import re |
| 3 | +import bleach |
4 | 4 | import datetime |
| 5 | +import re |
| 6 | + |
5 | 7 | import types |
| 8 | + |
6 | 9 | from email.utils import parseaddr |
7 | 10 |
|
8 | 11 | from django import template |
9 | 12 | from django.conf import settings |
10 | 13 | from django.utils.html import escape |
11 | | -from django.template.defaultfilters import truncatewords_html, linebreaksbr, stringfilter, striptags, urlize |
| 14 | +from django.template.defaultfilters import truncatewords_html, linebreaksbr, stringfilter, striptags |
12 | 15 | from django.utils.safestring import mark_safe, SafeData |
13 | 16 | from django.utils.html import strip_tags |
14 | 17 |
|
@@ -367,7 +370,9 @@ def format_history_text(text, trunc_words=25): |
367 | 370 | def format_snippet(text, trunc_words=25): |
368 | 371 | # urlize if there aren't already links present |
369 | 372 | if not 'href=' in text: |
370 | | - text = urlize(text) |
| 373 | + # django's urlize() is buggy in at least Django 1.11; use |
| 374 | + # bleach.linkify instead |
| 375 | + text = bleach.linkify(text) |
371 | 376 | full = keep_spacing(collapsebr(linebreaksbr(mark_safe(sanitize_html(text))))) |
372 | 377 | snippet = truncatewords_html(full, trunc_words) |
373 | 378 | if snippet != full: |
@@ -473,29 +478,6 @@ def lower_allcaps(text): |
473 | 478 | result = result.replace(token, token.lower()) |
474 | 479 | return result |
475 | 480 |
|
476 | | -# See https://djangosnippets.org/snippets/2072/ and |
477 | | -# https://stackoverflow.com/questions/9939248/how-to-prevent-django-basic-inlines-from-autoescaping |
478 | | -@register.filter |
479 | | -def urlize_html(html, autoescape=False): |
480 | | - """ |
481 | | - Returns urls found in an (X)HTML text node element as urls via Django urlize filter. |
482 | | - """ |
483 | | - try: |
484 | | - from BeautifulSoup import BeautifulSoup |
485 | | - except ImportError: |
486 | | - if settings.DEBUG: |
487 | | - raise template.TemplateSyntaxError, "Error in urlize_html The Python BeautifulSoup libraries aren't installed." |
488 | | - return html |
489 | | - else: |
490 | | - soup = BeautifulSoup(html) |
491 | | - |
492 | | - textNodes = soup.findAll(text=True) |
493 | | - for textNode in textNodes: |
494 | | - urlizedText = urlize(textNode, autoescape=autoescape) |
495 | | - textNode.replaceWith(BeautifulSoup(urlizedText)) |
496 | | - |
497 | | - return str(soup) |
498 | | - |
499 | 481 | @register.filter |
500 | 482 | def emailwrap(email): |
501 | 483 | email = str(email) |
|
0 commit comments