Allow manual builds #20
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,6 +9,7 @@ __pycache__
|
||||
/output
|
||||
/output.zip
|
||||
*.pdf
|
||||
/manual
|
||||
|
||||
# TeX build files
|
||||
*.synctex*
|
||||
|
@ -5,6 +5,7 @@ import tomllib
|
||||
import shutil
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
# TODO:
|
||||
# list handouts without solutions
|
||||
@ -16,10 +17,22 @@ ROOT: Path = Path(os.getcwd())
|
||||
|
||||
### CONFIGURATION
|
||||
OUT_DIR: Path = ROOT / "output"
|
||||
OUT_DIR_MANUAL: Path = ROOT / "manual"
|
||||
TYPST_PATH: str = "typst"
|
||||
XETEX_PATH: str = "xelatex"
|
||||
### END CONFIGURATION
|
||||
|
||||
|
||||
# If we're given an argument, build one handout.
|
||||
# This places output into `OUT_DIR_MANUAL`
|
||||
#
|
||||
# If we're given no arguments, build everything,
|
||||
# Placing output into `OUT_DIR`
|
||||
target = None
|
||||
if len(sys.argv) == 2:
|
||||
target = Path(sys.argv[1])
|
||||
print(f"Compiling `{target}`")
|
||||
|
||||
# Allow path override
|
||||
_env = os.environ.get("TYPST_PATH")
|
||||
if isinstance(_env, str):
|
||||
@ -35,13 +48,6 @@ if not ROOT.is_dir():
|
||||
log("Root is not a directory, cannot continue")
|
||||
exit(1)
|
||||
|
||||
log(f"Output dir is {OUT_DIR}")
|
||||
if OUT_DIR.exists():
|
||||
log("Output dir exists, removing")
|
||||
shutil.rmtree(OUT_DIR)
|
||||
|
||||
OUT_DIR.mkdir(parents=True)
|
||||
|
||||
|
||||
IndexEntry = TypedDict(
|
||||
"IndexEntry",
|
||||
@ -115,7 +121,9 @@ def log_error(res):
|
||||
exit(1)
|
||||
|
||||
|
||||
def build_typst(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
def build_typst(
|
||||
source_dir: Path, out_subdir: Path, *, out_dir=OUT_DIR
|
||||
) -> IndexEntry | None:
|
||||
if not (source_dir / "main.typ").is_file():
|
||||
# log(f"No main.typ, skipping {source_dir}")
|
||||
return None
|
||||
@ -136,7 +144,7 @@ def build_typst(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
# Build handout
|
||||
log(f"Building typst (handout) : {source_dir}")
|
||||
|
||||
out = OUT_DIR / out_subdir
|
||||
out = out_dir / out_subdir
|
||||
out.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
res = subprocess.run(
|
||||
@ -190,7 +198,9 @@ def build_typst(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
}
|
||||
|
||||
|
||||
def build_xetex(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
def build_xetex(
|
||||
source_dir: Path, out_subdir: Path, *, out_dir=OUT_DIR
|
||||
) -> IndexEntry | None:
|
||||
if not (source_dir / "main.tex").is_file():
|
||||
# log(f"No main.tex, skipping {source_dir}")
|
||||
return None
|
||||
@ -211,7 +221,7 @@ def build_xetex(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
# Build handout
|
||||
log(f"Building xetex (handout) : {source_dir}")
|
||||
|
||||
out = OUT_DIR / out_subdir
|
||||
out = out_dir / out_subdir
|
||||
out.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
res = subprocess.run(
|
||||
@ -297,10 +307,31 @@ def build_dir(base: str, out_sub: str, index: list[IndexEntry]):
|
||||
break
|
||||
|
||||
|
||||
index: list[IndexEntry] = []
|
||||
build_dir("src/Advanced", "Advanced", index)
|
||||
build_dir("src/Intermediate", "Intermediate", index)
|
||||
build_dir("src/Warm-Ups", "Warm-Ups", index)
|
||||
if target is None:
|
||||
log(f"Output dir is {OUT_DIR}")
|
||||
if OUT_DIR.exists():
|
||||
log("Output dir exists, removing")
|
||||
shutil.rmtree(OUT_DIR)
|
||||
|
||||
with open(OUT_DIR / "index.json", "w") as f:
|
||||
f.write(json.dumps(index))
|
||||
OUT_DIR.mkdir(parents=True)
|
||||
|
||||
index: list[IndexEntry] = []
|
||||
build_dir("src/Advanced", "Advanced", index)
|
||||
build_dir("src/Intermediate", "Intermediate", index)
|
||||
build_dir("src/Warm-Ups", "Warm-Ups", index)
|
||||
|
||||
with open(OUT_DIR / "index.json", "w") as f:
|
||||
f.write(json.dumps(index))
|
||||
|
||||
else:
|
||||
log(f"Output dir is {OUT_DIR_MANUAL}")
|
||||
# if OUT_DIR_MANUAL.exists():
|
||||
# log("Output dir exists, removing")
|
||||
# shutil.rmtree(OUT_DIR_MANUAL)
|
||||
OUT_DIR_MANUAL.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
builders = [build_typst, build_xetex]
|
||||
for builder in builders:
|
||||
res = builder(target, Path("."), out_dir=OUT_DIR_MANUAL)
|
||||
if res is not None:
|
||||
break
|
||||
|
Loading…
x
Reference in New Issue
Block a user