Track url as an absolute Path and add Site.url_for helper

This commit is contained in:
2026-08-24 14:34:08 +02:00
parent 74925f1668
commit 7a1926113c
3 changed files with 20 additions and 10 deletions
+4 -3
View File
@@ -26,9 +26,10 @@ class Output(ABC):
raise OutputError("destination path must be normalized") raise OutputError("destination path must be normalized")
self.destination = destination self.destination = destination
url = site.base_dir + "/" + destination.as_posix() url = site.base_dir / destination
url = url.removesuffix("/index.html") or "/" if url.name == "index.html":
self.url = url url = url.parent
self.url = url.as_posix()
@property @property
def site(self) -> Site: def site(self) -> Site:
+14 -5
View File
@@ -64,9 +64,9 @@ class Site:
return cast(dict[str, Any], config) return cast(dict[str, Any], config)
@staticmethod @staticmethod
def parse_base_url(value: str | None) -> tuple[str, str]: def parse_base_url(value: str | None) -> tuple[str, Path]:
if not value: if not value:
return ("", "") return ("", Path("/"))
parsed = urlparse(value) parsed = urlparse(value)
if parsed.scheme and parsed.netloc: if parsed.scheme and parsed.netloc:
origin = f"{parsed.scheme}://{parsed.netloc}" origin = f"{parsed.scheme}://{parsed.netloc}"
@@ -75,7 +75,11 @@ class Site:
else: else:
origin = "" origin = ""
path = parsed.path.removesuffix("/") if parsed.path:
path = Path(parsed.path)
else:
path = Path("/")
return (origin, path) return (origin, path)
@staticmethod @staticmethod
@@ -120,7 +124,12 @@ class Site:
self.directories.append(d) self.directories.append(d)
def home_url(self) -> str: 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: def section_url(self, section: str) -> str:
path = Index.build_path("section", 1, section) path = Index.build_path("section", 1, section)
@@ -179,7 +188,7 @@ class Site:
"base_url config value is missing a domain name, " "base_url config value is missing a domain name, "
"can't add canonical url to content" "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") raise ConfigError("base_url config value must be an absolute path")
self.discover() self.discover()
outputs: list[Output] = [*self.directories, *self.content] outputs: list[Output] = [*self.directories, *self.content]
+2 -2
View File
@@ -4,8 +4,8 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}{{ site.name }}{% endblock %}</title> <title>{% block title %}{{ site.name }}{% endblock %}</title>
<link rel="stylesheet" href="{{ site.base_dir }}/assets/style.css"> <link rel="stylesheet" href="{{ site.url_for('assets/style.css') }}">
<link rel="stylesheet" href="{{ site.base_dir }}/assets/pygments.css"> <link rel="stylesheet" href="{{ site.url_for('assets/pygments.css') }}">
{% if site.origin %}<link rel="canonical" href="{{ site.origin }}{{ page.url }}">{% endif %} {% if site.origin %}<link rel="canonical" href="{{ site.origin }}{{ page.url }}">{% endif %}
{% block head %}{% endblock %} {% block head %}{% endblock %}
</head> </head>