Compare commits
2 Commits
1a32cb2b42
...
562ecf366c
| Author | SHA1 | Date |
|---|---|---|
|
|
562ecf366c | |
|
|
a23da83687 |
|
|
@ -45,6 +45,18 @@ class ContentDirectory(Content):
|
||||||
raise NotImplementedError()
|
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):
|
class DataFile(Content):
|
||||||
def __init__(self, path: Path) -> None:
|
def __init__(self, path: Path) -> None:
|
||||||
super().__init__(path)
|
super().__init__(path)
|
||||||
|
|
@ -52,10 +64,11 @@ class DataFile(Content):
|
||||||
self.__data = load(data_file, Loader)
|
self.__data = load(data_file, Loader)
|
||||||
|
|
||||||
def __getitem__(self, key: Any) -> Any:
|
def __getitem__(self, key: Any) -> Any:
|
||||||
return self.__data.get(key)
|
return DataField(self, self.__data.get(key))
|
||||||
|
|
||||||
def __iter__(self) -> Iterator[Any]:
|
def __iter__(self) -> Iterator[Any]:
|
||||||
yield from self.__data
|
for it in self.__data:
|
||||||
|
yield DataField(self, it)
|
||||||
|
|
||||||
|
|
||||||
class MarkdownFile(Content):
|
class MarkdownFile(Content):
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from shutil import copy
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from jinja2.environment import Environment
|
from jinja2.environment import Environment
|
||||||
|
|
@ -12,6 +13,7 @@ class Site:
|
||||||
self.__root_directory = root_directory
|
self.__root_directory = root_directory
|
||||||
self.__output_directory = root_directory / "build"
|
self.__output_directory = root_directory / "build"
|
||||||
self.__environment = Environment(loader=FileSystemLoader(searchpath=root_directory / "src"))
|
self.__environment = Environment(loader=FileSystemLoader(searchpath=root_directory / "src"))
|
||||||
|
self.__environment.filters.update({"output": self.__output})
|
||||||
self.__content = ContentDirectory(root_directory / "content")
|
self.__content = ContentDirectory(root_directory / "content")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -26,3 +28,16 @@ class Site:
|
||||||
|
|
||||||
with open(output_path, "w") as output_file:
|
with open(output_path, "w") as output_file:
|
||||||
output_file.write(content)
|
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}"
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,18 @@ def test_load_data(datadir: Path) -> None:
|
||||||
content = ContentDirectory(datadir)
|
content = ContentDirectory(datadir)
|
||||||
steven = content.load(Path("otters/steven.yml"))
|
steven = content.load(Path("otters/steven.yml"))
|
||||||
|
|
||||||
assert steven["name"] == "Steven"
|
assert str(steven["name"]) == "Steven"
|
||||||
assert steven["mood"] == "Angry"
|
assert str(steven["mood"]) == "Angry"
|
||||||
|
|
||||||
otter_list = content.load(Path("otters/otter_list.json"))
|
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:
|
def test_load_markdown(datadir: Path) -> None:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
steven: ./relative-steven
|
||||||
|
|
||||||
|
|
@ -17,3 +17,16 @@ def test_render_page(datadir: Path, site_dir: FakeFilesystem):
|
||||||
site.render("index.j2", "index.html")
|
site.render("index.j2", "index.html")
|
||||||
with open(datadir / "build" / "index.html", encoding="utf-8") as ouput_file:
|
with open(datadir / "build" / "index.html", encoding="utf-8") as ouput_file:
|
||||||
assert ouput_file.read() == "<p>Peter</p><p>Steven</p>"
|
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"
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
Yipee
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
{{ "assets/steven-avatar" | output }}
|
||||||
|
|
||||||
Loading…
Reference in New Issue