]> git.lizzy.rs Git - rust.git/blob - util/export.py
Merge pull request #2428 from phansch/fix_lint_list_issues
[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 and not last_section.startswith("Example"):
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         fragment = lint_dict['docs'].get(last_section, "")
43         if text == "\n":
44             line = fragment + text
45         else:
46             line = (fragment + "\n" + text).strip()
47
48         lint_dict['docs'][last_section] = line
49
50     return lint_dict
51
52
53 def main():
54     lintlist, configs = parse_all()
55     lints = {}
56     for lint in lintlist:
57         lints[lint.name] = parse_lint_def(lint)
58         if lint.name in configs:
59             lints[lint.name]['docs']['Configuration'] = \
60                 CONF_TEMPLATE % configs[lint.name]
61
62     outfile = sys.argv[1] if len(sys.argv) > 1 else "util/gh-pages/lints.json"
63     with open(outfile, "w") as fp:
64         json.dump(list(lints.values()), fp, indent=2)
65         log.info("wrote JSON for great justice")
66
67
68 if __name__ == "__main__":
69     main()