def check_uri(self, refnode: nodes.reference) -> None: """ If the URI in ``refnode`` has a replacement in ``extlinks``, emit a warning with a replacement suggestion. """ if 'internal' in refnode or 'refuri' not in refnode: return uri = refnode['refuri'] title = refnode.astext() for alias, (base_uri, _caption) in self.app.config.extlinks.items(): if sys.version_info < (3, 7): # Replace a leading backslash because re.escape() inserts a backslash before % # on python 3.6 uri_pattern = re.compile( re.escape(base_uri).replace('\\%s', '(?P<value>.+)')) else: uri_pattern = re.compile( re.escape(base_uri).replace('%s', '(?P<value>.+)')) match = uri_pattern.match(uri) if match and match.groupdict().get('value'): # build a replacement suggestion msg = __('hardcoded link %r could be replaced by an extlink ' '(try using %r instead)') value = match.groupdict().get('value') if uri != title: replacement = f":{alias}:`{rst.escape(title)} <{value}>`" else: replacement = f":{alias}:`{value}`" logger.warning(msg, uri, replacement, location=refnode)
def _mask_email(ref: nodes.reference, pep_num: int = -1) -> nodes.reference: """Mask the email address in `ref` and return a replacement node. `ref` is returned unchanged if it contains no email address. If given an email not explicitly whitelisted, process it such that `user@host` -> `user at host`. If given a PEP number `pep_num`, add a default email subject. """ if "refuri" in ref and ref["refuri"].startswith("mailto:"): non_masked_addresses = { "*****@*****.**", "*****@*****.**", "*****@*****.**" } if ref['refuri'].removeprefix( "mailto:").strip() in non_masked_addresses: replacement = ref[0] else: replacement_text = ref.astext().replace("@", " at ") replacement = nodes.raw('', replacement_text, format="html") if pep_num != -1: replacement['refuri'] += f"?subject=PEP%20{pep_num}" return replacement return ref
def _mask_email(ref: nodes.reference) -> nodes.reference: """Mask the email address in `ref` and return a replacement node. `ref` is returned unchanged if it contains no email address. If given an email not explicitly whitelisted, process it such that `user@host` -> `user at host`. The returned node has no refuri link attribute. """ if not ref.get("refuri", "").startswith("mailto:"): return ref return nodes.raw("", ref[0].replace("@", " at "), format="html")
def visit_reference(node: nodes.reference) -> None: """Mask email addresses if present.""" node.replace_self(peps.mask_email(node))