Compare commits

..

No commits in common. "8591ae53fa82d76cbf31321d5ce7409018b3637b" and "b2748a6cf3bee48cd2b426c3b407b6e28b9a46b7" have entirely different histories.

4 changed files with 8 additions and 59 deletions

View File

@ -7,8 +7,6 @@ from yaml import Loader, load
class Content:
current_language: str | None = None
def __init__(self, path: Path) -> None:
self.__path = path
@ -31,20 +29,17 @@ class ContentDirectory(Content):
def glob(self, pattern: str) -> Iterable[Content]:
for item in self.path.glob(pattern):
yield self.load(str(item.relative_to(self.path)))
def __load_children(self, name: str) -> Content:
child_path = self.__get_localized_path(self.path / name)
return self.__load_path(child_path)
yield self.load(str(item))
@cache # noqa: B019
def __load_path(self, child_path: Path) -> Content:
def __load_children(self, name: str) -> Content:
child_path = self.path / name
if not child_path.exists():
raise FileNotFoundError(child_path)
if child_path.is_dir():
return ContentDirectory(child_path)
if child_path.is_file():
if child_path.suffix in [".yml", ".yaml", ".json"]:
return DataFile(child_path)
@ -53,17 +48,6 @@ class ContentDirectory(Content):
raise NotImplementedError()
@staticmethod
def __get_localized_path(path: Path) -> Path:
if Content.current_language is None:
return path
localized_path = path.with_name(f"{path.stem}-{Content.current_language}{path.suffix}")
print(localized_path)
if not localized_path.exists():
return path
return localized_path
class DataField:
def __init__(self, file_path: Path, value: Any) -> None:

View File

@ -1,5 +1,3 @@
import gettext
from gettext import GNUTranslations, NullTranslations
from pathlib import Path
from shutil import copy
from typing import Any
@ -7,7 +5,7 @@ from typing import Any
from jinja2.environment import Environment
from jinja2.loaders import FileSystemLoader
from jwebsite.content import Content, ContentDirectory
from jwebsite.content import ContentDirectory
from jwebsite.git import git_creation_date
@ -18,34 +16,16 @@ class Site:
self.__environment = Environment(loader=FileSystemLoader(searchpath=root_directory / "src"))
self.__environment.filters.update({"output": self.__output, "git_creation_date": git_creation_date})
self.__content = ContentDirectory(root_directory / "content")
self.__translations: dict[str, GNUTranslations | NullTranslations] = {}
@property
def content(self) -> ContentDirectory:
return self.__content
def set_translations(self, domain: str, locale_dir: str, languages: list[str]) -> None:
self.__environment.add_extension("jinja2.ext.i18n")
self.__translations["en"] = NullTranslations()
for language in languages:
self.__translations[language] = gettext.translation(
domain, localedir=str(locale_dir), languages=[language]
)
def render(self, source: str, output: str | Path, **context: Any) -> None:
if self.__translations:
for language, translation in self.__translations.items():
Content.current_language = language
self.__environment.install_gettext_translations(translation, newstyle=True) # type: ignore
self.__render(source, self.__output_directory / language / output, **context)
self.__environment.uninstall_gettext_translations(translation) # type: ignore
else:
self.__render(source, self.__output_directory / output, **context)
def __render(self, source: str, output_path: Path, **context: Any) -> None:
output_path.parent.mkdir(exist_ok=True, parents=True)
self.__output_directory.mkdir(parents=True, exist_ok=True)
template = self.__environment.get_template(source)
content = template.render(site=self, **context)
output_path = self.__output_directory / output
with open(output_path, "w") as output_file:
output_file.write(content)

View File

@ -2,7 +2,7 @@ from pathlib import Path
from pytest import raises
from jwebsite.content import Content, ContentDirectory
from jwebsite.content import ContentDirectory
def test_load_directory(datadir: Path) -> None:
@ -45,12 +45,3 @@ def test_load_markdown(datadir: Path) -> None:
assert page.html == "<p>Content</p>"
assert str(page.meta["title"]) == "Title"
def test_localized_content(datadir: Path) -> None:
content = ContentDirectory(datadir)
Content.current_language = "fr"
page = content.load(Path("page.md"))
assert page.html == "<p>Contenu</p>"
assert str(page.meta["title"]) == "Titre"

View File

@ -1,6 +0,0 @@
---
title: Titre
---
Contenu