]> git.lizzy.rs Git - rust.git/blob - util/export.py
ae8e4a72c08d8827efeed86d9f27844fbfc719f5
[rust.git] / util / export.py
1 #!/usr/bin/env python
2 # Build the gh-pages
3
4 import re
5 import sys
6 import json
7
8 from lintlib import parse_all, log
9
10 lint_subheadline = re.compile(r'''^\*\*([\w\s]+?)[:?.!]?\*\*(.*)''')
11
12 CONF_TEMPLATE = """\
13 This lint has the following configuration variables:
14
15 * `%s: %s`: %s (defaults to `%s`)."""
16
17
18 def parse_lint_def(lint):
19     lint_dict = {}
20     lint_dict['id'] = lint.name
21     lint_dict['level'] = lint.level
22     lint_dict['docs'] = {}
23
24     last_section = None
25
26     for line in lint.doc:
27         if len(line.strip()) == 0:
28             continue
29
30         match = re.match(lint_subheadline, line)
31         if match:
32             last_section = match.groups()[0]
33         if match:
34             text = match.groups()[1]
35         else:
36             text = line
37
38         if not last_section:
39             log.warn("Skipping comment line as it was not preceded by a heading")
40             log.debug("in lint `%s`, line `%s`", lint.name, line)
41
42         lint_dict['docs'][last_section] = \
43             (lint_dict['docs'].get(last_section, "") + "\n" + text).strip()
44
45     return lint_dict
46
47
48 def main():
49     lintlist, configs = parse_all()
50     lints = {}
51     for lint in lintlist:
52         lints[lint.name] = parse_lint_def(lint)
53         if lint.name in configs:
54             lints[lint.name]['docs']['Configuration'] = \
55                 CONF_TEMPLATE % configs[lint.name]
56
57     outfile = sys.argv[1] if len(sys.argv) > 1 else "util/gh-pages/lints.json"
58     with open(outfile, "w") as fp:
59         json.dump(list(lints.values()), fp, indent=2)
60         log.info("wrote JSON for great justice")
61
62
63 if __name__ == "__main__":
64     main()