1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5#      http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13from docutils import nodes
14from docutils.parsers import rst
15from docutils.statemachine import ViewList
16
17from sphinx.util.nodes import nested_parse_with_titles
18
19from reno import config
20import six
21
22
23def _multi_line_string(s, indent=''):
24    output_lines = s.splitlines()
25    if not output_lines[0].strip():
26        output_lines = output_lines[1:]
27    for l in output_lines:
28        yield indent + l
29
30
31def _format_option_help(options):
32    "Produce RST lines for the configuration options."
33    for opt in options:
34        yield '``{}``'.format(opt.name)
35        for l in _multi_line_string(opt.help, '  '):
36            yield l
37        yield ''
38        if isinstance(opt.default, six.string_types) and '\n' in opt.default:
39            # Multi-line string
40            yield '  Defaults to'
41            yield ''
42            yield '  ::'
43            yield ''
44            for l in _multi_line_string(opt.default, '    '):
45                yield l
46        else:
47            yield '  Defaults to ``{!r}``'.format(opt.default)
48        yield ''
49
50
51class ShowConfigDirective(rst.Directive):
52
53    option_spec = {}
54
55    has_content = True
56
57    def run(self):
58        env = self.state.document.settings.env
59        app = env.app
60
61        result = ViewList()
62        source_name = '<' + __name__ + '>'
63        for line in _format_option_help(config._OPTIONS):
64            app.info(line)
65            result.append(line, source_name)
66
67        node = nodes.section()
68        node.document = self.state.document
69        nested_parse_with_titles(self.state, result, node)
70
71        return node.children
72
73
74def setup(app):
75    app.add_directive('show-reno-config', ShowConfigDirective)
76