]> git.lizzy.rs Git - rust.git/blob - util/export.py
Merge pull request #2977 from flip1995/tool_lints
[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['group'] = lint.group
22     lint_dict['level'] = lint.level
23     lint_dict['docs'] = {}
24
25     last_section = None
26
27     for line in lint.doc:
28         if len(line.strip()) == 0 and not last_section.startswith("Example"):
29             continue
30
31         match = re.match(lint_subheadline, line)
32         if match:
33             last_section = match.groups()[0]
34         if match:
35             text = match.groups()[1]
36         else:
37             text = line
38
39         if not last_section:
40             log.warn("Skipping comment line as it was not preceded by a heading")
41             log.debug("in lint `%s`, line `%s`", lint.name, line)
42
43         fragment = lint_dict['docs'].get(last_section, "")
44         if text == "\n":
45             line = fragment + text
46         else:
47             line = (fragment + "\n" + text).strip()
48
49         lint_dict['docs'][last_section] = line
50
51     return lint_dict
52
53
54 def main():
55     lintlist, configs = parse_all()
56     lints = {}
57     for lint in lintlist:
58         lints[lint.name] = parse_lint_def(lint)
59         if lint.name in configs:
60             lints[lint.name]['docs']['Configuration'] = \
61                 CONF_TEMPLATE % configs[lint.name]
62
63     outfile = sys.argv[1] if len(sys.argv) > 1 else "util/gh-pages/lints.json"
64     with open(outfile, "w") as fp:
65         json.dump(list(lints.values()), fp, indent=2)
66         log.info("wrote JSON for great justice")
67
68
69 if __name__ == "__main__":
70     main()