add templates

This commit is contained in:
Daniel Fichtinger 2025-06-21 17:28:00 -04:00
parent 6c13f4bfa8
commit 296b3d2385
6 changed files with 34 additions and 8 deletions

View file

@ -1,5 +1,6 @@
from zona.models import Item, Metadata, ItemType
from zona import markdown as zmd
from zona import templates as tmpl
from zona import util
from pathlib import Path
import frontmatter
@ -77,7 +78,8 @@ def discover(root: Path, out_dir: Path) -> list[Item]:
return items
def build(items: list[Item]):
def build(root: Path, items: list[Item]):
env = tmpl.init_templates(root / "templates")
for item in items:
dst = item.destination
# create parent dirs if needed
@ -85,9 +87,10 @@ def build(items: list[Item]):
assert item.content is not None
# parse markdown and render as html
raw_html = zmd.md_to_html(item.content)
# TODO: apply to template here
# TODO: test this
rendered = tmpl.render_item(item, raw_html, env)
util.ensure_parents(dst)
dst.write_text(raw_html, encoding="utf-8")
dst.write_text(rendered, encoding="utf-8")
else:
if item.copy:
util.copy_static_file(item.source, dst)

View file

@ -1,15 +1,18 @@
import typer
from pathlib import Path
from zona import builder
app = typer.Typer()
@app.command()
def discover(in_dir: str | None = None):
out_dir = ""
def build(in_dir: str | None = None, out_dir: str | None = None):
if in_dir is None:
in_dir = "."
_ = builder.discover(in_dir, out_dir)
if out_dir is None:
out_dir = "public"
items = builder.discover(Path(in_dir), Path(out_dir))
builder.build(Path(in_dir), items)
@app.command()

View file

View file

View file

@ -2,7 +2,6 @@ from pathlib import Path
from enum import Enum
from datetime import date
from dataclasses import dataclass
from typing import Literal
@dataclass
@ -10,7 +9,9 @@ class Metadata:
title: str
date: date
description: str | None
# add more options later...
style: str | None = "static/style.css"
header: bool = True
footer: bool = True
class ItemType(Enum):

19
src/zona/templates.py Normal file
View file

@ -0,0 +1,19 @@
from pathlib import Path
from jinja2 import Environment, FileSystemLoader, select_autoescape
from zona.models import Item
def init_templates(template_dir: Path) -> Environment:
return Environment(
loader=FileSystemLoader(template_dir),
autoescape=select_autoescape(["html", "xml"]),
)
def render_item(item: Item, content: str, env: Environment) -> str:
template = env.get_template("post.html")
meta = item.metadata
assert meta is not None
return template.render(
title=meta.title, content=content, url=item.url, metadata=meta
)