aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/tasks
diff options
context:
space:
mode:
authorDennis Fink2026-11-12 23:03:23 +0100
committerDennis Fink2026-11-12 23:03:23 +0100
commit8f4f9041c60991457ccb1b3f145676d90e28aa8c (patch)
treed66dc9375d9673df05ce59e5ebddd60cd4dca50b /webmentions_ssg/tasks
parenta10b29ac98ba535a5c2f9697c54265edd2c84a78 (diff)
downloadwebmentions-ssg-8f4f9041c60991457ccb1b3f145676d90e28aa8c.tar.gz
webmentions-ssg-8f4f9041c60991457ccb1b3f145676d90e28aa8c.zip
refactor(receiver): simplify HTML mention selectors
Store complete CSS selectors alongside their target attributes so HTML mention detection can query supported references directly without rebuilding selectors.
Diffstat (limited to 'webmentions_ssg/tasks')
-rw-r--r--webmentions_ssg/tasks/mentions.py34
1 files changed, 13 insertions, 21 deletions
diff --git a/webmentions_ssg/tasks/mentions.py b/webmentions_ssg/tasks/mentions.py
index 42dd0b6..687ba38 100644
--- a/webmentions_ssg/tasks/mentions.py
+++ b/webmentions_ssg/tasks/mentions.py
@@ -12,22 +12,18 @@ from bs4 import BeautifulSoup
IRI_PATTERN = rfc3987.get_compiled_pattern("IRI")
TRAILING_IRI_PUNCTUATION = ".,;:!?"
-HTML_URL_ATTRIBUTES = {
- "href": {"a", "area", "link"},
- "src": {
- "audio",
- "embed",
- "iframe",
- "img",
- 'input[type="image" i]',
- "script",
- "audio source",
- "video source",
- "track",
- "video",
- },
- "cite": {"blockquote", "del", "ins", "q"},
-}
+HTML_URL_ATTRIBUTES = (
+ ("a[href], area[href], link[href]", "href"),
+ (
+ (
+ "audio[src], embed[src], iframe[src], img[src], "
+ 'input[type="image" i][src], script[src], '
+ "audio source[src], video source[src], track[src], video[src]"
+ ),
+ "src",
+ ),
+ ("blockquote[cite], del[cite], ins[cite], q[cite]", "cite"),
+)
def html_mentions_target(body: bytes, source_url: str, target_url: str) -> bool:
@@ -49,11 +45,7 @@ def html_mentions_target(body: bytes, source_url: str, target_url: str) -> bool:
):
base_url = urljoin(source_url, base_href.strip())
- for attribute, selectors in HTML_URL_ATTRIBUTES.items():
- selector = ", ".join(
- [f"{selector_string}[{attribute}]" for selector_string in selectors]
- )
-
+ for selector, attribute in HTML_URL_ATTRIBUTES:
for element in document.select(selector):
if not isinstance(reference := element.get(attribute), str):
continue