Skip to content

Commit f174c6a

Browse files
committed
Simplify the mask email algorithm
1 parent f37a429 commit f174c6a

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

pep_sphinx_extensions/pep_processor/transforms/pep_headers.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
24
import re
35

@@ -68,11 +70,11 @@ def apply(self) -> None:
6870
raise PEPParsingError(msg)
6971

7072
para = body[0]
71-
if name in {"author", "bdfl-delegate", "pep-delegate", "sponsor"}:
73+
if name in {"author", "bdfl-delegate", "pep-delegate", "discussions-to", "sponsor"}:
7274
# mask emails
7375
for node in para:
7476
if isinstance(node, nodes.reference):
75-
pep_num = pep if name == "discussions-to" else -1
77+
pep_num = pep if name == "discussions-to" else None
7678
node.replace_self(_mask_email(node, pep_num))
7779
elif name in {"replaces", "superseded-by", "requires"}:
7880
# replace PEP numbers with normalised list of links to PEPs
@@ -93,7 +95,7 @@ def apply(self) -> None:
9395
field.parent.remove(field)
9496

9597

96-
def _mask_email(ref: nodes.reference, pep_num: int = -1) -> nodes.reference:
98+
def _mask_email(ref: nodes.reference, pep_num: int | None = None) -> nodes.reference:
9799
"""Mask the email address in `ref` and return a replacement node.
98100
99101
`ref` is returned unchanged if it contains no email address.
@@ -104,15 +106,12 @@ def _mask_email(ref: nodes.reference, pep_num: int = -1) -> nodes.reference:
104106
If given a PEP number `pep_num`, add a default email subject.
105107
106108
"""
107-
if "refuri" in ref and ref["refuri"].startswith("mailto:"):
108-
non_masked_addresses = {"[email protected]", "[email protected]", "[email protected]"}
109-
if ref["refuri"].removeprefix("mailto:").strip() in non_masked_addresses:
110-
replacement = ref[0]
111-
else:
112-
replacement_text = ref.astext().replace("@", " at ")
113-
replacement = nodes.raw("", replacement_text, format="html")
114-
115-
if pep_num != -1:
116-
replacement["refuri"] += f"?subject=PEP%20{pep_num}"
117-
return replacement
109+
if "refuri" not in ref or not ref["refuri"].startswith("mailto:"):
110+
return ref
111+
non_masked_addresses = {"[email protected]", "[email protected]", "[email protected]"}
112+
if ref["refuri"].removeprefix("mailto:").strip() not in non_masked_addresses:
113+
ref[0] = nodes.raw("", ref[0].replace("@", " at "), format="html")
114+
if pep_num is None:
115+
return ref[0] # return email text without mailto link
116+
ref["refuri"] += f"?subject=PEP%20{pep_num}"
118117
return ref

0 commit comments

Comments
 (0)