1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#!/usr/bin/env python3
import json
import os
import re
import shutil
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
import jinja2
import typst
@dataclass
class Metadata:
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] = {}
for path in content_dir.rglob('*.typ'):
print(path)
output_path = public_dir / path.relative_to(content_dir).with_suffix('.html')
output_path.parent.mkdir(parents=True, exist_ok=True)
metadata = json.loads(typst.query(
root=site_dir,
input=path,
selector='<post-meta>',
field='value',
one=True,
))
output_url = str(output_path.relative_to(public_dir))
pages[output_url] = Metadata(
title=metadata['title'],
date=datetime(
year=metadata['date_year'],
month=metadata['date_month'],
day=metadata['date_day'],
),
)
typst.compile(
root=site_dir,
input=path,
output=output_path
)
print(pages)
template = jinja2.Template(open(site_dir / 'index.html').read())
open(public_dir / 'index.html', 'w').write(template.render(pages=pages))
if __name__ == '__main__':
main()
|