summaryrefslogtreecommitdiff
path: root/docs/scripts/make_rst.py
blob: 78126b4949ddddb8b14ac1832772be6e74fd1174 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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();