Skip to content

Commit 9db1d48

Browse files
larseggertrjsparksNGPixel
authored
fix: Correctly linkify all current TLDs (ietf-tools#3868)
* fix: Correctly linkify all current TLDs * Pass a list to the build_*_re functions, not a string * Need to sort TLDs by length to force longer ones to match first * chore: silence incorrect mypy complaint. Co-authored-by: Robert Sparks <rjsparks@nostrum.com> Co-authored-by: Nicolas Giard <github@ngpixel.com>
1 parent 955d9ac commit 9db1d48

4 files changed

Lines changed: 18 additions & 9 deletions

File tree

ietf/doc/templatetags/ietf_filters.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# -*- coding: utf-8 -*-
33

44

5-
import bleach
65
import datetime
76
import re
87
from urllib.parse import urljoin
@@ -26,7 +25,7 @@
2625
from ietf.utils.html import sanitize_fragment
2726
from ietf.utils import log
2827
from ietf.doc.utils import prettify_std_name
29-
from ietf.utils.text import wordwrap, fill, wrap_text_if_unwrapped
28+
from ietf.utils.text import wordwrap, fill, wrap_text_if_unwrapped, bleach_linker
3029

3130
register = template.Library()
3231

@@ -428,14 +427,14 @@ def format_history_text(text, trunc_words=25):
428427
full = mark_safe(text)
429428
if "</a>" not in full:
430429
full = urlize_ietf_docs(full)
431-
full = bleach.linkify(full, parse_email=True)
430+
full = bleach_linker.linkify(full)
432431

433432
return format_snippet(full, trunc_words)
434433

435434
@register.filter
436435
def format_snippet(text, trunc_words=25):
437436
# urlize if there aren't already links present
438-
text = bleach.linkify(text, parse_email=True)
437+
text = bleach_linker.linkify(text)
439438
full = keep_spacing(collapsebr(linebreaksbr(mark_safe(sanitize_fragment(text)))))
440439
snippet = truncatewords_html(full, trunc_words)
441440
if snippet != full:

ietf/utils/templatetags/textfilters.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44

55
import re
6-
import bleach
76

87
from django import template
98
from django.conf import settings
@@ -12,7 +11,7 @@
1211

1312
import debug # pyflakes:ignore
1413

15-
from ietf.utils.text import xslugify as _xslugify, texescape
14+
from ietf.utils.text import xslugify as _xslugify, texescape, bleach_linker
1615

1716
register = template.Library()
1817

@@ -75,7 +74,7 @@ def texescape_filter(value):
7574
@register.filter
7675
@stringfilter
7776
def linkify(value):
78-
text = mark_safe(bleach.linkify(value, parse_email=True))
77+
text = mark_safe(bleach_linker.linkify(value))
7978
return text
8079

8180
@register.filter
@@ -92,4 +91,4 @@ def conference_url(value):
9291
return value if re.match(conf_re, value) else None
9392

9493

95-
94+

ietf/utils/text.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# -*- coding: utf-8 -*-
33

44

5+
import bleach
56
import email
67
import re
78
import textwrap
9+
import tlds
810
import unicodedata
911

1012
from django.utils.functional import keep_lazy
@@ -14,6 +16,14 @@
1416

1517
from .texescape import init as texescape_init, tex_escape_map
1618

19+
tlds_sorted = sorted(tlds.tld_set, key=len, reverse=True)
20+
bleach_linker = bleach.Linker(
21+
url_re=bleach.linkifier.build_url_re(tlds=tlds_sorted),
22+
email_re=bleach.linkifier.build_email_re(tlds=tlds_sorted), # type: ignore
23+
parse_email=True
24+
)
25+
26+
1727
@keep_lazy(str)
1828
def xslugify(value):
1929
"""
@@ -206,4 +216,4 @@ def parse_unicode(text):
206216
pass
207217
else:
208218
text = decoded_string
209-
return text
219+
return text

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ scout-apm>=2.23.0
6868
selenium>=3.141.0,<4.0
6969
six>=1.10.0
7070
tblib>=1.3.0
71+
tlds>=2022042100 # Used to teach bleach about which TLDs currently exist
7172
tqdm>=3.7.0
7273
Unidecode>=0.4.18,<1.2.0
7374
#wsgiref>=0.1.2

0 commit comments

Comments
 (0)