From 8a8f02c5bd55c5a391e81bb3fc27b3de35fad07d Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Wed, 16 Apr 2025 15:45:06 +0200 Subject: [PATCH] feat: implemented code to use with jinja --- src/templates/article.html | 47 ++++++++++---- src/templates/article_translation.html | 44 +++++++++++++ src/templates/overview.html | 69 --------------------- stsg/build.py | 85 ++++++++------------------ 4 files changed, 107 insertions(+), 138 deletions(-) create mode 100644 src/templates/article_translation.html delete mode 100644 src/templates/overview.html diff --git a/src/templates/article.html b/src/templates/article.html index 8fb16e6..aad4b13 100644 --- a/src/templates/article.html +++ b/src/templates/article.html @@ -1,9 +1,9 @@ - + - {article_language_flag} {article_title} + {article_title} @@ -14,21 +14,27 @@ aria-label="main navigation" > -
-
- {article_content} +
+
+

Translations

+
+
+ {article_translation_cards} +
+
-

Further Reading

- -
+
+
+

Child Articles

+
+
{article_children_cards}
@@ -41,4 +47,23 @@
+ + diff --git a/src/templates/article_translation.html b/src/templates/article_translation.html new file mode 100644 index 0000000..8fb16e6 --- /dev/null +++ b/src/templates/article_translation.html @@ -0,0 +1,44 @@ + + + + + + {article_language_flag} {article_title} + + + + + + + +
+
+ {article_content} + +

Further Reading

+ +
+ {article_children_cards} +
+
+
+ + +
+
+

STSG by Hazel. © 2025

+
+
+ + diff --git a/src/templates/overview.html b/src/templates/overview.html deleted file mode 100644 index aad4b13..0000000 --- a/src/templates/overview.html +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - {article_title} - - - - - - -
-
-
-

Translations

-
-
- {article_translation_cards} -
-
- -
-
-

Child Articles

-
-
- {article_children_cards} -
-
-
- - -
-
-

STSG by Hazel. © 2025

-
-
- - - - diff --git a/stsg/build.py b/stsg/build.py index feac937..c90da8d 100644 --- a/stsg/build.py +++ b/stsg/build.py @@ -9,6 +9,7 @@ from bs4 import BeautifulSoup from collections import defaultdict import toml from datetime import datetime +import jinja2 from . import config @@ -82,6 +83,24 @@ def stem_to_language_code(stem: str) -> str: exit(1) + +class TemplateDict(dict): + def __init__(self, folder: Path): + self.folder = folder + super().__init__() + + def __missing__(self, name: str) -> jinja2.Template: + f = self.folder / (name + ".html") + if not f.exists(): + logger.error("no template with the name %s exists", name) + exit(1) + + t = jinja2.Template(f.read_text()) + self[name] = t + return t + +TEMPLATE: Dict[str, jinja2.Template] = TemplateDict(Path(config.setup.source_directory, "templates")) + class LanguageDict(dict): def __missing__(self, key: str): if key not in config.languages: @@ -99,15 +118,6 @@ class LanguageDict(dict): LANGUAGES = LanguageDict() -class Template: - def __init__(self, folder: Path): - self.folder = folder - - self.article: str = (self.folder / "article.html").read_text() - self.overview: str = (self.folder / "overview.html").read_text() - self.translation_card: str = (self.folder / "translation_card.html").read_text() - self.article_card: str = (self.folder / "article_card.html").read_text() - class ArticleTranslation: def __init__(self, file: Path, article: Article): @@ -143,6 +153,7 @@ class ArticleTranslation: if self.file.suffix == ".md": html_content = markdown.markdown(html_content) + self.context["title"] = get_first_header_content(html_content, fallback=LANGUAGES[self.language_code]["native_name"]) self.context["content"] = html_content self.context["preview"] = shorten_text_and_clean(html_string=html_content) @@ -150,35 +161,7 @@ class ArticleTranslation: self.dist_path.mkdir(parents=True, exist_ok=True) with Path(self.dist_path, "index.html").open("w") as f: - f.write(self.get_article()) - - def _get_values(self, return_foreign_articles: bool = True) -> Dict[str, str]: - r = { - "article_content": self.article_content, - "article_preview": self.article_preview, - "article_title": self.title, - } - - if return_foreign_articles: - r.update(ARTICLE_REFERENCE_VALUES[self.language_code]) - - return r - - 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.slug] = value - - return res - - def get_article(self) -> str: - return replace_values(TEMPLATE.article, self._get_values()) - - def get_translation_card(self) -> str: - return replace_values(TEMPLATE.translation_card, self._get_values()) - - def get_article_card(self) -> str: - return replace_values(TEMPLATE.article_card, self._get_values()) + f.write(TEMPLATE["article_translation"].render(self.context)) class Article: @@ -228,10 +211,6 @@ class Article: 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) - def __init_context__(self): self.context["url"] = self.url self.context_meta["slug"] = self.slug @@ -257,20 +236,17 @@ class Article: for a in self.child_articles: a.__init_context__() - - - def build(self): - # builds the tree structure to the dist directory self.dist_path.mkdir(parents=True, exist_ok=True) + with Path(self.dist_path, "index.html").open("w") as f: - f.write(self.get_overview()) + f.write(TEMPLATE["article"].render(self.context)) for at in self.article_translations_list: at.build() - for ca in self.child_articles: - ca.build() + for ac in self.child_articles: + ac.build() def _get_values(self, return_foreign_articles: bool = True) -> Dict[str, str]: r = { @@ -301,7 +277,6 @@ class Article: # GLOBALS logger = logging.getLogger("stsg.build") -TEMPLATE = Template(Path(config.setup.source_directory, "templates")) ARTICLE_LAKE: Dict[str, Article] = {} ARTICLE_REFERENCE_VALUES: DefaultDict[str, Dict[str, str]] = defaultdict(dict) @@ -323,11 +298,5 @@ def build(): with Path("context.json").open("w") as f: json.dump(tree.context, f, indent=4) - # build article reverence values - for article_overview in ARTICLE_LAKE.values(): - ARTICLE_REFERENCE_VALUES[""].update(article_overview.get_article_values()) - for language_code, at in article_overview.article_translations_map.items(): - ARTICLE_REFERENCE_VALUES[language_code].update(at.get_article_values()) - - logger.info("writing page tree...") - # tree.build() \ No newline at end of file + logger.info("dumping page tree...") + tree.build() \ No newline at end of file