feat: add flat context to article

This commit is contained in:
Hazel 2025-04-16 14:05:16 +02:00
parent cf8e2955c2
commit 02a7c29dba

View File

@ -8,7 +8,7 @@ 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
import datetime from datetime import datetime
from . import config from . import config
@ -110,11 +110,8 @@ 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.url,
"article_slug": self.article.slug, "article_slug": self.article.slug,
"article_title": self.title, "article_title": self.title,
"article_datetime": self.article.article_written.strftime(config.formatting.datetime_format),
"article_datetime_iso": self.article.article_written.isoformat(),
} }
if return_foreign_articles: if return_foreign_articles:
@ -163,13 +160,9 @@ class Article:
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)
# build the tree
self.article_written = datetime.datetime.fromisoformat(article_config["datetime"]) if "datetime" in article_config else datetime.datetime.fromtimestamp(self.directory.stat().st_mtime)
self.child_articles: List[Article] = [] self.child_articles: List[Article] = []
self.article_translations: List[ArticleTranslation] = [] self.article_translations_list: List[ArticleTranslation] = []
self.article_translations_map: Dict[str, ArticleTranslation] = {} self.article_translations_map: Dict[str, ArticleTranslation] = {}
for c in self.directory.iterdir(): for c in self.directory.iterdir():
@ -178,7 +171,7 @@ class Article:
if c.is_file(): if c.is_file():
at = ArticleTranslation(c, self) at = ArticleTranslation(c, self)
self.article_translations.append(at) self.article_translations_list.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(Article( self.child_articles.append(Article(
@ -186,23 +179,37 @@ class Article:
location_in_tree=self.location_in_tree.copy(), location_in_tree=self.location_in_tree.copy(),
)) ))
# the tree is built self.article_translations_list.sort(key=lambda a: a.priority, reverse=True)
self.article_translations.sort(key=lambda a: a.priority, reverse=True)
self.translation_cards = "\n".join(a.get_translation_card() for a in self.article_translations)
self.article_cards = "\n".join(c.get_article_card() for c in self.child_articles)
for at in self.article_translations:
at.__init_context__()
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()))
# the tree is built
self.translation_cards = "\n".join(a.get_translation_card() for a in self.article_translations_list)
self.article_cards = "\n".join(c.get_article_card() for c in self.child_articles)
for at in self.article_translations_list:
at.__init_context__()
self.__init_context__()
def __init_context__(self):
self.context["url"] = self.url
self.context_meta["slug"] = self.slug
modified_at = datetime.fromisoformat(article_config["datetime"]) if "datetime" in article_config else datetime.fromtimestamp(self.directory.stat().st_mtime)
self.context_meta["date"] = modified_at.strftime(config.formatting.datetime_format)
self.context_meta["iso_date"] = modified_at.isoformat()
def build(self): def build(self):
# builds the tree structure to the dist directory # builds the tree structure to the dist directory
self.dist_path.mkdir(parents=True, exist_ok=True) self.dist_path.mkdir(parents=True, exist_ok=True)
with Path(self.dist_path, "index.html").open("w") as f: with Path(self.dist_path, "index.html").open("w") as f:
f.write(self.get_overview()) f.write(self.get_overview())
for at in self.article_translations: for at in self.article_translations_list:
at.build() at.build()
for ca in self.child_articles: for ca in self.child_articles:
@ -210,11 +217,6 @@ class Article:
def _get_values(self, return_foreign_articles: bool = True) -> Dict[str, str]: def _get_values(self, return_foreign_articles: bool = True) -> Dict[str, str]:
r = { r = {
"article_url": self.url,
"article_title": self.slug,
"article_slug": self.slug,
"article_datetime": self.article_written.strftime(config.formatting.datetime_format),
"article_datetime_iso": self.article_written.isoformat(),
"article_overview_url": self.url, "article_overview_url": self.url,
"article_translation_cards": self.translation_cards, "article_translation_cards": self.translation_cards,
"article_children_cards": self.article_cards, "article_children_cards": self.article_cards,