Add source index for Content, enforce stricter Content.source checks

This commit is contained in:
2026-08-25 03:08:07 +02:00
parent 3abb6ce8fe
commit 015f1375ec
2 changed files with 11 additions and 1 deletions
+5 -1
View File
@@ -1,6 +1,6 @@
import re import re
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from omicron.ssg.output.output import Output, OutputError from omicron.ssg.output.output import Output, OutputError, is_normalized
from pathlib import Path from pathlib import Path
from datetime import date from datetime import date
@@ -31,6 +31,10 @@ class Content(Output):
super().__init__(site, Content.build_path(section, slug)) super().__init__(site, Content.build_path(section, slug))
if tags is None: if tags is None:
tags = set() tags = set()
if not source.is_absolute():
raise ContentError("content source path must be absolute")
if not is_normalized(source):
raise ContentError("content source path must be normalized")
self.source = source self.source = source
self.section: str = section self.section: str = section
self.created = created self.created = created
+6
View File
@@ -38,6 +38,7 @@ class Site:
self.other_outputs: list[Output] = [] self.other_outputs: list[Output] = []
self.by_tag: dict[str, list[Content]] = {} self.by_tag: dict[str, list[Content]] = {}
self.by_path: dict[Path, Output] = {} self.by_path: dict[Path, Output] = {}
self.by_source: dict[Path, Content] = {}
self.by_section: dict[str, list[Content]] = {} self.by_section: dict[str, list[Content]] = {}
self.tags_by_count: list[tuple[str, int]] = [] self.tags_by_count: list[tuple[str, int]] = []
@@ -123,6 +124,10 @@ class Site:
self.by_path[d.destination] = d self.by_path[d.destination] = d
self.directories.append(d) self.directories.append(d)
def add_by_source(self, content: Content) -> None:
assert content.source.is_absolute(), "Expecting content to have absolute source"
self.by_source[content.source] = content
def home_url(self) -> str: def home_url(self) -> str:
return self.base_dir.as_posix() return self.base_dir.as_posix()
@@ -172,6 +177,7 @@ class Site:
self.add_by_path(content) self.add_by_path(content)
self.add_by_section(content) self.add_by_section(content)
self.add_by_tag(content) self.add_by_tag(content)
self.add_by_source(content)
self.content.append(content) self.content.append(content)
self.update_tags_by_count() self.update_tags_by_count()
for index in discover_index(self): for index in discover_index(self):