]> git.lizzy.rs Git - rust.git/blob - util/export.py
Merge pull request #3285 from devonhollowood/pedantic-dogfood-items-after-statements
[rust.git] / util / export.py
1 #!/usr/bin/env python
2
3 # Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
4 # file at the top-level directory of this distribution and at
5 # http://rust-lang.org/COPYRIGHT.
6 #
7 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 # option. This file may not be copied, modified, or distributed
11 # except according to those terms.
12
13
14 # Build the gh-pages
15
16 import re
17 import sys
18 import json
19
20 from lintlib import parse_all, log
21
22 lint_subheadline = re.compile(r'''^\*\*([\w\s]+?)[:?.!]?\*\*(.*)''')
23
24 CONF_TEMPLATE = """\
25 This lint has the following configuration variables:
26
27 * `%s: %s`: %s (defaults to `%s`)."""
28
29
30 def parse_lint_def(lint):
31     lint_dict = {}
32     lint_dict['id'] = lint.name
33     lint_dict['group'] = lint.group
34     lint_dict['level'] = lint.level
35     lint_dict['docs'] = {}
36
37     last_section = None
38
39     for line in lint.doc:
40         if len(line.strip()) == 0 and not last_section.startswith("Example"):
41             continue
42
43         match = re.match(lint_subheadline, line)
44         if match:
45             last_section = match.groups()[0]
46         if match:
47             text = match.groups()[1]
48         else:
49             text = line
50
51         if not last_section:
52             log.warn("Skipping comment line as it was not preceded by a heading")
53             log.debug("in lint `%s`, line `%s`", lint.name, line)
54
55         fragment = lint_dict['docs'].get(last_section, "")
56         if text == "\n":
57             line = fragment + text
58         else:
59             line = (fragment + "\n" + text).strip()
60
61         lint_dict['docs'][last_section] = line
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         json.dump(list(lints.values()), fp, indent=2)
78         log.info("wrote JSON for great justice")
79
80
81 if __name__ == "__main__":
82     main()