* fix: Correctly urlize bis documents

The old code would first substitute the "rfx<nnnn>" part, and then again
on the full draft name. This now only ever applies one substitute,
and does longer ones first.
This commit is contained in:
Lars Eggert 2022-04-14 20:33:47 +02:00 committed by GitHub
parent 966e1c806a
commit 79c7081181
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 10 deletions

View file

@ -192,15 +192,12 @@ def urlize_ietf_docs(string, autoescape=None):
"""
if autoescape and not isinstance(string, SafeData):
string = escape(string)
string = re.sub(r"(RFC\s*?)0{0,3}(\d+)", "<a href=\"/doc/rfc\\2/\">\\1\\2</a>", string, flags=re.IGNORECASE)
string = re.sub(r"(BCP\s*?)0{0,3}(\d+)", "<a href=\"/doc/bcp\\2/\">\\1\\2</a>", string, flags=re.IGNORECASE)
string = re.sub(r"(STD\s*?)0{0,3}(\d+)", "<a href=\"/doc/std\\2/\">\\1\\2</a>", string, flags=re.IGNORECASE)
string = re.sub(r"(FYI\s*?)0{0,3}(\d+)", "<a href=\"/doc/fyi\\2/\">\\1\\2</a>", string, flags=re.IGNORECASE)
string = re.sub(r"(draft-[-0-9a-zA-Z._+]+)", "<a href=\"/doc/\\1/\">\\1</a>", string, flags=re.IGNORECASE)
string = re.sub(r"(bofreq-[-0-9a-zA-Z._+]+)", "<a href=\"/doc/\\1/\">\\1</a>", string, flags=re.IGNORECASE)
string = re.sub(r"(conflict-review-[-0-9a-zA-Z._+]+)", "<a href=\"/doc/\\1/\">\\1</a>", string, flags=re.IGNORECASE)
string = re.sub(r"(status-change-[-0-9a-zA-Z._+]+)", "<a href=\"/doc/\\1/\">\\1</a>", string, flags=re.IGNORECASE)
string = re.sub(r"(charter-[-0-9a-zA-Z._+]+)", "<a href=\"/doc/\\1/\">\\1</a>", string, flags=re.IGNORECASE)
string = re.sub(
r"\b((RFC|BCP|STD|FYI|(?:draft-|bofreq-|conflict-review-|status-change-|charter-)[-\d\w.+]+)\s*0*(\d+))\b",
lambda x: f'<a href="/doc/{x[2].strip().lower()}{x[3]}/">{x[1]}</a>',
string,
flags=re.IGNORECASE | re.ASCII,
)
return mark_safe(string)
urlize_ietf_docs = stringfilter(urlize_ietf_docs)
@ -739,4 +736,4 @@ def absurl(viewname, **kwargs):
Uses settings.IDTRACKER_BASE_URL as the base.
"""
return urljoin(settings.IDTRACKER_BASE_URL, urlreverse(viewname, kwargs=kwargs))
return urljoin(settings.IDTRACKER_BASE_URL, urlreverse(viewname, kwargs=kwargs))

View file

@ -0,0 +1,27 @@
from ietf.doc.templatetags.ietf_filters import urlize_ietf_docs
from ietf.utils.test_utils import TestCase
# TODO: most other filters need test cases, too
class IetfFiltersTests(TestCase):
def test_urlize_ietf_docs(self):
cases = [
("no change", "no change"),
("bcp1", '<a href="/doc/bcp1/">bcp1</a>'),
("Std 003", '<a href="/doc/std3/">Std 003</a>'),
(
"FYI02 changes Std 003",
'<a href="/doc/fyi2/">FYI02</a> changes <a href="/doc/std3/">Std 003</a>',
),
("rfc2119", '<a href="/doc/rfc2119/">rfc2119</a>'),
("Rfc 02119", '<a href="/doc/rfc2119/">Rfc 02119</a>'),
("draft-abc-123", '<a href="/doc/draft-abc-123/">draft-abc-123</a>'),
(
"draft-ietf-rfc9999-bis-01",
'<a href="/doc/draft-ietf-rfc9999-bis-01/">draft-ietf-rfc9999-bis-01</a>',
),
]
for input, output in cases:
self.assertEqual(urlize_ietf_docs(input), output)