Created
June 17, 2026 07:08
-
-
Save rkrishnasanka/9b2962ab50116af88e19b312b0f0930b to your computer and use it in GitHub Desktop.
Comple Typst Template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| Compile Typst templates in the given folder. | |
| Reads input.json: root-level keys are PDF suffixes; each value is a dict of | |
| --input key=value for typst. Generates output/main-<suffix>.pdf for each entry. | |
| Usage: python compile-typst.py <folder> | |
| Example: python compile-typst.py experience-certificate | |
| """ | |
| import json | |
| import os | |
| import subprocess | |
| import sys | |
| def main(): | |
| if len(sys.argv) < 2: | |
| print("Usage: python compile-typst.py <folder>", file=sys.stderr) | |
| print( | |
| " folder: path to template folder containing main.typ and input.json", | |
| file=sys.stderr, | |
| ) | |
| print( | |
| " input.json: object whose root keys are PDF suffixes, values are dicts of typst inputs", | |
| file=sys.stderr, | |
| ) | |
| sys.exit(1) | |
| folder = os.path.abspath(sys.argv[1]) | |
| main_typ = os.path.join(folder, "main.typ") | |
| input_json = os.path.join(folder, "input.json") | |
| output_dir = os.path.join(folder, "output") | |
| if not os.path.isdir(folder): | |
| print(f"Error: not a directory: {folder}", file=sys.stderr) | |
| sys.exit(1) | |
| if not os.path.isfile(main_typ): | |
| print(f"Error: main.typ not found in {folder}", file=sys.stderr) | |
| sys.exit(1) | |
| if not os.path.isfile(input_json): | |
| print(f"Error: input.json not found in {folder}", file=sys.stderr) | |
| sys.exit(1) | |
| with open(input_json, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| if not isinstance(data, dict): | |
| print( | |
| "Error: input.json root must be a JSON object (dictionary); root keys are PDF suffixes", | |
| file=sys.stderr, | |
| ) | |
| sys.exit(1) | |
| os.makedirs(output_dir, exist_ok=True) | |
| exit_code = 0 | |
| for suffix, inputs in data.items(): | |
| if not isinstance(suffix, str) or not suffix.strip(): | |
| print(f"Warning: skipping invalid suffix key: {suffix!r}", file=sys.stderr) | |
| continue | |
| if not isinstance(inputs, dict): | |
| print( | |
| f"Warning: skipping '{suffix}': value must be a JSON object", | |
| file=sys.stderr, | |
| ) | |
| continue | |
| output_pdf = os.path.join(output_dir, f"main-{suffix}.pdf") | |
| rel_pdf = f"output/main-{suffix}.pdf" | |
| args = ["typst", "compile", "main.typ", rel_pdf] | |
| for key, value in inputs.items(): | |
| if not isinstance(key, str): | |
| continue | |
| str_value = str(value) if value is not None else "" | |
| args.append("--input") | |
| args.append(f"{key}={str_value}") | |
| result = subprocess.run(args, cwd=folder) | |
| if result.returncode != 0: | |
| exit_code = result.returncode | |
| else: | |
| print(f"Generated {rel_pdf}") | |
| sys.exit(exit_code) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment