Compare commits

..

7 Commits

2 changed files with 38 additions and 19 deletions

View File

@ -10,8 +10,6 @@ from bs4 import BeautifulSoup
from .config import SOURCE_DIRECTORY, DIST_DIRECTORY, LANGUAGE_INFORMATION, ARTICLE_PREVIEW_LENGTH, DEFAULT_LANGUAGE
logger = logging.getLogger("stsg.build")
def replace_values(template: str, values: Dict[str, str]) -> str:
for key, value in values.items():
@ -38,7 +36,6 @@ class Template:
self.overview: str = (self.folder / "overview.html").read_text()
self.overview_card: str = (self.folder / "overview_card.html").read_text()
TEMPLATE = Template(Path(SOURCE_DIRECTORY, "templates"))
class ArticleTranslation:
@ -51,8 +48,9 @@ class ArticleTranslation:
if self.file.suffix == ".md":
self.article_content = markdown.markdown(self.article_content)
self.url = "/" + self.language_code + self.article_overview.url
self.dist_path = Path(DIST_DIRECTORY, self.url.strip("/"))
self.location_in_tree = [self.language_code, *self.article_overview.location_in_tree]
self.url = "/" + "/".join(self.location_in_tree)
self.dist_path = Path(DIST_DIRECTORY, *self.location_in_tree)
_language_info = DEFAULT_LANGUAGE
parsed_language_code = self.language_code.lower().replace("-", "_")
@ -94,12 +92,20 @@ class ArticleTranslation:
class ArticleOverview:
def __init__(self, directory: Path, url: str = ""):
def __init__(self, directory: Path, location_in_tree: Optional[List[str]] = None):
self.directory = directory
self.name = self.directory.name
self.slug = self.directory.name
self.location_in_tree: List[str] = location_in_tree or []
self.location_in_tree.append(self.slug)
self.url = "/" + "/".join(self.location_in_tree)
self.dist_path = Path(DIST_DIRECTORY, *self.location_in_tree)
if self.slug in ARTICLE_LAKE:
logger.error("two articles have the same name at %s and %r", ARTICLE_LAKE[self.slug].directory, self.directory)
exit(1)
ARTICLE_LAKE[self.slug] = self
self.url = url + "/" + self.name
self.dist_path = Path(DIST_DIRECTORY, self.url.strip("/"))
self.child_articles: List[ArticleOverview] = []
self.article_translations: List[ArticleTranslation] = []
@ -108,7 +114,10 @@ class ArticleOverview:
if c.is_file():
self.article_translations.append(ArticleTranslation(c, self))
elif c.is_dir():
self.child_articles.append(ArticleOverview(c, self.url))
self.child_articles.append(ArticleOverview(
directory=c,
location_in_tree=self.location_in_tree,
))
# the tree is built
self.article_translations.sort(key=lambda a: a.priority, reverse=True)
@ -126,11 +135,10 @@ class ArticleOverview:
for ca in self.child_articles:
ca.build()
def _get_values(self) -> Dict[str, str]:
return {
"overview_cards": self.overview_cards,
"overview_slug": self.name,
"overview_slug": self.slug,
}
def get_overview(self) -> str:
@ -171,9 +179,17 @@ def copy_static():
shutil.copy2(src_file, dest_file)
# GLOBALS
logger = logging.getLogger("stsg.build")
TEMPLATE = Template(Path(SOURCE_DIRECTORY, "templates"))
ARTICLE_LAKE: Dict[str, ArticleOverview] = {}
def build():
logger.info("building static page")
copy_static()
tree = ArticleOverview(directory=Path(SOURCE_DIRECTORY, "pages"))
print(ARTICLE_LAKE)
tree.build()

View File

@ -10,13 +10,16 @@ ARTICLE_PREVIEW_LENGTH = 200
# FOR DEVELOPMENT
CODE_DIRECTORY = "stsg"
class TypedLanguageInformation(typing_extensions.TypedDict):
flag: str
name: str
native_name: str
priority: typing_extensions.NotRequired[int]
# LANGUAGE INFORMATION
LANGUAGE_INFORMATION: typing.Dict[str, {
"flag": str,
"name": str,
"native_name": str,
"priority": typing_extensions.NotRequired[int],
}] = {
LANGUAGE_INFORMATION: typing.Dict[str, TypedLanguageInformation] = {
"af": {
"flag": "🇿🇦",
"name": "Afrikaans",
@ -766,4 +769,4 @@ LANGUAGE_INFORMATION: typing.Dict[str, {
}
DEFAULT_LANGUAGE = LANGUAGE_INFORMATION["de"]
DEFAULT_LANGUAGE: TypedLanguageInformation = LANGUAGE_INFORMATION["de"]