feat: return Markdown metadata items as DataItem

This commit is contained in:
Corentin 2024-05-21 00:54:01 +02:00
parent 051a51280f
commit 8a614fee99
2 changed files with 9 additions and 9 deletions

View File

@ -50,22 +50,22 @@ class ContentDirectory(Content):
class DataField:
def __init__(self, file: "DataFile", value: Any) -> None:
self.__file = file
def __init__(self, file_path: Path, value: Any) -> None:
self.__file_path = file_path
self.__value = value
def as_path(self) -> Path:
return self.__file.path.parent / str(self.__value)
return self.__file_path.parent / str(self.__value)
def __str__(self) -> str:
return str(self.__value)
def __getitem__(self, key: Any) -> Any:
return DataField(self.__file, self.__value.get(key))
return DataField(self.__file_path, self.__value.get(key))
def __iter__(self) -> Iterator[Any]:
for it in self.__value:
yield DataField(self.__file, it)
yield DataField(self.__file_path, it)
class DataFile(Content):
@ -75,11 +75,11 @@ class DataFile(Content):
self.__data = load(data_file, Loader)
def __getitem__(self, key: Any) -> Any:
return DataField(self, self.__data.get(key))
return DataField(self.path, self.__data.get(key))
def __iter__(self) -> Iterator[Any]:
for it in self.__data:
yield DataField(self, it)
yield DataField(self.path, it)
class MarkdownFile(Content):
@ -95,4 +95,4 @@ class MarkdownFile(Content):
@property
def meta(self) -> Any:
return self.__markdown.Meta # type: ignore
return DataField(self.path, self.__markdown.Meta) # type: ignore

View File

@ -44,4 +44,4 @@ def test_load_markdown(datadir: Path) -> None:
page = content.load(Path("page.md"))
assert page.html == "<p>Content</p>"
assert page.meta["title"] == "Title"
assert str(page.meta["title"]) == "Title"