Compare commits
4 Commits
cf8e2955c2
...
c9fb8fda93
Author | SHA1 | Date | |
---|---|---|---|
c9fb8fda93 | |||
1fae03e70b | |||
6ed94db8cf | |||
02a7c29dba |
@ -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
|
||||||
|
|
||||||
@ -85,6 +85,7 @@ class ArticleTranslation:
|
|||||||
self.dist_path = Path(config.setup.dist_directory, *self.location_in_tree)
|
self.dist_path = Path(config.setup.dist_directory, *self.location_in_tree)
|
||||||
|
|
||||||
self.priority = LANGUAGES[self.language_code]["priority"]
|
self.priority = LANGUAGES[self.language_code]["priority"]
|
||||||
|
self.real_language_code = LANGUAGES[self.language_code]["code"]
|
||||||
|
|
||||||
# TODO remove
|
# TODO remove
|
||||||
self.article_content = self.file.read_text()
|
self.article_content = self.file.read_text()
|
||||||
@ -110,11 +111,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 +161,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 +172,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 +180,49 @@ 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)
|
||||||
|
|
||||||
|
def __init_context__(self):
|
||||||
|
self.context["url"] = self.url
|
||||||
|
self.context_meta["slug"] = self.slug
|
||||||
|
|
||||||
|
modified_at = datetime.fromisoformat(self.config["datetime"]) if "datetime" in self.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()
|
||||||
|
|
||||||
|
# recursive context structures
|
||||||
|
translation_list = self.context["translations"] = []
|
||||||
|
child_article_list = self.context["children"] = []
|
||||||
|
|
||||||
|
for article_translation in self.article_translations_list:
|
||||||
|
self.context[article_translation.real_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__()
|
||||||
|
for a in self.child_articles:
|
||||||
|
a.__init_context__()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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 +230,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,
|
||||||
@ -252,8 +267,13 @@ def build():
|
|||||||
logger.info("copying static folder...")
|
logger.info("copying static folder...")
|
||||||
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("building page tree...")
|
||||||
tree = Article(directory=Path(config.setup.source_directory, "articles"), is_root=True)
|
tree = Article(directory=Path(config.setup.source_directory, "articles"), is_root=True)
|
||||||
|
logger.info("compiling tree context...")
|
||||||
|
tree.__init_context__()
|
||||||
|
|
||||||
|
import json
|
||||||
|
print(json.dumps(tree.context, indent=4))
|
||||||
|
|
||||||
# build article reverence values
|
# build article reverence values
|
||||||
for article_overview in ARTICLE_LAKE.values():
|
for article_overview in ARTICLE_LAKE.values():
|
||||||
@ -262,4 +282,4 @@ def build():
|
|||||||
ARTICLE_REFERENCE_VALUES[language_code].update(at.get_article_values())
|
ARTICLE_REFERENCE_VALUES[language_code].update(at.get_article_values())
|
||||||
|
|
||||||
logger.info("writing page tree...")
|
logger.info("writing page tree...")
|
||||||
tree.build()
|
# tree.build()
|
Loading…
x
Reference in New Issue
Block a user