51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from common import read_json, write_text
|
|
|
|
|
|
def render_sections(sections: list[dict[str, Any]]) -> list[str]:
|
|
lines: list[str] = []
|
|
for section in sections:
|
|
heading = section.get("heading")
|
|
if heading:
|
|
lines.extend([f"## {heading}", ""])
|
|
for paragraph in section.get("paragraphs", []):
|
|
lines.append(paragraph)
|
|
if section.get("paragraphs"):
|
|
lines.append("")
|
|
bullets = section.get("bullets", [])
|
|
if bullets:
|
|
lines.extend([f"- {item}" for item in bullets])
|
|
lines.append("")
|
|
return lines
|
|
|
|
|
|
def build_markdown(spec: dict[str, Any]) -> str:
|
|
if "markdown" in spec:
|
|
return str(spec["markdown"])
|
|
|
|
lines = [f"# {spec.get('title', '报告')}", ""]
|
|
summary = spec.get("summary")
|
|
if summary:
|
|
lines.extend([summary, ""])
|
|
lines.extend(render_sections(spec.get("sections", [])))
|
|
return "\n".join(lines).rstrip() + "\n"
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--spec", required=True)
|
|
parser.add_argument("--out", required=True)
|
|
args = parser.parse_args()
|
|
|
|
spec = read_json(Path(args.spec).resolve())
|
|
write_text(Path(args.out).resolve(), build_markdown(spec))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|