From b8a0a94ba96a079fb9b81adcbfc2061375f254af Mon Sep 17 00:00:00 2001 From: omicron Date: Tue, 4 Aug 2026 13:20:57 +0200 Subject: [PATCH] populate the by_tag, by_uri and by_section maps --- omicron/ssg/site.py | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/omicron/ssg/site.py b/omicron/ssg/site.py index 645defe..5f6c81b 100644 --- a/omicron/ssg/site.py +++ b/omicron/ssg/site.py @@ -1,7 +1,12 @@ import logging from typing import Any -from omicron.ssg.content import Content, create_content, CONTENT_EXTENSIONS +from omicron.ssg.content import ( + Content, + create_content, + CONTENT_EXTENSIONS, + ContentError, +) from pathlib import Path import yaml from jinja2 import Environment, FileSystemLoader @@ -18,9 +23,9 @@ class Site: def __init__(self, site: Path): self.site_path = site.absolute() self.content: list[Content] = [] - self.by_tag: dict[str, list[Content]] - self.by_uri: dict[str, Content] - self.by_section: dict[str, Content] + self.by_tag: dict[str, list[Content]] = {} + self.by_uri: dict[str, Content] = {} + self.by_section: dict[str, list[Content]] = {} config = self.read_config() self.name: str = config["name"] @@ -47,12 +52,36 @@ class Site: raise ConfigError(f"template '{template}' not found at {path}") return path + def add_by_section(self, content: Content) -> None: + if content.section not in self.by_section: + self.by_section[content.section] = [] + self.by_section[content.section].append(content) + + def add_by_tag(self, content: Content) -> None: + for tag in content.tags: + if tag not in self.by_tag: + self.by_tag[tag] = [] + self.by_tag[tag].append(content) + + def add_by_uri(self, content: Content) -> None: + if content.uri in self.by_uri: + conflict = self.by_uri[content.uri] + raise ContentError( + f"the file at '{content.source}' maps to the same uri as the " + f"file at '{conflict.source}'" + ) + self.by_uri[content.uri] = content + def discover(self) -> None: log.info("Discovering content...") content_path = self.site_path / "content" for file in content_path.rglob("*"): if file.is_file() and file.suffix in CONTENT_EXTENSIONS: - self.content.append(create_content(file)) + content = create_content(file) + self.add_by_uri(content) + self.add_by_section(content) + self.add_by_tag(content) + self.content.append(content) log.info("Discovered %d content items", len(self.content)) def build(self):