From 263281df3c42e2847206a63c98c1aed5e88f9f34 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Wed, 16 Apr 2025 12:18:44 +0200 Subject: [PATCH] feat: renamed ArticleOverview to article --- stsg/build.py | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/stsg/build.py b/stsg/build.py index fe0066e..bcc9548 100644 --- a/stsg/build.py +++ b/stsg/build.py @@ -4,7 +4,7 @@ import shutil from pathlib import Path import os import markdown -from typing import Optional, Union, Dict, Generator, List, DefaultDict +from typing import Optional, Union, Dict, Generator, List, DefaultDict, Any from bs4 import BeautifulSoup from collections import defaultdict import toml @@ -40,11 +40,16 @@ class Template: self.article_card: str = (self.folder / "article_card.html").read_text() +class TemplateContext: + def __init__(self, name: str): + self.name = name + self.context: Dict[str, Any] = {} + class ArticleTranslation: - def __init__(self, file: Path, article_overview: ArticleOverview): + def __init__(self, file: Path, article: Article): self.file = file - self.article_overview = article_overview + self.article = article self.language_code = self.file.stem self.article_content = self.file.read_text() @@ -53,7 +58,7 @@ class ArticleTranslation: self.article_content = markdown.markdown(self.article_content) self.article_preview = markdown.markdown(self.article_preview) - self.location_in_tree = [self.language_code, *self.article_overview.location_in_tree] + self.location_in_tree = [self.language_code, *self.article.location_in_tree] self.url = "/" + "/".join(self.location_in_tree) self.dist_path = Path(config.setup.dist_directory, *self.location_in_tree) @@ -72,19 +77,14 @@ class ArticleTranslation: self.article_cards = "" - - def post_init(self): - """ - the initializing that takes place after the creation of the tree - """ + def __init_values__(self): article_card_strings = [] - for child in self.article_overview.child_articles: + for child in self.article.child_articles: if self.language_code in child.article_translations_map: article_card_strings.append(child.article_translations_map[self.language_code].get_article_card()) self.article_cards = "\n".join(article_card_strings) - def build(self): self.dist_path.mkdir(parents=True, exist_ok=True) @@ -96,11 +96,11 @@ class ArticleTranslation: "article_content": self.article_content, "article_preview": self.article_preview, "article_url": self.url, - "article_overview_url": self.article_overview.url, - "article_slug": self.article_overview.slug, + "article_overview_url": self.article.url, + "article_slug": self.article.slug, "article_title": self.title, - "article_datetime": self.article_overview.article_written.strftime(config.formatting.datetime_format), - "article_datetime_iso": self.article_overview.article_written.isoformat(), + "article_datetime": self.article.article_written.strftime(config.formatting.datetime_format), + "article_datetime_iso": self.article.article_written.isoformat(), "article_language_name": self.language_name, "article_language_code": self.language_code, "article_language_flag": self.language_flag, @@ -115,7 +115,7 @@ class ArticleTranslation: def get_article_values(self) -> Dict[str, str]: res = {} for key, value in self._get_values(return_foreign_articles=False).items(): - res[key + ":" + self.article_overview.slug] = value + res[key + ":" + self.article.slug] = value return res @@ -129,7 +129,7 @@ class ArticleTranslation: return replace_values(TEMPLATE.article_card, self._get_values()) -class ArticleOverview: +class Article: def __init__(self, directory: Path, location_in_tree: Optional[List[str]] = None, is_root: bool = False): self.directory = directory @@ -152,7 +152,7 @@ class ArticleOverview: exit(1) ARTICLE_LAKE[self.slug] = self - self.child_articles: List[ArticleOverview] = [] + self.child_articles: List[Article] = [] self.article_translations: List[ArticleTranslation] = [] self.article_translations_map: Dict[str, ArticleTranslation] = {} @@ -165,7 +165,7 @@ class ArticleOverview: self.article_translations.append(at) self.article_translations_map[at.language_code] = at elif c.is_dir(): - self.child_articles.append(ArticleOverview( + self.child_articles.append(Article( directory=c, location_in_tree=self.location_in_tree.copy(), )) @@ -176,7 +176,7 @@ class ArticleOverview: self.article_cards = "\n".join(c.get_article_card() for c in self.child_articles) for at in self.article_translations: - at.post_init() + at.__init_values__() logger.info("found %s at %s with the translations %s", self.slug, ".".join(list(self.location_in_tree)), ",".join(self.article_translations_map.keys())) @@ -227,7 +227,7 @@ class ArticleOverview: # GLOBALS logger = logging.getLogger("stsg.build") TEMPLATE = Template(Path(config.setup.source_directory, "templates")) -ARTICLE_LAKE: Dict[str, ArticleOverview] = {} +ARTICLE_LAKE: Dict[str, Article] = {} ARTICLE_REFERENCE_VALUES: DefaultDict[str, Dict[str, str]] = defaultdict(dict) def build(): @@ -237,7 +237,7 @@ def build(): shutil.copytree(Path(config.setup.source_directory, "static"), Path(config.setup.dist_directory, "static"), dirs_exist_ok=True) logger.info("reading page tree...") - tree = ArticleOverview(directory=Path(config.setup.source_directory, "articles"), is_root=True) + tree = Article(directory=Path(config.setup.source_directory, "articles"), is_root=True) # build article reverence values for article_overview in ARTICLE_LAKE.values():