From 7ce4c309224d56f5e1a270690105fbecd848b7c3 Mon Sep 17 00:00:00 2001 From: Martin Gross Date: Fri, 17 Apr 2020 17:44:08 +0200 Subject: [PATCH] Parse more nTLDs and gTLDs (Fix #1488) --- src/pretix/base/templatetags/rich_text.py | 23 +++++++++++++++++------ src/requirements/production.txt | 3 ++- src/setup.py | 3 ++- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/pretix/base/templatetags/rich_text.py b/src/pretix/base/templatetags/rich_text.py index b010462c4..7aebf5427 100644 --- a/src/pretix/base/templatetags/rich_text.py +++ b/src/pretix/base/templatetags/rich_text.py @@ -3,12 +3,14 @@ import urllib.parse import bleach import markdown from bleach import DEFAULT_CALLBACKS +from bleach.linkifier import build_email_re, build_url_re from django import template from django.conf import settings from django.core import signing from django.urls import reverse from django.utils.http import url_has_allowed_host_and_scheme from django.utils.safestring import mark_safe +from tlds import tld_set register = template.Library() @@ -84,7 +86,12 @@ def abslink_callback(attrs, new=False): def markdown_compile_email(source): - return bleach.linkify(bleach.clean( + linker = bleach.Linker( + url_re=build_url_re(tlds=tld_set), + email_re=build_email_re(tlds=tld_set), + parse_email=True + ) + return linker.linkify(bleach.clean( markdown.markdown( source, extensions=[ @@ -95,7 +102,7 @@ def markdown_compile_email(source): tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, protocols=ALLOWED_PROTOCOLS, - ), parse_email=True) + )) def markdown_compile(source, snippet=False): @@ -121,11 +128,13 @@ def rich_text(text: str, **kwargs): Processes markdown and cleans HTML in a text input. """ text = str(text) - body_md = bleach.linkify( - markdown_compile(text), + linker = bleach.Linker( + url_re=build_url_re(tlds=tld_set), + email_re=build_email_re(tlds=tld_set), callbacks=DEFAULT_CALLBACKS + ([safelink_callback] if kwargs.get('safelinks', True) else [abslink_callback]), parse_email=True ) + body_md = linker.linkify(markdown_compile(text)) return mark_safe(body_md) @@ -135,9 +144,11 @@ def rich_text_snippet(text: str, **kwargs): Processes markdown and cleans HTML in a text input. """ text = str(text) - body_md = bleach.linkify( - markdown_compile(text, snippet=True), + linker = bleach.Linker( + url_re=build_url_re(tlds=tld_set), + email_re=build_email_re(tlds=tld_set), callbacks=DEFAULT_CALLBACKS + ([safelink_callback] if kwargs.get('safelinks', True) else [abslink_callback]), parse_email=True ) + body_md = linker.linkify(markdown_compile(text, snippet=True)) return mark_safe(body_md) diff --git a/src/requirements/production.txt b/src/requirements/production.txt index 4ad5b1b7b..cef9e35c1 100644 --- a/src/requirements/production.txt +++ b/src/requirements/production.txt @@ -31,7 +31,7 @@ dj-static csscompressor django-markup markdown<=2.2 -bleach==3.1.* +bleach>=3.1.3 sentry-sdk==0.14.* babel django-i18nfield>=1.7.0 @@ -64,3 +64,4 @@ phonenumberslite==8.11.* python-bidi==0.4.* # Support for arabic in reportlab arabic-reshaper==2.0.15 # Support for Aabic in reportlab packaging +tlds>=2020041600 \ No newline at end of file diff --git a/src/setup.py b/src/setup.py index 88b25d62b..d8e2140c8 100644 --- a/src/setup.py +++ b/src/setup.py @@ -120,7 +120,7 @@ setup( 'csscompressor', 'django-markup', 'markdown<=2.2', - 'bleach==3.1.*', + 'bleach>=3.1.3', 'sentry-sdk==0.14.*', 'babel', 'paypalrestsdk==1.13.*', @@ -151,6 +151,7 @@ setup( 'python-bidi==0.4.*', # Support for Arabic in reportlab 'arabic-reshaper==2.0.15', # Support for Arabic in reportlab 'packaging', + 'tlds>=2020041600' ], extras_require={ 'dev': [