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