Create Tags(Output) class and move tag rendering there from Site
This commit is contained in:
@@ -3,6 +3,7 @@ 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.index import Index, discover_index
|
from omicron.ssg.output.index import Index, discover_index
|
||||||
from omicron.ssg.output.memory import Memory
|
from omicron.ssg.output.memory import Memory
|
||||||
|
from omicron.ssg.output.tags import Tags, discover_tags
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"Output",
|
"Output",
|
||||||
@@ -13,4 +14,6 @@ __all__ = [
|
|||||||
"Index",
|
"Index",
|
||||||
"discover_index",
|
"discover_index",
|
||||||
"Memory",
|
"Memory",
|
||||||
|
"Tags",
|
||||||
|
"discover_tags",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
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 Tags(Output):
|
||||||
|
def __init__(self, tags: list[tuple[str, int]]):
|
||||||
|
super().__init__("tags/index.html")
|
||||||
|
self.tags = tags
|
||||||
|
|
||||||
|
def write(self, site: Site) -> None:
|
||||||
|
template = site.jinja_env.get_template("tags.html")
|
||||||
|
html = template.render(site=site, tags=self.tags)
|
||||||
|
dest = site.output_path / self.uri
|
||||||
|
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
dest.write_text(html, encoding="utf-8")
|
||||||
|
log.debug("Wrote %s", dest)
|
||||||
|
|
||||||
|
|
||||||
|
def discover_tags(site: Site) -> Tags:
|
||||||
|
return Tags(site.tags_by_count)
|
||||||
+4
-8
@@ -10,6 +10,7 @@ from omicron.ssg.output import (
|
|||||||
create_content,
|
create_content,
|
||||||
Memory,
|
Memory,
|
||||||
discover_index,
|
discover_index,
|
||||||
|
discover_tags,
|
||||||
)
|
)
|
||||||
from omicron.ssg.markdown import highlight_style
|
from omicron.ssg.markdown import highlight_style
|
||||||
|
|
||||||
@@ -120,15 +121,11 @@ class Site:
|
|||||||
for index in discover_index(self):
|
for index in discover_index(self):
|
||||||
self.add_by_uri(index)
|
self.add_by_uri(index)
|
||||||
self.other_outputs.append(index)
|
self.other_outputs.append(index)
|
||||||
|
tags_page = discover_tags(self)
|
||||||
|
self.add_by_uri(tags_page)
|
||||||
|
self.other_outputs.append(tags_page)
|
||||||
log.info("Discovered %d content items", len(self.content))
|
log.info("Discovered %d content items", len(self.content))
|
||||||
|
|
||||||
def render_tags_page(self) -> None:
|
|
||||||
template = self.jinja_env.get_template("tags.html")
|
|
||||||
html = template.render(site=self, tags=self.tags_by_count)
|
|
||||||
tags_dir = self.output_path / "tags"
|
|
||||||
tags_dir.mkdir(parents=True, exist_ok=True)
|
|
||||||
(tags_dir / "index.html").write_text(html, encoding="utf-8")
|
|
||||||
|
|
||||||
def build(self) -> None:
|
def build(self) -> None:
|
||||||
if not self.origin:
|
if not self.origin:
|
||||||
log.warning(
|
log.warning(
|
||||||
@@ -141,6 +138,5 @@ class Site:
|
|||||||
self.output_path.mkdir(parents=True, exist_ok=True)
|
self.output_path.mkdir(parents=True, exist_ok=True)
|
||||||
for content in self.content:
|
for content in self.content:
|
||||||
content.write(self)
|
content.write(self)
|
||||||
self.render_tags_page()
|
|
||||||
for output in self.other_outputs:
|
for output in self.other_outputs:
|
||||||
output.write(self)
|
output.write(self)
|
||||||
|
|||||||
Reference in New Issue
Block a user