Merge branch 'generator-restructure'

This commit is contained in:
akp 2025-03-18 23:52:10 +00:00
commit 3f32b4d69e
3 changed files with 20 additions and 79 deletions

View file

@ -15,14 +15,6 @@ def make_absurl_filter(site_conf: any) -> Callable[[str], str]:
class CLI:
@staticmethod
def g(*args, **kwargs):
return CLI.generate(*args, **kwargs)
@staticmethod
def w(*args, **kwargs):
return CLI.watch(*args, **kwargs)
@staticmethod
def generate(
base_dir: str = "site", output_dir: str = "_dist", production: bool = False
@ -48,12 +40,7 @@ class CLI:
"date": git.get_latest_commit_datetime(base_dir),
}
jinja_env = Environment(
loader=FileSystemLoader("site/templates"), autoescape=select_autoescape()
)
jinja_env.filters["absurl"] = make_absurl_filter(site_config)
jinja_env.filters["fmtdate"] = lambda x: x.strftime("%Y-%m-%d")
jinja_env = build_jinja_env(site_config)
rprint(INFO_LEADER + "Processing content")
process.content(base_dir, html_dir, jinja_env, site_config)
@ -70,13 +57,7 @@ class CLI:
rprint(INFO_LEADER + "Stripping EXIF data")
process.strip_exif(output_dir)
thing_overrides = {"rendered": "page", "ran": "command"}
res_parts = []
for key, count in get_counts().items():
s = "" if count == 1 else "s"
res_parts.append(f"{key} {count} {thing_overrides.get(key, 'file')}{s}")
rprint(INFO_LEADER + "Finished working, " + ", ".join(res_parts))
rprint(INFO_LEADER + "Finished working")
@staticmethod
def watch(base_dir: str = "site", output_dir: str = "_dist"):
@ -90,7 +71,6 @@ class CLI:
def run():
if os.path.exists(output_dir):
shutil.rmtree(output_dir)
reset_counts() # boys and girls, this is why global state is bad
CLI.generate(base_dir, output_dir=output_dir)
debounced_run = debounce(1)(run)

View file

@ -5,7 +5,7 @@ import re
from pathlib import Path
from rich import print as rprint
import shutil
from jinja2 import Environment
import jinja2
import feeds
import markdown
from typedef import *
@ -13,15 +13,7 @@ from util import *
import itertools
import subprocess
import datetime
def _template_frontmatter(data: any, jinja_env: Environment, context: any):
for key in data:
v = data[key]
if (t := type(v)) == str:
data[key] = jinja_env.from_string(v).render(context)
elif t == dict:
_template_frontmatter(data[key], jinja_env, context)
import os
class _FileType(enum.IntEnum):
@ -69,7 +61,7 @@ def _make_canonical_url(site_config: any, path: Path | str) -> str:
)
def content(base_dir: Path, output_dir: Path, jinja_env: Environment, site_config: any):
def content(base_dir: Path, output_dir: Path, jinja_env: jinja2.Environment, site_config: any):
markdown_to_html = markdown.create(escape=False)
walk_dir = base_dir / "content"
@ -108,14 +100,7 @@ def content(base_dir: Path, output_dir: Path, jinja_env: Environment, site_confi
)
os.makedirs(target_path.parent, exist_ok=True)
# rprint(
# INFO_LEADER
# + f"Rendering [bold]{fpath.relative_to(base_dir)}[/bold]"
# f"[white] => {target_path}[/white]"
# )
ctx = {"site": site_config}
_template_frontmatter(tpl_frontmatter, jinja_env, ctx)
if "canonicalURL" not in tpl_frontmatter and not tpl_frontmatter.get(
"preserveCanonicalURL", False
@ -165,8 +150,6 @@ def content(base_dir: Path, output_dir: Path, jinja_env: Environment, site_confi
with open(target_path, "w") as f:
f.write(res)
update_counts("rendered", 1)
else:
if (
fpath.suffix.lower() != ".scss"
@ -179,7 +162,6 @@ def content(base_dir: Path, output_dir: Path, jinja_env: Environment, site_confi
target_path = output_dir / site_inner_path
os.makedirs(target_path.parent, exist_ok=True)
shutil.copy(fpath, target_path)
update_counts("copied", 1)
def sass(base_dir: Path, output_dir: Path):
@ -192,14 +174,13 @@ def sass(base_dir: Path, output_dir: Path):
+ str(output_dir / "html/assets/css"),
]
).check_returncode()
update_counts("ran", 1)
BLOG_DATE_FORMAT = "%Y-%m-%d"
BLOG_TAG_RE = re.compile(r"[a-zA-Z\d-]+")
def blog(base_dir: Path, output_dir: Path, jinja_env: Environment, site_config: any):
def blog(base_dir: Path, output_dir: Path, jinja_env: jinja2.Environment, site_config: any):
markdown_to_html = markdown.create(escape=False, header_level_delta=1)
walk_dir = base_dir / "blog"
@ -254,12 +235,6 @@ def blog(base_dir: Path, output_dir: Path, jinja_env: Environment, site_config:
target_path = output_dir / "blog" / post_slug / "index.html"
os.makedirs(target_path.parent, exist_ok=True)
# rprint(
# INFO_LEADER
# + f"Rendering [bold]{fpath.relative_to(base_dir)}[/bold]"
# f"[white] => {target_path}[/white]"
# )
if "updatedDate" in post_frontmatter:
post_frontmatter["updatedDate"] = list(
sorted(post_frontmatter["updatedDate"], reverse=True)
@ -293,7 +268,6 @@ def blog(base_dir: Path, output_dir: Path, jinja_env: Environment, site_config:
with open(target_path, "w") as f:
f.write(res)
update_counts("rendered", 1)
case _:
if filetype != _FileType.STATIC:
rprint(
@ -303,7 +277,6 @@ def blog(base_dir: Path, output_dir: Path, jinja_env: Environment, site_config:
target_path = output_dir / "blog" / inner_path
os.makedirs(target_path.parent, exist_ok=True)
shutil.copy(fpath, target_path)
update_counts("copied", 1)
# generate listing
post_list = []
@ -365,7 +338,6 @@ def blog(base_dir: Path, output_dir: Path, jinja_env: Environment, site_config:
)
f.write(r)
update_counts("generated", 1)
# generate tag list
tags_output_dir = output_dir / "blog" / "tags"
@ -385,7 +357,6 @@ def blog(base_dir: Path, output_dir: Path, jinja_env: Environment, site_config:
}
)
f.write(r)
update_counts("generated", 1)
# generate tag-specific index pages
tpl = jinja_env.get_template("_layouts/blog/postsFilteredByTag.html")
@ -409,7 +380,6 @@ def blog(base_dir: Path, output_dir: Path, jinja_env: Environment, site_config:
}
)
)
update_counts("generated", 1)
# generate feeds
with open(output_dir / "blog" / "feed.atom", "w") as f:
@ -418,11 +388,9 @@ def blog(base_dir: Path, output_dir: Path, jinja_env: Environment, site_config:
with open(output_dir / "blog" / "feed.json", "w") as f:
f.write(feeds.json(site_config, post_list))
update_counts("generated", 2)
def caddy_config(
base_dir: Path, output_dir: Path, jinja_env: Environment, site_config: any
base_dir: Path, output_dir: Path, jinja_env: jinja2.Environment, site_config: any
):
try:
x = open(base_dir / "redirects.yml").read()
@ -539,8 +507,6 @@ def caddy_config(
with open(output_dir / "caddy_config.json", "w") as f:
json.dump(conf, f, indent="\t")
update_counts("generated", 1)
def compress_png(output_dir: Path):
pngcrush_exe: str | None = shutil.which("pngcrush")
@ -557,7 +523,6 @@ def compress_png(output_dir: Path):
):
proc: subprocess.CompletedProcess = subprocess.run([pngcrush_exe, "-ow", image])
proc.check_returncode()
update_counts("compressed", 1)
def strip_exif(output_dir: Path):
@ -582,4 +547,3 @@ def strip_exif(output_dir: Path):
[mogrify_exe, "-strip", image]
)
proc.check_returncode()
update_counts("sanitised", 1)

View file

@ -3,6 +3,8 @@ import os
from pathlib import Path
import threading
import yaml
import jinja2
import functools
ERROR_LEADER = "[[red bold]FAIL[/bold red]] "
WARN_LEADER = "[[yellow bold]WARN[/bold yellow]] "
@ -17,22 +19,6 @@ def is_directory_empty(path: str | Path) -> bool:
return len(os.listdir(path)) == 0
_counts: dict[str, int] = {}
def reset_counts():
global _counts
_counts = {}
def update_counts(key: str, delta: int):
_counts[key] = _counts.get(key, 0) + delta
def get_counts() -> dict[str, int]:
return _counts
def extract_frontmatter(instr: str) -> tuple[any, str]:
first = instr.find("---")
second = instr.find("---", first + 1)
@ -61,6 +47,17 @@ def date_to_datetime(d: datetime.date) -> datetime.datetime:
return datetime.datetime(d.year, d.month, d.day)
def build_jinja_env(site_config: any) -> jinja2.Environment:
jinja_env = jinja2.Environment(
loader=jinja2.FileSystemLoader("site/templates"), autoescape=jinja2.select_autoescape(),
)
jinja_env.filters["absurl"] = functools.partial(get_absolute_url, site_config)
jinja_env.filters["fmtdate"] = lambda x: x.strftime("%Y-%m-%d")
return jinja_env
def debounce(wait_time):
"""
https://stackoverflow.com/a/66907107