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