diff --git a/jweb/content.py b/jweb/content.py index 5b3a18d..5ce4402 100644 --- a/jweb/content.py +++ b/jweb/content.py @@ -27,16 +27,21 @@ class Content(Generic[TData]): class ContentField: - def __init__(self, content_path: Path, value: Any) -> None: - self.__content_path = content_path + def __init__(self, owner_content_path: Path, value: Any) -> None: + self.__owner_content_path = owner_content_path self.__value = value def __getitem__(self, key: Any) -> Any: - return ContentField(self.__content_path, self.__value.get(key)) + return ContentField(self.__owner_content_path, self.__value.get(key)) def __iter__(self) -> Iterator[Any]: for it in self.__value: - yield ContentField(self.__content_path, it) + yield ContentField(self.__owner_content_path, it) def __str__(self) -> str: return str(self.__value) + + def as_path(self) -> Path: + if not isinstance(self.__value, (str, Path)): + raise ValueError("You can't use a field of type %s as a path", type(self.__value)) + return self.__owner_content_path.parent / str(self) diff --git a/jweb/context.py b/jweb/context.py index 6c8fe6a..9a46374 100644 --- a/jweb/context.py +++ b/jweb/context.py @@ -10,7 +10,7 @@ from jinja2.environment import Environment from jinja2.loaders import FileSystemLoader from jinja2.runtime import Context as JinjaContext -from jweb.content import Content +from jweb.content import Content, ContentField _DEFAULT_LANGUAGE = "en" @@ -70,7 +70,10 @@ class Context: self.__render(source, self.__output_directory / output, **context) @pass_context - def __load(self, context: JinjaContext, path: str | Path) -> Content[str]: + def __load(self, context: JinjaContext, path: str | Path | ContentField) -> Content[str]: + if isinstance(path, ContentField): + path = path.as_path() + current_language = self.current_language if current_language: localized_path = self.__content_directory / current_language / path diff --git a/tests/test_context.py b/tests/test_context.py index 917feb9..80964c7 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -1,7 +1,8 @@ from pathlib import Path -from pytest import mark +from pytest import mark, raises +from jweb.content import ContentField from jweb.context import Context @@ -33,6 +34,19 @@ def test_load(datadir: Path, path: str | Path) -> None: assert ouput_file.read() == "Otters\n" +def test_load_content_field(datadir: Path) -> None: + context = Context(datadir) + context.render( + "test-load.html", "output.html", path=ContentField(Path("subdir/dummy.yml"), "content.txt") + ) + + with open(datadir / "build" / "output.html", encoding="utf-8") as ouput_file: + assert ouput_file.read() == "Weasel\n" + + with raises(ValueError): + context.render("test-load.html", "output.html", path=ContentField(Path("subdir"), 42)) + + def test_load_localized(datadir: Path) -> None: context = Context(datadir) context.load_translations("tests", datadir / "locale", "fr") diff --git a/tests/test_context/locale/fr/LC_MESSAGES/tests.po b/tests/test_context/locale/fr/LC_MESSAGES/tests.po index bbb7231..48f331b 100644 --- a/tests/test_context/locale/fr/LC_MESSAGES/tests.po +++ b/tests/test_context/locale/fr/LC_MESSAGES/tests.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2024-05-21 23:52+0200\n" +"POT-Creation-Date: 2024-05-22 00:10+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language: fr\n" diff --git a/tests/test_context/src/test-load.html b/tests/test_context/src/test-load.html index 961788a..68fee27 100644 --- a/tests/test_context/src/test-load.html +++ b/tests/test_context/src/test-load.html @@ -1 +1 @@ -{{ 'content.txt' | load }} +{{ path | load }}