diff --git a/stsg/__init__.py b/stsg/__init__.py index c6164a1..4b00972 100644 --- a/stsg/__init__.py +++ b/stsg/__init__.py @@ -8,6 +8,9 @@ class config: fallback_language = "en" preview_length = 400 preview_header_shift = 2 + markdown_extras = [ + "fenced-code-blocks" + ] languages = { "af": { diff --git a/stsg/build.py b/stsg/build.py index 9edba10..cbc995f 100644 --- a/stsg/build.py +++ b/stsg/build.py @@ -79,20 +79,7 @@ def shift_headings(html_string, header_shift=config.formatting.preview_header_sh def get_preview_text(html_string: str): return shift_headings(shorten_text_and_clean(html_string)) - - -def stem_to_language_code(stem: str) -> str: - language_code = stem.lower().replace("-", "_") - if language_code in config.languages: - return language_code - - language_code = language_code.split("_")[0] - if language_code in config.languages: - return language_code - - logger.error("Didn't recognize %s as a valid language code, add it to the config, or fix your structure.", stem) - exit(1) class TemplateDict(dict): @@ -140,28 +127,66 @@ def compile_cross_article_context(cross_article_context): cross_article_context["link"] = f'{title}' +class ArticleTranslationContext(TypedDict): + slug: str + name: str + datetime: str + author: str + url: str + + class ArticleTranslation: + article: Article + file: Path + + @cached_property + def html_content(self) -> str: + html_content = self.file.read_text() + if self.file.suffix == ".md": + return markdown(html_content, extras=config.formatting.markdown_extras) + return html_content + + @cached_property + def language_code(self) -> str: + language_code = self.file.stem.lower().replace("-", "_") + + if language_code in config.languages: + return language_code + + language_code = language_code.split("_")[0] + if language_code in config.languages: + return language_code + + logger.error("Didn't recognize %s as a valid language code, add it to the config, or fix your structure.", stem) + exit(1) + + @cached_property + def priority(self) -> int: + return LANGUAGES[self.language_code]["priority"] + + @cached_property + def slug_path(self) -> List[str]: + return [self.language_code, *self.article.slug_path] + + @cached_property + def url(self) -> str: + return "/" + "/".join(self.slug_path) + + @cached_property + def dist_path(self) -> Path: + return Path(config.setup.dist_directory, *self.slug_path) + + context: ArticleTranslationContext + cross_article_context: Dict[str, Any] + def __init__(self, file: Path, article: Article): - self.file = file self.article = article + self.file = file - self.context: Dict[str, Any] = {} - - # initializing the location of the article translation - self.language_code = stem_to_language_code(self.file.stem) - self.slug_path = [self.language_code, *self.article.slug_path[1:]] - self.url = "/" + "/".join(self.slug_path) - self.dist_path = Path(config.setup.dist_directory, *self.slug_path) + self.context = {} self.cross_article_context = TRANSLATED_CROSS_ARTICLE_CONTEXT[self.language_code][self.article.slug] = {} - - self.priority = LANGUAGES[self.language_code]["priority"] - self.real_language_code = LANGUAGES[self.language_code]["code"] - - self.html_content = self.file.read_text() - if self.file.suffix == ".md": - self.html_content = markdown(self.html_content, extras=["fenced-code-blocks"]) - + def __init_context__(self): self.context["meta"] = self.article.context_shared self.context["url"] = self.url @@ -252,15 +277,15 @@ class Article: @cached_property def slug_path(self) -> List[str]: - return [a.slug for a in self.article_path] + return [a.slug for a in self.article_path[1:]] @cached_property def url(self) -> str: - return "/" + "/".join(self.slug_path[1:]) + return "/" + "/".join(self.slug_path) @cached_property def dist_path(self) -> Path: - return Path(config.setup.dist_directory, *self.slug_path[1:]) + return Path(config.setup.dist_directory, *self.slug_path) context: ArticleContext context_shared: Dict[str, Any] @@ -322,7 +347,7 @@ class Article: child_article_list = self.context["children"] = [] for article_translation in self.article_translations_list: - self.context[article_translation.real_language_code] = article_translation.context + self.context[article_translation.language_code] = article_translation.context translation_list.append(article_translation.context) for child_article in self.child_articles: