diff --git a/scripts/generate-template.py b/scripts/generate-template.py new file mode 100644 index 0000000..5b4f247 --- /dev/null +++ b/scripts/generate-template.py @@ -0,0 +1,136 @@ +#!/usr/bin/env python3 +"""Generate a quality-verified HTML artifact from a pre-built template YAML.""" + +from __future__ import annotations +import argparse, json, subprocess, sys, tempfile, re +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +TEMPLATES_DIR = ROOT / "templates" / "prebuilt" + +def validate(data: dict, schema: dict) -> list[str]: + errors = [] + for key, spec in schema.items(): + if spec.get("required") and key not in data: + errors.append(f"Missing required field: {key}") + if key in data: + if spec["type"] == "array" and not isinstance(data[key], list): + errors.append(f"{key} must be an array") + if spec["type"] == "integer" and not isinstance(data[key], int): + errors.append(f"{key} must be an integer") + return errors + +def generate(artifact_path: Path, template_data: dict) -> str: + """Generate HTML from template data. Returns artifact path.""" + title = template_data.get("title", "Generated Artifact") + options = template_data.get("options", []) + recommendation = template_data.get("recommendation", "") + next_action = template_data.get("next_action", "") + + rows = "" + for opt in options: + risk_list = "" + evidence_list = "" + rows += f""" + {opt.get("name", "")} + {opt.get("score", 0)} + {risk_list} + {evidence_list} + """ + + html = f""" + + + + +{title} + + + +
+
+

{title}

+
+ + +{rows} +
OptionScoreRisksEvidence
+
+Recommendation: {recommendation} +
+
+Next action: {next_action} +
+
+""" + + out = artifact_path or Path(tempfile.mktemp(suffix=".html", prefix="template-")) + out.write_text(html) + return out + +def main() -> int: + parser = argparse.ArgumentParser(description="Generate artifact from pre-built template YAML") + parser.add_argument("action", choices=["generate", "validate", "list-templates"]) + parser.add_argument("template", nargs="?", help="Template name (e.g. decision-deck)") + parser.add_argument("data", nargs="?", help="Path to data YAML file") + args = parser.parse_args() + + if args.action == "list-templates": + for f in sorted(TEMPLATES_DIR.glob("*.yaml")): + data = json.loads(f.read_text()) + print(f" {data[\"name\"]}: {data[\"description\"]}") + return 0 + + if args.action == "validate": + if not args.template or not args.data: + print("Usage: generate-template.py validate