From a1ecd0f4800e58f49f232cd500dc0e29e09a8966 Mon Sep 17 00:00:00 2001 From: omicron Date: Tue, 20 Oct 2026 19:19:45 +0200 Subject: [PATCH] Allow local template overrides in the site directory closes #13 --- omicron/ssg/cli.py | 1 - omicron/ssg/site.py | 14 +++++++++----- tests/unit/test_site.py | 24 ++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/omicron/ssg/cli.py b/omicron/ssg/cli.py index b81e409..968432d 100644 --- a/omicron/ssg/cli.py +++ b/omicron/ssg/cli.py @@ -149,7 +149,6 @@ def init(args: argparse.Namespace) -> None: "items_per_page": int(items_per_page), } ) - Site.resolve_template_path(config.template) config.to_file(config_path) (args.site / "content").mkdir(exist_ok=True) diff --git a/omicron/ssg/site.py b/omicron/ssg/site.py index fc00657..bd6f04b 100644 --- a/omicron/ssg/site.py +++ b/omicron/ssg/site.py @@ -27,6 +27,7 @@ from pathlib import Path from jinja2 import Environment, FileSystemLoader log = logging.getLogger(__name__) +builtin_templates = Path(__file__).parent / "templates" class Site: @@ -43,15 +44,18 @@ class Site: self.tags_by_count: list[tuple[str, int]] = [] self.config = SiteConfig.from_file(site / "config.yml") - self.template_path: Path = Site.resolve_template_path(self.config.template) + self.template_path: Path = self.resolve_template_path(self.config.template) self.output_path: Path = self.site_path / "output" self.jinja_env = Environment(loader=FileSystemLoader(self.template_path)) - @staticmethod - def resolve_template_path(template: str) -> Path: - path = Path(__file__).parent / "templates" / template + def resolve_template_path(self, template: str) -> Path: + path = self.site_path / "templates" / template + if path.is_dir(): + return path + + path = builtin_templates / template if not path.is_dir(): - raise ConfigError(f"template '{template}' not found at {path}") + raise ConfigError(f"template '{template}' not found.") return path def add_by_section(self, content: Content) -> None: diff --git a/tests/unit/test_site.py b/tests/unit/test_site.py index f4a0068..eca723c 100644 --- a/tests/unit/test_site.py +++ b/tests/unit/test_site.py @@ -78,9 +78,29 @@ def test_clean_removes_output_directory(make_site): assert not site.output_path.exists() -def test_resolve_template_path_raises_for_missing_template(): +def test_resolve_template_path_raises_for_missing_template(site_dir): + path = site_dir("thin_site") + (path / "config.yml").write_text("name: test\ntemplate: does-not-exist\n") + with pytest.raises(ConfigError, match="not found"): - Site.resolve_template_path("does-not-exist") + Site(path) + + +def test_resolve_template_path_prefers_local_override(site_dir): + path = site_dir("thin_site") + (path / "config.yml").write_text("name: test\ntemplate: plain\n") + local = path / "templates" / "plain" + local.mkdir(parents=True) + + assert Site(path).template_path == local + + +def test_resolve_template_path_falls_back_to_builtin(site_dir): + path = site_dir("thin_site") + (path / "config.yml").write_text("name: test\ntemplate: plain\n") + + assert Site(path).template_path.name == "plain" + assert not Site(path).template_path.is_relative_to(path) def test_home_url(site_dir):