feat: renamed ArticleOverview to article

This commit is contained in:
Hazel 2025-04-16 12:18:44 +02:00
parent b953933e6f
commit 263281df3c

View File

@ -4,7 +4,7 @@ import shutil
from pathlib import Path from pathlib import Path
import os import os
import markdown 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 bs4 import BeautifulSoup
from collections import defaultdict from collections import defaultdict
import toml import toml
@ -40,11 +40,16 @@ class Template:
self.article_card: str = (self.folder / "article_card.html").read_text() 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: class ArticleTranslation:
def __init__(self, file: Path, article_overview: ArticleOverview): def __init__(self, file: Path, article: Article):
self.file = file self.file = file
self.article_overview = article_overview self.article = article
self.language_code = self.file.stem self.language_code = self.file.stem
self.article_content = self.file.read_text() self.article_content = self.file.read_text()
@ -53,7 +58,7 @@ class ArticleTranslation:
self.article_content = markdown.markdown(self.article_content) self.article_content = markdown.markdown(self.article_content)
self.article_preview = markdown.markdown(self.article_preview) 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.url = "/" + "/".join(self.location_in_tree)
self.dist_path = Path(config.setup.dist_directory, *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 = "" self.article_cards = ""
def __init_values__(self):
def post_init(self):
"""
the initializing that takes place after the creation of the tree
"""
article_card_strings = [] 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: if self.language_code in child.article_translations_map:
article_card_strings.append(child.article_translations_map[self.language_code].get_article_card()) article_card_strings.append(child.article_translations_map[self.language_code].get_article_card())
self.article_cards = "\n".join(article_card_strings) self.article_cards = "\n".join(article_card_strings)
def build(self): def build(self):
self.dist_path.mkdir(parents=True, exist_ok=True) self.dist_path.mkdir(parents=True, exist_ok=True)
@ -96,11 +96,11 @@ class ArticleTranslation:
"article_content": self.article_content, "article_content": self.article_content,
"article_preview": self.article_preview, "article_preview": self.article_preview,
"article_url": self.url, "article_url": self.url,
"article_overview_url": self.article_overview.url, "article_overview_url": self.article.url,
"article_slug": self.article_overview.slug, "article_slug": self.article.slug,
"article_title": self.title, "article_title": self.title,
"article_datetime": self.article_overview.article_written.strftime(config.formatting.datetime_format), "article_datetime": self.article.article_written.strftime(config.formatting.datetime_format),
"article_datetime_iso": self.article_overview.article_written.isoformat(), "article_datetime_iso": self.article.article_written.isoformat(),
"article_language_name": self.language_name, "article_language_name": self.language_name,
"article_language_code": self.language_code, "article_language_code": self.language_code,
"article_language_flag": self.language_flag, "article_language_flag": self.language_flag,
@ -115,7 +115,7 @@ class ArticleTranslation:
def get_article_values(self) -> Dict[str, str]: def get_article_values(self) -> Dict[str, str]:
res = {} res = {}
for key, value in self._get_values(return_foreign_articles=False).items(): 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 return res
@ -129,7 +129,7 @@ class ArticleTranslation:
return replace_values(TEMPLATE.article_card, self._get_values()) 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): def __init__(self, directory: Path, location_in_tree: Optional[List[str]] = None, is_root: bool = False):
self.directory = directory self.directory = directory
@ -152,7 +152,7 @@ class ArticleOverview:
exit(1) exit(1)
ARTICLE_LAKE[self.slug] = self ARTICLE_LAKE[self.slug] = self
self.child_articles: List[ArticleOverview] = [] self.child_articles: List[Article] = []
self.article_translations: List[ArticleTranslation] = [] self.article_translations: List[ArticleTranslation] = []
self.article_translations_map: Dict[str, ArticleTranslation] = {} self.article_translations_map: Dict[str, ArticleTranslation] = {}
@ -165,7 +165,7 @@ class ArticleOverview:
self.article_translations.append(at) self.article_translations.append(at)
self.article_translations_map[at.language_code] = at self.article_translations_map[at.language_code] = at
elif c.is_dir(): elif c.is_dir():
self.child_articles.append(ArticleOverview( self.child_articles.append(Article(
directory=c, directory=c,
location_in_tree=self.location_in_tree.copy(), 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) self.article_cards = "\n".join(c.get_article_card() for c in self.child_articles)
for at in self.article_translations: 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())) 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 # GLOBALS
logger = logging.getLogger("stsg.build") logger = logging.getLogger("stsg.build")
TEMPLATE = Template(Path(config.setup.source_directory, "templates")) 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) ARTICLE_REFERENCE_VALUES: DefaultDict[str, Dict[str, str]] = defaultdict(dict)
def build(): 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) shutil.copytree(Path(config.setup.source_directory, "static"), Path(config.setup.dist_directory, "static"), dirs_exist_ok=True)
logger.info("reading page tree...") 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 # build article reverence values
for article_overview in ARTICLE_LAKE.values(): for article_overview in ARTICLE_LAKE.values():