From 7a1926113cf0e1d6f651928142393ad401f3bb99 Mon Sep 17 00:00:00 2001 From: omicron Date: Mon, 24 Aug 2026 14:34:08 +0200 Subject: [PATCH] Track url as an absolute Path and add Site.url_for helper --- omicron/ssg/output/output.py | 7 ++++--- omicron/ssg/site.py | 19 ++++++++++++++----- omicron/ssg/templates/plain/base.html | 4 ++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/omicron/ssg/output/output.py b/omicron/ssg/output/output.py index f29d0fe..e3e2c0f 100644 --- a/omicron/ssg/output/output.py +++ b/omicron/ssg/output/output.py @@ -26,9 +26,10 @@ class Output(ABC): raise OutputError("destination path must be normalized") self.destination = destination - url = site.base_dir + "/" + destination.as_posix() - url = url.removesuffix("/index.html") or "/" - self.url = url + url = site.base_dir / destination + if url.name == "index.html": + url = url.parent + self.url = url.as_posix() @property def site(self) -> Site: diff --git a/omicron/ssg/site.py b/omicron/ssg/site.py index 0d173b3..387fe36 100644 --- a/omicron/ssg/site.py +++ b/omicron/ssg/site.py @@ -64,9 +64,9 @@ class Site: return cast(dict[str, Any], config) @staticmethod - def parse_base_url(value: str | None) -> tuple[str, str]: + def parse_base_url(value: str | None) -> tuple[str, Path]: if not value: - return ("", "") + return ("", Path("/")) parsed = urlparse(value) if parsed.scheme and parsed.netloc: origin = f"{parsed.scheme}://{parsed.netloc}" @@ -75,7 +75,11 @@ class Site: else: origin = "" - path = parsed.path.removesuffix("/") + if parsed.path: + path = Path(parsed.path) + else: + path = Path("/") + return (origin, path) @staticmethod @@ -120,7 +124,12 @@ class Site: self.directories.append(d) def home_url(self) -> str: - return self.base_dir or "/" + return self.base_dir.as_posix() + + def url_for(self, path: str) -> str: + if Path(path).is_absolute(): + raise OutputError("path must be relative") + return (self.base_dir / path).as_posix() def section_url(self, section: str) -> str: path = Index.build_path("section", 1, section) @@ -179,7 +188,7 @@ class Site: "base_url config value is missing a domain name, " "can't add canonical url to content" ) - if self.base_dir and not self.base_dir.startswith("/"): + if not self.base_dir.is_absolute(): raise ConfigError("base_url config value must be an absolute path") self.discover() outputs: list[Output] = [*self.directories, *self.content] diff --git a/omicron/ssg/templates/plain/base.html b/omicron/ssg/templates/plain/base.html index 243cdfc..3af903a 100644 --- a/omicron/ssg/templates/plain/base.html +++ b/omicron/ssg/templates/plain/base.html @@ -4,8 +4,8 @@ {% block title %}{{ site.name }}{% endblock %} - - + + {% if site.origin %}{% endif %} {% block head %}{% endblock %}