feat: allow to load content from relative path nested in other content

Allow to declare assets paths relative to the current yaml or Markdown file
This commit is contained in:
Corentin 2024-05-22 00:11:47 +02:00
parent cf5cfbd09f
commit f7bd1fd62f
5 changed files with 31 additions and 9 deletions

View File

@ -27,16 +27,21 @@ class Content(Generic[TData]):
class ContentField: class ContentField:
def __init__(self, content_path: Path, value: Any) -> None: def __init__(self, owner_content_path: Path, value: Any) -> None:
self.__content_path = content_path self.__owner_content_path = owner_content_path
self.__value = value self.__value = value
def __getitem__(self, key: Any) -> Any: 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]: def __iter__(self) -> Iterator[Any]:
for it in self.__value: for it in self.__value:
yield ContentField(self.__content_path, it) yield ContentField(self.__owner_content_path, it)
def __str__(self) -> str: def __str__(self) -> str:
return str(self.__value) 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)

View File

@ -10,7 +10,7 @@ from jinja2.environment import Environment
from jinja2.loaders import FileSystemLoader from jinja2.loaders import FileSystemLoader
from jinja2.runtime import Context as JinjaContext from jinja2.runtime import Context as JinjaContext
from jweb.content import Content from jweb.content import Content, ContentField
_DEFAULT_LANGUAGE = "en" _DEFAULT_LANGUAGE = "en"
@ -70,7 +70,10 @@ class Context:
self.__render(source, self.__output_directory / output, **context) self.__render(source, self.__output_directory / output, **context)
@pass_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 current_language = self.current_language
if current_language: if current_language:
localized_path = self.__content_directory / current_language / path localized_path = self.__content_directory / current_language / path

View File

@ -1,7 +1,8 @@
from pathlib import Path from pathlib import Path
from pytest import mark from pytest import mark, raises
from jweb.content import ContentField
from jweb.context import Context from jweb.context import Context
@ -33,6 +34,19 @@ def test_load(datadir: Path, path: str | Path) -> None:
assert ouput_file.read() == "Otters\n" 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: def test_load_localized(datadir: Path) -> None:
context = Context(datadir) context = Context(datadir)
context.load_translations("tests", datadir / "locale", "fr") context.load_translations("tests", datadir / "locale", "fr")

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PROJECT VERSION\n" "Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\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" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: fr\n" "Language: fr\n"

View File

@ -1 +1 @@
{{ 'content.txt' | load }} {{ path | load }}