Compare commits

...

2 Commits

7 changed files with 58 additions and 5 deletions

View File

@ -45,6 +45,18 @@ class ContentDirectory(Content):
raise NotImplementedError()
class DataField:
def __init__(self, file: "DataFile", value: Any) -> None:
self.__file = file
self.__value = value
def as_path(self) -> Path:
return self.__file.path.parent / str(self.__value)
def __str__(self) -> str:
return str(self.__value)
class DataFile(Content):
def __init__(self, path: Path) -> None:
super().__init__(path)
@ -52,10 +64,11 @@ class DataFile(Content):
self.__data = load(data_file, Loader)
def __getitem__(self, key: Any) -> Any:
return self.__data.get(key)
return DataField(self, self.__data.get(key))
def __iter__(self) -> Iterator[Any]:
yield from self.__data
for it in self.__data:
yield DataField(self, it)
class MarkdownFile(Content):

View File

@ -1,4 +1,5 @@
from pathlib import Path
from shutil import copy
from typing import Any
from jinja2.environment import Environment
@ -12,6 +13,7 @@ class Site:
self.__root_directory = root_directory
self.__output_directory = root_directory / "build"
self.__environment = Environment(loader=FileSystemLoader(searchpath=root_directory / "src"))
self.__environment.filters.update({"output": self.__output})
self.__content = ContentDirectory(root_directory / "content")
@property
@ -26,3 +28,16 @@ class Site:
with open(output_path, "w") as output_file:
output_file.write(content)
def __output(self, path: str | Path) -> str:
path = Path(path)
if path.is_absolute():
src_path = path
relative_src_path = src_path.relative_to(self.__content.path)
else:
src_path = self.__content.path / path
relative_src_path = path
dst_path = self.__output_directory / relative_src_path
dst_path.parent.mkdir(parents=True, exist_ok=True)
copy(src_path, dst_path, follow_symlinks=True)
return f"/{relative_src_path}"

View File

@ -25,11 +25,18 @@ def test_load_data(datadir: Path) -> None:
content = ContentDirectory(datadir)
steven = content.load(Path("otters/steven.yml"))
assert steven["name"] == "Steven"
assert steven["mood"] == "Angry"
assert str(steven["name"]) == "Steven"
assert str(steven["mood"]) == "Angry"
otter_list = content.load(Path("otters/otter_list.json"))
assert list(otter_list) == ["steven", "peter"]
assert [str(it) for it in otter_list] == ["steven", "peter"]
def test_data_as_path(datadir: Path) -> None:
content = ContentDirectory(datadir)
paths = content.load(Path("otters/paths.yml"))
assert paths["steven"].as_path() == datadir / "otters" / "relative-steven"
def test_load_markdown(datadir: Path) -> None:

View File

@ -0,0 +1,2 @@
steven: ./relative-steven

View File

@ -17,3 +17,16 @@ def test_render_page(datadir: Path, site_dir: FakeFilesystem):
site.render("index.j2", "index.html")
with open(datadir / "build" / "index.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "<p>Peter</p><p>Steven</p>"
def test_output_file(datadir: Path, site_dir: FakeFilesystem):
site = Site(datadir)
build_dir = datadir / "build"
site.render("output-file.j2", "output-file.html")
with open(build_dir / "assets/steven-avatar", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Yipee\n"
with open(build_dir / "output-file.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "/assets/steven-avatar\n"

View File

@ -0,0 +1 @@
Yipee

View File

@ -0,0 +1,2 @@
{{ "assets/steven-avatar" | output }}