refactor: address type issues

This commit is contained in:
Marc 2024-11-15 23:20:46 -05:00
parent 351e73224e
commit 0b2a911405
Signed by: marc
GPG key ID: 048E042F22B5DC79
4 changed files with 17 additions and 9 deletions

View file

@ -13,7 +13,7 @@ def build_site(
*,
client: DocumentClient,
renderer: DocumentRenderer,
destination_path: str | None = None,
destination_path: str | pathlib.Path | None = None,
):
"""
Build HTML documents from data extracted from the given client, using
@ -44,13 +44,15 @@ def build_site(
docs.extend(current_document.children)
continue
current_path = current_document.path or ""
target_path = destination_path.joinpath(
pathlib.Path(current_document.path).stem + ".html"
pathlib.Path(current_path).stem + ".html"
)
with open(target_path, "w") as out:
rendered = renderer.render(
current_metadata["template"],
str(current_metadata["template"]),
{
**current_metadata,
"site": site_metadata,

View file

@ -1,5 +1,6 @@
import pydantic
import typing
import abc
@ -12,7 +13,7 @@ class Document(pydantic.BaseModel, abc.ABC):
@property
@abc.abstractmethod
def metadata(self) -> dict[str, str]:
def metadata(self) -> dict[str, object]:
"""Metadata associated with the document."""
@property
@ -47,4 +48,4 @@ class DocumentRenderer(abc.ABC):
"""Renders documents into HTML."""
@abc.abstractmethod
def render(self, template_path: str, content: dict[str, str]) -> str: ...
def render(self, template_path: str, content: dict[str, typing.Any]) -> str: ...

View file

@ -22,7 +22,7 @@ class NextCloudDocument(Document):
type: str
@functools.cached_property
def _parsed_markdown(self) -> tuple[dict[str, str], str]:
def _parsed_markdown(self) -> tuple[dict[str, object], str]:
try:
return frontmatter.parse(self.raw)
except Exception as e:
@ -30,7 +30,7 @@ class NextCloudDocument(Document):
return tuple()
@property
def metadata(self) -> dict[str, str]:
def metadata(self) -> dict[str, object]:
return self._parsed_markdown[0]
@property
@ -68,7 +68,10 @@ class NextCloudClient(DocumentClient, arbitrary_types_allowed=True):
root_document = NextCloudDocument(path=root_path, type="collection")
for r in root_properties:
doc_path = r.find("{DAV:}href").text
doc_path = r.find("{DAV:}href")
if doc_path is not None:
doc_path = doc_path.text
if doc_path == root_document.path:
continue

View file

@ -2,6 +2,8 @@ from .models import DocumentRenderer
import jinja2
import typing
class JinjaDocumentRenderer(DocumentRenderer):
"""Renders documents using Jinja templates."""
@ -12,7 +14,7 @@ class JinjaDocumentRenderer(DocumentRenderer):
loader=jinja2.FileSystemLoader(searchpath="./templates")
)
def render(self, template_path: str, content: dict[str, str]) -> str:
def render(self, template_path: str, content: dict[str, typing.Any]) -> str:
t = self._environment.get_template(template_path)
return t.render(**content)