]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/util/export.py
New upstream version 1.52.1+dfsg1
[rustc.git] / src / tools / clippy / 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 # fix syntax highlighting for headers like ```rust,ignore
26 if line.startswith('```rust'):
27 lines.append('```rust')
28 elif not line.startswith('# '):
29 lines.append(line)
30
31 return '\n'.join(lines)
32
33
34 def parse_lint_def(lint):
35 lint_dict = {}
36 lint_dict['id'] = lint.name
37 lint_dict['group'] = lint.group
38 lint_dict['level'] = lint.level
39 lint_dict['docs'] = OrderedDict()
40
41 last_section = None
42
43 for line in lint.doc:
44 match = re.match(lint_subheadline, line)
45 if match:
46 last_section = match.groups()[0]
47 text = match.groups()[1]
48 else:
49 text = line
50
51 if not last_section:
52 log.warning("Skipping comment line as it was not preceded by a heading")
53 log.debug("in lint `%s`, line `%s`", lint.name, line)
54
55 if last_section not in lint_dict['docs']:
56 lint_dict['docs'][last_section] = ""
57
58 lint_dict['docs'][last_section] += text + "\n"
59
60 for section in lint_dict['docs']:
61 lint_dict['docs'][section] = re.sub(rust_code_block, parse_code_block, lint_dict['docs'][section].strip())
62
63 return lint_dict
64
65
66 def main():
67 lintlist, configs = parse_all()
68 lints = {}
69 for lint in lintlist:
70 lints[lint.name] = parse_lint_def(lint)
71 if lint.name in configs:
72 lints[lint.name]['docs']['Configuration'] = \
73 CONF_TEMPLATE % configs[lint.name]
74
75 outfile = sys.argv[1] if len(sys.argv) > 1 else "util/gh-pages/lints.json"
76 with open(outfile, "w") as fp:
77 lints = list(lints.values())
78 lints.sort(key=lambda x: x['id'])
79 json.dump(lints, fp, indent=2)
80 log.info("wrote JSON for great justice")
81
82
83 if __name__ == "__main__":
84 main()