-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor/cli template dir #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
649214a
1b7ba28
3c30872
097bca2
d099454
248357f
ad7158e
07eb447
c58d328
a9a1951
d4a8f94
350a9ae
b117af6
85345f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # PHU Templates Directory | ||
|
|
||
| This directory contains Public Health Unit (PHU) specific template customizations. | ||
|
|
||
| ## Usage | ||
|
|
||
| Each PHU should create a subdirectory here with their organization-specific templates: | ||
|
|
||
| ``` | ||
| phu_templates/ | ||
| ├── my_phu/ | ||
| │ ├── en_template.py (required for English output) | ||
| │ ├── fr_template.py (required for French output) | ||
| │ ├── conf.typ (required) | ||
| │ └── assets/ (optional - only if templates reference assets) | ||
| │ ├── logo.png (optional) | ||
| │ └── signature.png (optional) | ||
| ``` | ||
|
|
||
| ## Running with PHU Templates | ||
|
|
||
| To use a PHU-specific template, specify the template name with `--template`: | ||
|
|
||
| ```bash | ||
| # Generate English notices | ||
| uv run viper students.xlsx en --template my_phu | ||
|
|
||
| # Generate French notices | ||
| uv run viper students.xlsx fr --template my_phu | ||
| ``` | ||
|
|
||
| This will load templates from `phu_templates/my_phu/`. | ||
|
|
||
| ## Template File Requirements | ||
|
|
||
| ### Core Requirements (Always Required) | ||
|
|
||
| - `conf.typ` - Typst configuration and utility functions | ||
|
|
||
| ### Language-Specific Requirements (Based on Output Language) | ||
|
|
||
| - `en_template.py` - Required only if generating English notices (`--language en`) | ||
| - Must define `render_notice()` function | ||
| - Consulted only when `--language en` is specified | ||
|
|
||
| - `fr_template.py` - Required only if generating French notices (`--language fr`) | ||
| - Must define `render_notice()` function | ||
| - Consulted only when `--language fr` is specified | ||
|
|
||
| **Note:** A PHU may provide templates for only one language. If a user requests a language your template does not support, the pipeline will fail with a clear error message. If you only support one language, only include that template file (e.g., only `en_template.py`). | ||
|
|
||
| ### Asset Requirements (Based on Template Implementation) | ||
|
|
||
| Assets in the `assets/` directory are **optional** and depend entirely on your template implementation: | ||
|
|
||
| - `assets/logo.png` - Only required if your `en_template.py` or `fr_template.py` references a logo | ||
| - `assets/signature.png` - Only required if your `en_template.py` or `fr_template.py` references a signature | ||
| - Other files - Any additional assets (e.g., `assets/header.png`, `assets/seal.pdf`) may be included and referenced in your templates | ||
|
|
||
| **Note:** If your template references an asset (e.g., `include "assets/logo.png"` in Typst), that asset **must** exist. The pipeline will fail with a clear error if a referenced asset is missing. | ||
|
|
||
| ## Creating a PHU Template | ||
|
|
||
| If your PHU supports both English and French: | ||
|
|
||
| ```bash | ||
| cp -r templates/ phu_templates/my_phu/ | ||
| ``` | ||
|
|
||
| Then customize: | ||
| - Replace `assets/logo.png` with your PHU logo | ||
| - Replace `assets/signature.png` with your signature | ||
| - Modify `en_template.py` and `fr_template.py` as needed | ||
| - Adjust `conf.typ` for organization-specific styling | ||
|
|
||
| ### Testing Your Template | ||
|
|
||
| ```bash | ||
| # Test English generation | ||
| uv run viper students.xlsx en --template my_phu | ||
|
|
||
| # Test French generation (if you provided fr_template.py) | ||
| uv run viper students.xlsx fr --template my_phu | ||
| ``` | ||
|
|
||
| If a language template is missing: | ||
| ``` | ||
| FileNotFoundError: Template module not found: /path/to/phu_templates/my_phu/fr_template.py | ||
| Expected fr_template.py in /path/to/phu_templates/my_phu | ||
| ``` | ||
|
|
||
| If an asset referenced by your template is missing: | ||
| ``` | ||
| FileNotFoundError: Logo not found: /path/to/phu_templates/my_phu/assets/logo.png | ||
| ``` | ||
|
|
||
| ## Git Considerations | ||
|
|
||
| **Important:** PHU-specific templates are excluded from version control via `.gitignore`. | ||
|
|
||
| - Templates in this directory will NOT be committed to the main repository | ||
| - Each PHU should maintain their templates in their own fork or separate repository | ||
| - The `README.md` file and `.gitkeep` are the only tracked files in this directory |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -88,6 +88,7 @@ def compile_file( | |||||||
| Optional path to directory containing custom fonts. | ||||||||
| root_dir : Path | ||||||||
| Root directory for relative path resolution in Typst compilation. | ||||||||
| This should be the template directory containing conf.typ and assets/. | ||||||||
| verbose : bool | ||||||||
| If True, print compilation status message. | ||||||||
| """ | ||||||||
|
|
@@ -124,6 +125,7 @@ def compile_typst_files( | |||||||
| Optional custom fonts directory. | ||||||||
| root_dir : Path | ||||||||
| Root directory for relative path resolution. | ||||||||
| Should be the template directory containing conf.typ and assets/. | ||||||||
| verbose : bool | ||||||||
| If True, print per-file compilation status. | ||||||||
|
|
||||||||
|
|
@@ -154,17 +156,24 @@ def compile_with_config( | |||||||
| artifact_dir: Path, | ||||||||
| output_dir: Path, | ||||||||
| config_path: Path | None = None, | ||||||||
| template_dir: Path | None = None, | ||||||||
| ) -> int: | ||||||||
| """Compile Typst files using configuration from parameters.yaml. | ||||||||
|
|
||||||||
| Reads typst configuration (binary path, font path) from parameters.yaml | ||||||||
| and compiles all Typst files in the artifact directory. | ||||||||
|
|
||||||||
| Parameters | ||||||||
| ---------- | ||||||||
| artifact_dir : Path | ||||||||
| Directory containing Typst artifacts (.typ files). | ||||||||
| Directory containing Typst template files | ||||||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was previous documentation was better? Here
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like use of word template better. Maybe a hybrid? "Directory containing Typst template files (.typ)." |
||||||||
| output_dir : Path | ||||||||
| Directory where compiled PDFs will be written. | ||||||||
| Directory where compiled PDFs will be written | ||||||||
| config_path : Path, optional | ||||||||
| Path to parameters.yaml. If not provided, uses default location. | ||||||||
| template_dir : Path, optional | ||||||||
| Template directory for dynamic template loading. Typst compilation always | ||||||||
| uses PROJECT_ROOT as --root to find both templates and output artifacts. | ||||||||
|
|
||||||||
| Returns | ||||||||
| ------- | ||||||||
|
|
@@ -182,17 +191,26 @@ def compile_with_config( | |||||||
|
|
||||||||
| font_path = Path(font_path_str) if font_path_str else None | ||||||||
|
|
||||||||
| # Typst compilation always uses PROJECT_ROOT to find both templates and output artifacts. | ||||||||
| # template_dir parameter is for dynamic template loading in generate_notices, not for Typst --root. | ||||||||
| root_dir = ROOT_DIR | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this necessary if a constant? |
||||||||
|
|
||||||||
| return compile_typst_files( | ||||||||
| artifact_dir, | ||||||||
| output_dir, | ||||||||
| typst_bin=typst_bin, | ||||||||
| font_path=font_path, | ||||||||
| root_dir=ROOT_DIR, | ||||||||
| root_dir=root_dir, | ||||||||
| verbose=False, | ||||||||
| ) | ||||||||
|
|
||||||||
|
|
||||||||
| def main(artifact_dir: Path, output_dir: Path, config_path: Path | None = None) -> int: | ||||||||
| def main( | ||||||||
| artifact_dir: Path, | ||||||||
| output_dir: Path, | ||||||||
| config_path: Path | None = None, | ||||||||
| template_dir: Path | None = None, | ||||||||
| ) -> int: | ||||||||
| """Main entry point for Typst compilation. | ||||||||
|
|
||||||||
| Parameters | ||||||||
|
|
@@ -203,13 +221,16 @@ def main(artifact_dir: Path, output_dir: Path, config_path: Path | None = None) | |||||||
| Directory for output PDFs. | ||||||||
| config_path : Path, optional | ||||||||
| Path to parameters.yaml configuration file. | ||||||||
| template_dir : Path, optional | ||||||||
| Template directory containing conf.typ and assets/. Used as Typst --root. | ||||||||
| If not provided, defaults to project root. | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
| Returns | ||||||||
| ------- | ||||||||
| int | ||||||||
| Number of files compiled. | ||||||||
| """ | ||||||||
| compiled = compile_with_config(artifact_dir, output_dir, config_path) | ||||||||
| compiled = compile_with_config(artifact_dir, output_dir, config_path, template_dir) | ||||||||
| if compiled: | ||||||||
| print(f"Compiled {compiled} Typst file(s) to PDFs in {output_dir}.") | ||||||||
| return compiled | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really thinking that we may need to clarify template name to the phu acronym!