diff options
| author | Paweł Redman <pawel.redman@gmail.com> | 2026-06-14 22:19:49 +0200 |
|---|---|---|
| committer | Paweł Redman <pawel.redman@gmail.com> | 2026-06-14 22:19:49 +0200 |
| commit | 62ef3bb5f7915d2326bcc9f351265107425fc087 (patch) | |
| tree | 1a6640f9584afdf9228b082f00416adf519c6b0e /build.py | |
| parent | c9474445c9bbf22f79778acb292ccd79c7b4d1e5 (diff) | |
Index page in Typst
Diffstat (limited to 'build.py')
| -rw-r--r-- | build.py | 54 |
1 files changed, 43 insertions, 11 deletions
@@ -8,10 +8,9 @@ from dataclasses import dataclass from datetime import datetime from pathlib import Path -import jinja2 from loguru import logger from typst import Compiler, TypstError -from watchfiles import watch +from watchfiles import DefaultFilter, watch @dataclass @@ -22,12 +21,14 @@ class CompiledPage: class Builder: - def __init__(self, site_dir: Path, static_dir: Path, public_dir: Path) -> None: + def __init__(self, site_dir: Path, static_dir: Path, public_dir: Path, temp_dir: Path) -> None: self.site_dir = site_dir self.content_dir = site_dir / 'content' self.static_dir = static_dir self.public_dir = public_dir - self.compiler = Compiler(root=site_dir) + self.temp_dir = temp_dir + + self._compiler = Compiler(root=site_dir) def _compile_typst_page(self, path: Path) -> CompiledPage: relative_path = path.relative_to(self.content_dir) @@ -36,7 +37,7 @@ class Builder: output_path = self.public_dir / relative_path.with_suffix('.html') output_path.parent.mkdir(parents=True, exist_ok=True) - self.compiler.compile( + self._compiler.compile( input=path, output=output_path, sys_inputs={ @@ -45,7 +46,7 @@ class Builder: ) metadata = json.loads( - self.compiler.query( + self._compiler.query( selector='<post-meta>', field='value', one=True, @@ -66,9 +67,37 @@ class Builder: return [self._compile_typst_page(path) for path in self.content_dir.rglob('*.typ')] def _compile_index_page(self, pages: list[CompiledPage]) -> None: - pages.sort(key=lambda page: page.date) - template = jinja2.Template((self.site_dir / 'index.html').read_text()) - (self.public_dir / 'index.html').write_text(template.render(pages=pages)) + # pages.sort(key=lambda page: page.date) + # template = jinja2.Template((self.site_dir / 'index.html').read_text()) + # (self.public_dir / 'index.html').write_text(template.render(pages=pages)) + + payload_path = self.temp_dir / 'payload.json' + payload_path.parent.mkdir(parents=True, exist_ok=True) + + pages.sort(key=lambda page: page.date, reverse=True) + + with payload_path.open('w') as file: + payload = { + 'pages': [ + { + 'url': page.url, + 'title': page.title, + 'year': page.date.year, + 'month': page.date.month, + 'day': page.date.day, + } + for page in pages + ], + } + json.dump(payload, file) + + self._compiler.compile( + input=self.site_dir / 'index.typ', + output=self.public_dir / 'index.html', + sys_inputs={ + 'payload': str(payload_path.relative_to(self.site_dir)), + }, + ) def build(self) -> None: logger.info(f'Removing {str(self.public_dir)!r}...') @@ -93,16 +122,19 @@ def main() -> None: site_dir = repo_dir / 'site' public_dir = repo_dir / 'public' static_dir = repo_dir / 'static' + temp_dir = site_dir / 'temp' os.environ['TYPST_FEATURES'] = 'html' - builder = Builder(site_dir=site_dir, static_dir=static_dir, public_dir=public_dir) + builder = Builder(site_dir, static_dir, public_dir, temp_dir) builder.build() if args.watch: logger.info('Watching for changes...') - for changes in watch(site_dir): + watch_filter = DefaultFilter(ignore_paths=[temp_dir]) + + for changes in watch(site_dir, watch_filter=watch_filter): print(changes) try: builder.build() |
