populate the by_tag, by_uri and by_section maps

This commit is contained in:
2026-08-04 13:20:57 +02:00
parent cb48dc035a
commit b8a0a94ba9
+34 -5
View File
@@ -1,7 +1,12 @@
import logging import logging
from typing import Any 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 from pathlib import Path
import yaml import yaml
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
@@ -18,9 +23,9 @@ class Site:
def __init__(self, site: Path): def __init__(self, site: Path):
self.site_path = site.absolute() self.site_path = site.absolute()
self.content: list[Content] = [] self.content: list[Content] = []
self.by_tag: dict[str, list[Content]] self.by_tag: dict[str, list[Content]] = {}
self.by_uri: dict[str, Content] self.by_uri: dict[str, Content] = {}
self.by_section: dict[str, Content] self.by_section: dict[str, list[Content]] = {}
config = self.read_config() config = self.read_config()
self.name: str = config["name"] self.name: str = config["name"]
@@ -47,12 +52,36 @@ class Site:
raise ConfigError(f"template '{template}' not found at {path}") raise ConfigError(f"template '{template}' not found at {path}")
return 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: def discover(self) -> None:
log.info("Discovering content...") log.info("Discovering content...")
content_path = self.site_path / "content" content_path = self.site_path / "content"
for file in content_path.rglob("*"): for file in content_path.rglob("*"):
if file.is_file() and file.suffix in CONTENT_EXTENSIONS: 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)) log.info("Discovered %d content items", len(self.content))
def build(self): def build(self):