Compare commits

..

No commits in common. "b2748a6cf3bee48cd2b426c3b407b6e28b9a46b7" and "f691669d76690f84753f2d1fdbf88a5d51e783cb" have entirely different histories.

4 changed files with 11 additions and 42 deletions

View File

@ -1,6 +1,6 @@
from functools import cache from functools import cache
from pathlib import Path from pathlib import Path
from typing import Any, Iterable, Iterator from typing import Any, Iterator
from markdown import Markdown from markdown import Markdown
from yaml import Loader, load from yaml import Loader, load
@ -27,10 +27,6 @@ class ContentDirectory(Content):
return current return current
def glob(self, pattern: str) -> Iterable[Content]:
for item in self.path.glob(pattern):
yield self.load(str(item))
@cache # noqa: B019 @cache # noqa: B019
def __load_children(self, name: str) -> Content: def __load_children(self, name: str) -> Content:
child_path = self.path / name child_path = self.path / name
@ -50,22 +46,22 @@ class ContentDirectory(Content):
class DataField: class DataField:
def __init__(self, file_path: Path, value: Any) -> None: def __init__(self, file: "DataFile", value: Any) -> None:
self.__file_path = file_path self.__file = file
self.__value = value self.__value = value
def as_path(self) -> Path: 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: def __str__(self) -> str:
return str(self.__value) return str(self.__value)
def __getitem__(self, key: Any) -> Any: def __getitem__(self, key: Any) -> Any:
return DataField(self.__file_path, self.__value.get(key)) return DataField(self.__file, self.__value.get(key))
def __iter__(self) -> Iterator[Any]: def __iter__(self) -> Iterator[Any]:
for it in self.__value: for it in self.__value:
yield DataField(self.__file_path, it) yield DataField(self.__file, it)
class DataFile(Content): class DataFile(Content):
@ -75,11 +71,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 DataField(self.path, self.__data.get(key)) return DataField(self, self.__data.get(key))
def __iter__(self) -> Iterator[Any]: def __iter__(self) -> Iterator[Any]:
for it in self.__data: for it in self.__data:
yield DataField(self.path, it) yield DataField(self, it)
class MarkdownFile(Content): class MarkdownFile(Content):
@ -95,4 +91,4 @@ class MarkdownFile(Content):
@property @property
def meta(self) -> Any: def meta(self) -> Any:
return DataField(self.path, self.__markdown.Meta) # type: ignore return self.__markdown.Meta # type: ignore

View File

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

View File

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