refactoring article context building

This commit is contained in:
Hazel Noack 2025-05-22 13:43:34 +02:00
parent 753de66e08
commit f2a3d7ada4
2 changed files with 56 additions and 25 deletions

View File

@ -121,11 +121,11 @@ class LanguageDict(dict):
LANGUAGES = LanguageDict()
def compile_cross_article_context(cross_article_context):
title = cross_article_context["title"]
url = cross_article_context["url"]
def add_html_link(c):
name = c["name"]
url = c["url"]
cross_article_context["link"] = f'<a href="{url}">{title}</a>'
c["link"] = f'<a href="{url}">{name}</a>'
@ -193,7 +193,7 @@ class ArticleTranslation:
self.cross_article_context["title"] = self.context["title"]
self.cross_article_context["article_url"] = self.article.url
self.cross_article_context["url"] = self.url
compile_cross_article_context(self.cross_article_context)
add_html_link(self.cross_article_context)
# get children
self.context["children"] = [
@ -272,6 +272,26 @@ class Article:
context_shared: Dict[str, Any]
cross_article_context: Dict[str, Any]
child_articles: List[Article]
article_translations_list: List[ArticleTranslation]
article_translations_map: Dict[str, ArticleTranslation]
linked_articles: List[Article]
@cached_property
def related_articles(self) -> List[Article]:
used_slugs = set()
related = []
for a in [*self.child_articles, *self.linked_articles]:
if a.slug in used_slugs:
continue
used_slugs.add(a.slug)
related.append(a)
return related
def __init__(self, directory: Path, article_path: Optional[List[str]] = None, is_root: bool = False, parent: Optional[Article] = None):
self.directory = directory
@ -283,11 +303,13 @@ class Article:
self.cross_article_context = CROSS_ARTICLE_CONTEXT[self.slug] = {}
ARTICLE_LAKE[self.slug] = self
self.linked_articles = []
# build the tree
self.child_articles: List[Article] = []
self.article_translations_list: List[ArticleTranslation] = []
self.article_translations_map: Dict[str, ArticleTranslation] = {}
self.child_articles = []
self.article_translations_list = []
self.article_translations_map = {}
for c in self.directory.iterdir():
if c.name == "index.toml":
@ -328,29 +350,25 @@ class Article:
self.context["slug"] = self.slug
self.context["name"] = self.name
self.context["url"] = self.url
add_html_link(self.context)
self.context["date"] = self.modified_at.strftime(config.formatting.datetime_format)
self.context["iso_date"] = self.modified_at.isoformat()
self.context["author"] = self.author
self.cross_article_context.update(self.context_shared)
self.cross_article_context["title"] = self.context_shared["slug"]
self.cross_article_context["article_url"] = self.context_shared["url"]
compile_cross_article_context(self.cross_article_context)
# recursive context structures
translation_list = self.context["translations"] = []
child_article_list = self.context["children"] = []
self.context["translations"] = [t.context for t in self.self.article_translations_list]
self.context["children"] = [c.context for c in self.child_articles]
for lang, article in self.article_translations_map.items():
self.context[lang] = article.context
for article_translation in self.article_translations_list:
self.context[article_translation.language_code] = article_translation.context
translation_list.append(article_translation.context)
for child_article in self.child_articles:
child_article_list.append(child_article.context)
# recursively build context
for at in self.article_translations_list:
at.__init_context__()
# the __init_context__ functions of the translations needs to be called first
# because the linked articles will be set there
self.context["linked"] = [l.context for l in self.linked_articles]
self.context["related"] = [r.context for r in self.related_articles]
for a in self.child_articles:
a.__init_context__()

View File

@ -1,4 +1,5 @@
from typing import TypedDict
from __future__ import annotations
from typing import TypedDict, List
class ArticleConfig(TypedDict):
@ -12,15 +13,27 @@ class ArticleContext(TypedDict):
slug: str
name: str
url: str
link: str
date: str
iso_date: str
author: str
translations: List[ArticleTranslationContext]
children: List[ArticleContext]
linked: List[ArticleContext]
related: List[ArticleContext]
class ArticleTranslationContext(TypedDict):
slug: str
name: str
url: str
link: str
date: str
iso_date: str
author: str
translations: List[ArticleTranslationContext]
children: List[ArticleTranslationContext]
linked: List[ArticleTranslationContext]
related: List[ArticleTranslationContext]