-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
73 lines (54 loc) · 1.88 KB
/
build.py
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
73
import tomllib
from pathlib import Path
import subprocess
import itertools
import datetime
import zoneinfo
MY_NAME = "jonathan-zhang"
JOBNAME_TEMPLATE = "{name}-{date}-{slug}"
PARENT_DIR = Path(__file__).parent
CONFIG_DIR = PARENT_DIR / "data"
BUILD_DIR = PARENT_DIR / ".build"
BUILD_DIR.mkdir(exist_ok=True)
LATEX_TEMPLATE = r"{}\include{{src/main.tex}}"
LUALATEX_ARGS = [
"lualatex",
"-halt-on-error",
"-file-line-error",
"-interaction=nonstopmode",
f"-output-directory={BUILD_DIR.resolve()}",
]
def date() -> str:
"""Produces a date in the format I want, in my time zone"""
return datetime.datetime.now(tz=zoneinfo.ZoneInfo("America/New_York")).strftime("%Y-%m-%d")
def cat_files(p: Path) -> str:
"""Concatenate all files in a directory"""
assert p.is_dir()
out = []
for child in p.iterdir():
out.append(child.read_text())
return "\n".join(out)
def build_newcommand(k: str, v: object) -> str:
"""Format a key value pair as a Latex newcommand"""
if isinstance(v, list):
v = ", ".join(map(str, v))
return rf"\newcommand{{\{k}}}{{{v}}}"
if __name__ == '__main__':
data = tomllib.loads(cat_files(CONFIG_DIR))
for x in itertools.product(*(x.items() for x in data.values())):
newcommands = []
job_name = []
for group_name, group in x:
# special case for "notanon" to not show it in the slug
if group_name != "notanon":
job_name.append(group_name)
for k, v in group.items():
newcommands.append(build_newcommand(k, v))
name = "anon" if "anon" in job_name else MY_NAME
subprocess.run(
LUALATEX_ARGS
+ [
f"-jobname={JOBNAME_TEMPLATE.format(name=name, date=date(), slug='-'.join(job_name))}",
LATEX_TEMPLATE.format("".join(newcommands)),
]
)