| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from docutils import nodes |
| 14 |
from docutils.parsers import rst |
| 15 |
from docutils.statemachine import ViewList |
| 16 |
from sphinx.util import logging |
| 17 |
from sphinx.util.nodes import nested_parse_with_titles |
| 18 |
|
| 19 |
from reno import config |
| 20 |
|
| 21 |
LOG = logging.getLogger(__name__) |
| 22 |
|
| 23 |
|
| 24 |
def _multi_line_string(s, indent=''): |
| 25 |
output_lines = s.splitlines() |
| 26 |
if not output_lines[0].strip(): |
| 27 |
output_lines = output_lines[1:] |
| 28 |
for l in output_lines: |
| 29 |
yield indent + l |
| 30 |
|
| 31 |
|
| 32 |
def _format_option_help(options): |
| 33 |
"Produce RST lines for the configuration options." |
| 34 |
for opt in sorted(options, key=lambda opt: opt.name): |
| 35 |
yield '``{}``'.format(opt.name) |
| 36 |
for l in _multi_line_string(opt.help, ' '): |
| 37 |
yield l |
| 38 |
yield '' |
| 39 |
if isinstance(opt.default, str) and '\n' in opt.default: |
| 40 |
|
| 41 |
yield ' Defaults to' |
| 42 |
yield '' |
| 43 |
yield ' ::' |
| 44 |
yield '' |
| 45 |
for l in _multi_line_string(opt.default, ' '): |
| 46 |
yield l |
| 47 |
else: |
| 48 |
yield ' Defaults to ``{!r}``'.format(opt.default) |
| 49 |
yield '' |
| 50 |
|
| 51 |
|
| 52 |
class ShowConfigDirective(rst.Directive): |
| 53 |
|
| 54 |
option_spec = {} |
| 55 |
|
| 56 |
has_content = True |
| 57 |
|
| 58 |
def run(self): |
| 59 |
result = ViewList() |
| 60 |
source_name = '<' + __name__ + '>' |
| 61 |
for line in _format_option_help(config._OPTIONS): |
| 62 |
LOG.info(line) |
| 63 |
result.append(line, source_name) |
| 64 |
|
| 65 |
node = nodes.section() |
| 66 |
node.document = self.state.document |
| 67 |
nested_parse_with_titles(self.state, result, node) |
| 68 |
|
| 69 |
return node.children |
| 70 |
|
| 71 |
|
| 72 |
def setup(app): |
| 73 |
app.add_directive('show-reno-config', ShowConfigDirective) |