diff options
Diffstat (limited to 'webmentions_ssg/tasks/source_parser.py')
| -rw-r--r-- | webmentions_ssg/tasks/source_parser.py | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/webmentions_ssg/tasks/source_parser.py b/webmentions_ssg/tasks/source_parser.py index 1bd498c..9cbe071 100644 --- a/webmentions_ssg/tasks/source_parser.py +++ b/webmentions_ssg/tasks/source_parser.py @@ -97,30 +97,31 @@ def canonical_url( :raises SourceScanError: If the canonical URL is empty, cannot be resolved, or does not resolve to an HTTP or HTTPS URL. """ - if (link := document.select_one('link[rel~="canonical"][href]')) is not None: - href = link.get("href") + if (link := document.select_one('link[rel~="canonical"][href]')) is None: + return None - if not isinstance(href, str) or not (href := href.strip()): - raise SourceScanError("Document contains an empty canonical URL") + href = link.get("href") - if is_http_url(href): - return href + if not isinstance(href, str) or not (href := href.strip()): + raise SourceScanError("Document contains an empty canonical URL") - if base_url is None: - raise SourceScanError( - "Document contains a relative canonical URL, but " - "SOURCE_BASE_URL is not configured" - ) + if is_http_url(href): + return href - resolved = urljoin(source_url_for_path(relative_path, base_url), href) + if base_url is None: + raise SourceScanError( + "Document contains a relative canonical URL, but " + "SOURCE_BASE_URL is not configured" + ) - if not is_http_url(resolved): - raise SourceScanError( - f"Canonical URL is not a valid HTTP or HTTPS URL: {href!r}" - ) + resolved = urljoin(source_url_for_path(relative_path, base_url), href) + + if not is_http_url(resolved): + raise SourceScanError( + f"Canonical URL is not a valid HTTP or HTTPS URL: {href!r}" + ) - return resolved - return None + return resolved def property_urls(entry: dict[str, Any], property_name: str) -> Iterator[str]: |
