diff options
Diffstat (limited to 'webmentions_ssg/tasks/receiver.py')
| -rw-r--r-- | webmentions_ssg/tasks/receiver.py | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/webmentions_ssg/tasks/receiver.py b/webmentions_ssg/tasks/receiver.py index 2bb7222..d195cf5 100644 --- a/webmentions_ssg/tasks/receiver.py +++ b/webmentions_ssg/tasks/receiver.py @@ -32,6 +32,7 @@ class TemporaryFetchError(Exception): IRI_PATTERN = rfc3987.get_compiled_pattern("IRI") +TRAILING_IRI_PUNCTUATION = ".,;:!?" HTML_URL_ATTRIBUTES = { @@ -90,13 +91,35 @@ def html_mentions_target(body: bytes, source_url: str, target_url: str) -> bool: def text_mentions_target(body: str, target_url: str) -> bool: """ - Check whether plain text contains the exact target IRI. + Check whether plain text contains the target IRI, ignoring trailing prose + punctuation but not extra URL path or query characters. :param body: Plain-text document body. :param target_url: Exact IRI to look for. :return: Whether the target IRI occurs in the document. """ - return any(match.group() == target_url for match in IRI_PATTERN.finditer(body)) + for match in IRI_PATTERN.finditer(body): + candidate = match.group() + + # A URL in prose can be followed by punctuation that the IRI pattern + # consumes as part of its match. Only remove trailing punctuation, + # leaving longer URLs (e.g. ``target/more``) unmatched. + if not candidate.startswith(target_url): + continue + + while candidate != target_url: + if candidate[-1] in TRAILING_IRI_PUNCTUATION: + candidate = candidate[:-1] + elif candidate[-1] == ")" and candidate.count(")") > candidate.count("("): + # Keep balanced parentheses that belong to the URL itself. + candidate = candidate[:-1] + else: + break + + if candidate == target_url: + return True + + return False def ensure_public_request(request: httpx.Request) -> None: |
