summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.py77
-rw-r--r--site/index.html4
2 files changed, 50 insertions, 31 deletions
diff --git a/build.py b/build.py
index 5d4a5b3..3838d57 100644
--- a/build.py
+++ b/build.py
@@ -9,45 +9,43 @@ from datetime import datetime
from pathlib import Path
import jinja2
-import typst
+from typst import Compiler
@dataclass
-class Metadata:
+class CompiledPage:
+ url: str
title: str
date: datetime
-def main() -> None:
- repo_dir = Path(__file__).parent
- site_dir = repo_dir / 'site'
- content_dir = site_dir / 'content'
- public_dir = repo_dir / 'public'
- shutil.rmtree(public_dir, ignore_errors=True)
- shutil.copytree(repo_dir / 'static', public_dir / 'static')
- os.environ['TYPST_FEATURES'] = 'html'
-
- pages: dict[Path, Metadata] = {}
+class Builder:
+ def __init__(self, site_dir: Path, static_dir: Path, public_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)
- for path in content_dir.rglob('*.typ'):
- print(path)
+ def _compile_typst_page(self, path: Path) -> CompiledPage:
+ relative_path = path.relative_to(self.content_dir)
+ print(relative_path)
- output_path = public_dir / path.relative_to(content_dir).with_suffix('.html')
+ output_path = self.public_dir / relative_path.with_suffix('.html')
output_path.parent.mkdir(parents=True, exist_ok=True)
- metadata = json.loads(typst.query(
- root=site_dir,
- input=path,
+ self.compiler.compile(input=path, output=output_path)
+
+ metadata = json.loads(self.compiler.query(
selector='<post-meta>',
field='value',
one=True,
))
- output_url = str(output_path.relative_to(public_dir))
-
- pages[output_url] = Metadata(
+ return CompiledPage(
+ url=str(relative_path.with_suffix('.html')),
title=metadata['title'],
date=datetime(
year=metadata['date_year'],
@@ -56,16 +54,37 @@ def main() -> None:
),
)
- typst.compile(
- root=site_dir,
- input=path,
- output=output_path
- )
+ def _compile_typst_pages(self) -> list[CompiledPage]:
+ 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))
- print(pages)
- template = jinja2.Template(open(site_dir / 'index.html').read())
- open(public_dir / 'index.html', 'w').write(template.render(pages=pages))
+ def build(self) -> None:
+ pages = self._compile_typst_pages()
+ self._compile_index_page(pages)
+
+
+def main() -> None:
+ repo_dir = Path(__file__).parent
+ site_dir = repo_dir / 'site'
+ content_dir = site_dir / 'content'
+ public_dir = repo_dir / 'public'
+ static_dir = repo_dir / 'static'
+
+ shutil.rmtree(public_dir, ignore_errors=True)
+ shutil.copytree(repo_dir / 'static', public_dir / 'static')
+
+ os.environ['TYPST_FEATURES'] = 'html'
+
+ builder = Builder(site_dir=site_dir, static_dir=static_dir, public_dir=public_dir)
+ builder.build()
if __name__ == '__main__':
diff --git a/site/index.html b/site/index.html
index 317d439..7cd6639 100644
--- a/site/index.html
+++ b/site/index.html
@@ -2,9 +2,9 @@
<body>
<h1>Blog</h1>
<ul>
- {% for url, metadata in pages.items() %}
+ {% for page in pages %}
<li>
- <a href="{{ url }}">{{ metadata.title }}</a>
+ <a href="{{ page.url }}">{{ page.title }}</a>
</li>
{% endfor %}
</ul>