refactor: do not allow relative content loading
It messes up with the localization system, and brings to much trouble for a feature that will probably encourage bad or convoluted designs
This commit is contained in:
parent
7a86eb432c
commit
b028477371
|
|
@ -1,5 +1,5 @@
|
|||
from pathlib import Path
|
||||
from typing import Any, Generic, Iterator, TypeVar
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
TData = TypeVar("TData")
|
||||
|
||||
|
|
@ -26,24 +26,3 @@ class Content(Generic[TData]):
|
|||
if isinstance(self.__data, bytes):
|
||||
return self.__data.decode("utf-8")
|
||||
return str(self.__data)
|
||||
|
||||
|
||||
class ContentField:
|
||||
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.__owner_content_path, self.__value.get(key))
|
||||
|
||||
def __iter__(self) -> Iterator[Any]:
|
||||
for it in self.__value:
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -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, ContentField
|
||||
from jweb.content import Content
|
||||
|
||||
_DEFAULT_LANGUAGE = "en"
|
||||
|
||||
|
|
@ -70,10 +70,7 @@ class Context:
|
|||
self.__render(source, self.__output_directory / output, **context)
|
||||
|
||||
@pass_context
|
||||
def __load(self, context: JinjaContext, path: str | Path | ContentField) -> Content[bytes]:
|
||||
if isinstance(path, ContentField):
|
||||
path = path.as_path()
|
||||
|
||||
def __load(self, context: JinjaContext, path: str | Path) -> Content[bytes]:
|
||||
current_language = self.current_language
|
||||
if current_language:
|
||||
localized_path = self.__content_directory / current_language / path
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from typing import Any
|
|||
|
||||
from markdown import Markdown
|
||||
|
||||
from jweb.content import Content, ContentField
|
||||
from jweb.content import Content
|
||||
from jweb.context import Context
|
||||
|
||||
|
||||
|
|
@ -27,4 +27,4 @@ class _MarkdownDocument(Content[str]):
|
|||
|
||||
@property
|
||||
def meta(self) -> Any:
|
||||
return ContentField(self.__content.path, self.__markdown.Meta) # type: ignore
|
||||
return self.__markdown.Meta # type: ignore
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from typing import Any
|
|||
|
||||
from yaml import Loader, load
|
||||
|
||||
from jweb.content import Content, ContentField
|
||||
from jweb.content import Content
|
||||
from jweb.context import Context
|
||||
|
||||
|
||||
|
|
@ -10,7 +10,7 @@ def load_extension(context: Context) -> None:
|
|||
context.add_filters(yaml=_load_yaml)
|
||||
|
||||
|
||||
def _load_yaml(content: Any) -> ContentField:
|
||||
def _load_yaml(content: Any) -> Any:
|
||||
if not isinstance(content, Content) or not isinstance(content.data, (str, bytes)):
|
||||
raise ValueError("yaml filter can only accept byte or string content")
|
||||
|
||||
|
|
@ -20,4 +20,4 @@ def _load_yaml(content: Any) -> ContentField:
|
|||
|
||||
assert isinstance(data, str)
|
||||
|
||||
return ContentField(content.path, load(data, Loader))
|
||||
return load(data, Loader)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
from pathlib import Path
|
||||
|
||||
from pytest import mark, raises
|
||||
from pytest import mark
|
||||
|
||||
from jweb.content import ContentField
|
||||
from jweb.context import Context
|
||||
|
||||
|
||||
|
|
@ -34,19 +33,6 @@ 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")
|
||||
|
|
|
|||
Loading…
Reference in New Issue