From cb77d1fe956d6011a5739bce5eddf6f6daf80661 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Thu, 11 Apr 2024 18:25:22 +0200 Subject: docs: Introduce old base scripts for handling doc generation --- docs/scripts/make_rst.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 docs/scripts/make_rst.py (limited to 'docs/scripts/make_rst.py') diff --git a/docs/scripts/make_rst.py b/docs/scripts/make_rst.py new file mode 100644 index 0000000..78126b4 --- /dev/null +++ b/docs/scripts/make_rst.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +import argparse +import json +import jinja2 +from pathlib import Path + + +def parse_args(): + parser = argparse.ArgumentParser( + description="Generates source files based on a jinja2 template " + "and a json variable map") + parser.add_argument( + '-t', '--template', required=True, + help='path to the jinja2 template dir') + parser.add_argument( + '-m', '--map', required=True, + help='path to the json variable map file') + parser.add_argument( + '-o', '--output', required=True, + help='path to the output dir') + parser.add_argument( + '--title', default="C++ API Reference", + help='The title of the index for the API' + ); + + return parser.parse_args() + + +def read_template(path): + with open(path, "r") as f: + return jinja2.Template(f.read(), keep_trailing_newline=True) + + +def read_var_map(path): + with open(path, "r") as f: + return json.loads(f.read()); + #endwith + + +def main(): + args = parse_args() + + template_dir = Path(args.template); + template = read_template(template_dir/"class.rst.tmpl"); + var_map = read_var_map(args.map); + + var_map['title'] = args.title; + + out_dir = Path(args.output); + for k,v in var_map["classes"].items(): + out_name = k+".rst"; + out_file = out_dir / out_name; + v["specializations"] = sorted(v["specializations"], key=lambda k: k["name"]); + with open(out_file, "w") as f: + f.write(template.render(v)); + #endwith + #endfor + + out_globs = out_dir / "globals.rst"; + template_globs = read_template(template_dir/"globals.rst.tmpl"); + with open(out_globs, "w") as f: + f.write(template_globs.render(var_map)); + #endwith + + out_index = out_dir / "index.rst"; + template_index = read_template(template_dir/"index.rst.tmpl"); + var_map["classes"] = dict(sorted(var_map["classes"].items(), key=lambda k: k[1]["name"])); + with open(out_index, "w") as f: + f.write(template_index.render(var_map)); + pass + +if __name__ == "__main__": + main(); -- cgit v1.2.3