Compare commits

..

3 Commits

4 changed files with 42 additions and 11 deletions

View File

@ -1,6 +1,6 @@
from functools import cache
from pathlib import Path
from typing import Any, Iterator
from typing import Any, Iterable, Iterator
from markdown import Markdown
from yaml import Loader, load
@ -27,6 +27,10 @@ class ContentDirectory(Content):
return current
def glob(self, pattern: str) -> Iterable[Content]:
for item in self.path.glob(pattern):
yield self.load(str(item))
@cache # noqa: B019
def __load_children(self, name: str) -> Content:
child_path = self.path / name
@ -46,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):
@ -71,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):
@ -91,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

26
jwebsite/git.py Normal file
View File

@ -0,0 +1,26 @@
from datetime import datetime
from subprocess import check_output
from jwebsite.content import Content
def git_creation_date(content: Content) -> datetime:
git_dir = check_output(["git", "rev-parse", "--show-toplevel"], encoding="utf-8")
git_dir = git_dir.strip()
log = check_output(
[
"git",
"log",
"--pretty=format:%ad",
"--date=iso-strict",
"--diff-filter=A",
"--",
str(content.path.relative_to(git_dir)),
],
encoding="utf-8",
)
log_lines = log.splitlines()
if len(log_lines) == 0:
return datetime.now()
return datetime.fromisoformat(log_lines[0])

View File

@ -6,6 +6,7 @@ from jinja2.environment import Environment
from jinja2.loaders import FileSystemLoader
from jwebsite.content import ContentDirectory
from jwebsite.git import git_creation_date
class Site:
@ -13,7 +14,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.__environment.filters.update({"output": self.__output, "git_creation_date": git_creation_date})
self.__content = ContentDirectory(root_directory / "content")
@property

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"