Add Memory output, change Site to use Output files in the build loop
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from omicron.ssg.output.output import Output
|
from omicron.ssg.output.output import Output
|
||||||
from omicron.ssg.output.content import Content, ContentError
|
from omicron.ssg.output.content import Content, ContentError
|
||||||
from omicron.ssg.output.factory import create_content, is_content
|
from omicron.ssg.output.factory import create_content, is_content
|
||||||
|
from omicron.ssg.output.memory import Memory
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"Output",
|
"Output",
|
||||||
@@ -8,4 +9,5 @@ __all__ = [
|
|||||||
"ContentError",
|
"ContentError",
|
||||||
"create_content",
|
"create_content",
|
||||||
"is_content",
|
"is_content",
|
||||||
|
"Memory",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import logging
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from omicron.ssg.output.output import Output
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from omicron.ssg.site import Site
|
||||||
|
else:
|
||||||
|
Site = "omicron.ssg.site.Site"
|
||||||
|
|
||||||
|
|
||||||
|
class Memory(Output):
|
||||||
|
def __init__(self, uri: str, content: str):
|
||||||
|
super().__init__(uri)
|
||||||
|
self.content = content
|
||||||
|
|
||||||
|
def write(self, site: Site) -> None:
|
||||||
|
dest = site.output_path / self.uri
|
||||||
|
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
dest.write_text(self.content, encoding="utf-8")
|
||||||
|
log.debug("Wrote %s", dest)
|
||||||
+19
-11
@@ -4,7 +4,14 @@ from urllib.parse import urlparse
|
|||||||
from itertools import batched
|
from itertools import batched
|
||||||
from math import ceil
|
from math import ceil
|
||||||
|
|
||||||
from omicron.ssg.output import Content, ContentError, is_content, create_content
|
from omicron.ssg.output import (
|
||||||
|
Output,
|
||||||
|
Content,
|
||||||
|
ContentError,
|
||||||
|
is_content,
|
||||||
|
create_content,
|
||||||
|
Memory,
|
||||||
|
)
|
||||||
from omicron.ssg.markdown import highlight_style
|
from omicron.ssg.markdown import highlight_style
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -22,8 +29,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.other_outputs: list[Output] = []
|
||||||
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, Output] = {}
|
||||||
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]] = []
|
||||||
|
|
||||||
@@ -84,14 +92,12 @@ class Site:
|
|||||||
self.by_tag[tag] = []
|
self.by_tag[tag] = []
|
||||||
self.by_tag[tag].append(content)
|
self.by_tag[tag].append(content)
|
||||||
|
|
||||||
def add_by_uri(self, content: Content) -> None:
|
def add_by_uri(self, output: Output) -> None:
|
||||||
if content.uri in self.by_uri:
|
if output.uri in self.by_uri:
|
||||||
conflict = self.by_uri[content.uri]
|
|
||||||
raise ContentError(
|
raise ContentError(
|
||||||
f"the file at '{content.source}' maps to the same uri as the "
|
f"uri '{output.uri}' is already claimed by another output"
|
||||||
f"file at '{conflict.source}'"
|
|
||||||
)
|
)
|
||||||
self.by_uri[content.uri] = content
|
self.by_uri[output.uri] = output
|
||||||
|
|
||||||
def update_tags_by_count(self) -> None:
|
def update_tags_by_count(self) -> None:
|
||||||
tags = [(tag, len(items)) for tag, items in self.by_tag.items()]
|
tags = [(tag, len(items)) for tag, items in self.by_tag.items()]
|
||||||
@@ -100,6 +106,9 @@ class Site:
|
|||||||
|
|
||||||
def discover(self) -> None:
|
def discover(self) -> None:
|
||||||
log.info("Discovering content...")
|
log.info("Discovering content...")
|
||||||
|
pygments = Memory("pygments.css", highlight_style())
|
||||||
|
self.other_outputs.append(pygments)
|
||||||
|
self.add_by_uri(pygments)
|
||||||
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 is_content(file):
|
if file.is_file() and is_content(file):
|
||||||
@@ -184,9 +193,8 @@ class Site:
|
|||||||
raise ConfigError("base_uri config value must be an absolute path")
|
raise ConfigError("base_uri config value must be an absolute path")
|
||||||
self.discover()
|
self.discover()
|
||||||
self.output_path.mkdir(parents=True, exist_ok=True)
|
self.output_path.mkdir(parents=True, exist_ok=True)
|
||||||
(self.output_path / "pygments.css").write_text(
|
|
||||||
highlight_style(), encoding="utf-8"
|
|
||||||
)
|
|
||||||
for content in self.content:
|
for content in self.content:
|
||||||
content.write(self)
|
content.write(self)
|
||||||
self.render_indices()
|
self.render_indices()
|
||||||
|
for output in self.other_outputs:
|
||||||
|
output.write(self)
|
||||||
|
|||||||
Reference in New Issue
Block a user