Compare commits
No commits in common. "8591ae53fa82d76cbf31321d5ce7409018b3637b" and "b2748a6cf3bee48cd2b426c3b407b6e28b9a46b7" have entirely different histories.
8591ae53fa
...
b2748a6cf3
|
|
@ -7,8 +7,6 @@ from yaml import Loader, load
|
||||||
|
|
||||||
|
|
||||||
class Content:
|
class Content:
|
||||||
current_language: str | None = None
|
|
||||||
|
|
||||||
def __init__(self, path: Path) -> None:
|
def __init__(self, path: Path) -> None:
|
||||||
self.__path = path
|
self.__path = path
|
||||||
|
|
||||||
|
|
@ -31,20 +29,17 @@ class ContentDirectory(Content):
|
||||||
|
|
||||||
def glob(self, pattern: str) -> Iterable[Content]:
|
def glob(self, pattern: str) -> Iterable[Content]:
|
||||||
for item in self.path.glob(pattern):
|
for item in self.path.glob(pattern):
|
||||||
yield self.load(str(item.relative_to(self.path)))
|
yield self.load(str(item))
|
||||||
|
|
||||||
def __load_children(self, name: str) -> Content:
|
|
||||||
child_path = self.__get_localized_path(self.path / name)
|
|
||||||
return self.__load_path(child_path)
|
|
||||||
|
|
||||||
@cache # noqa: B019
|
@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():
|
if not child_path.exists():
|
||||||
raise FileNotFoundError(child_path)
|
raise FileNotFoundError(child_path)
|
||||||
|
|
||||||
if child_path.is_dir():
|
if child_path.is_dir():
|
||||||
return ContentDirectory(child_path)
|
return ContentDirectory(child_path)
|
||||||
|
|
||||||
if child_path.is_file():
|
if child_path.is_file():
|
||||||
if child_path.suffix in [".yml", ".yaml", ".json"]:
|
if child_path.suffix in [".yml", ".yaml", ".json"]:
|
||||||
return DataFile(child_path)
|
return DataFile(child_path)
|
||||||
|
|
@ -53,17 +48,6 @@ class ContentDirectory(Content):
|
||||||
|
|
||||||
raise NotImplementedError()
|
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:
|
class DataField:
|
||||||
def __init__(self, file_path: Path, value: Any) -> None:
|
def __init__(self, file_path: Path, value: Any) -> None:
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
import gettext
|
|
||||||
from gettext import GNUTranslations, NullTranslations
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from shutil import copy
|
from shutil import copy
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
@ -7,7 +5,7 @@ from typing import Any
|
||||||
from jinja2.environment import Environment
|
from jinja2.environment import Environment
|
||||||
from jinja2.loaders import FileSystemLoader
|
from jinja2.loaders import FileSystemLoader
|
||||||
|
|
||||||
from jwebsite.content import Content, ContentDirectory
|
from jwebsite.content import ContentDirectory
|
||||||
from jwebsite.git import git_creation_date
|
from jwebsite.git import git_creation_date
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -18,34 +16,16 @@ class Site:
|
||||||
self.__environment = Environment(loader=FileSystemLoader(searchpath=root_directory / "src"))
|
self.__environment = Environment(loader=FileSystemLoader(searchpath=root_directory / "src"))
|
||||||
self.__environment.filters.update({"output": self.__output, "git_creation_date": git_creation_date})
|
self.__environment.filters.update({"output": self.__output, "git_creation_date": git_creation_date})
|
||||||
self.__content = ContentDirectory(root_directory / "content")
|
self.__content = ContentDirectory(root_directory / "content")
|
||||||
self.__translations: dict[str, GNUTranslations | NullTranslations] = {}
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def content(self) -> ContentDirectory:
|
def content(self) -> ContentDirectory:
|
||||||
return self.__content
|
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:
|
def render(self, source: str, output: str | Path, **context: Any) -> None:
|
||||||
if self.__translations:
|
self.__output_directory.mkdir(parents=True, exist_ok=True)
|
||||||
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)
|
|
||||||
template = self.__environment.get_template(source)
|
template = self.__environment.get_template(source)
|
||||||
content = template.render(site=self, **context)
|
content = template.render(site=self, **context)
|
||||||
|
output_path = self.__output_directory / output
|
||||||
|
|
||||||
with open(output_path, "w") as output_file:
|
with open(output_path, "w") as output_file:
|
||||||
output_file.write(content)
|
output_file.write(content)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ from pathlib import Path
|
||||||
|
|
||||||
from pytest import raises
|
from pytest import raises
|
||||||
|
|
||||||
from jwebsite.content import Content, ContentDirectory
|
from jwebsite.content import ContentDirectory
|
||||||
|
|
||||||
|
|
||||||
def test_load_directory(datadir: Path) -> None:
|
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 page.html == "<p>Content</p>"
|
||||||
assert str(page.meta["title"]) == "Title"
|
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"
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
---
|
|
||||||
title: Titre
|
|
||||||
---
|
|
||||||
|
|
||||||
Contenu
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue