|
3 | 3 |
|
4 | 4 | import builtins |
5 | 5 | import re |
| 6 | +import textwrap |
6 | 7 | import typing |
| 8 | +from functools import lru_cache |
7 | 9 | from importlib import import_module |
8 | 10 | from typing import ( |
9 | 11 | Any, Callable, Dict, Iterable, Iterator, List, Match, Optional, Set, Tuple, |
@@ -78,30 +80,31 @@ def iter_blocks(lines: Iterable[str]) -> Iterator[Tuple[List[str], int]]: |
78 | 80 | yield current_block, indent_size |
79 | 81 |
|
80 | 82 |
|
81 | | -def wrapped_block(lines: Iterable[str]) -> Iterator[str]: |
82 | | - wrapper = getattr(wrapped_block, 'wrapper', None) |
83 | | - if wrapper is None: |
84 | | - import textwrap |
85 | | - wrapper = textwrap.TextWrapper( |
86 | | - initial_indent='#: ', subsequent_indent='#: ', width=70, break_long_words=False |
| 83 | +@lru_cache(maxsize=8) |
| 84 | +def block_wrapper(comment_symbol: str) -> textwrap.TextWrapper: |
| 85 | + return textwrap.TextWrapper( |
| 86 | + initial_indent=comment_symbol, subsequent_indent=comment_symbol, width=70, break_long_words=False |
87 | 87 | ) |
88 | | - setattr(wrapped_block, 'wrapper', wrapper) |
| 88 | + |
| 89 | + |
| 90 | +def wrapped_block(lines: Iterable[str], comment_symbol: str = '#: ') -> Iterator[str]: |
| 91 | + wrapper = block_wrapper(comment_symbol) |
89 | 92 | for block, indent_size in iter_blocks(lines): |
90 | 93 | if indent_size > 0: |
91 | 94 | for line in block: |
92 | 95 | if not line: |
93 | 96 | yield line |
94 | 97 | else: |
95 | | - yield '#: ' + line |
| 98 | + yield comment_symbol + line |
96 | 99 | else: |
97 | 100 | for line in wrapper.wrap('\n'.join(block)): |
98 | 101 | yield line |
99 | 102 |
|
100 | 103 |
|
101 | | -def render_block(text: str) -> str: |
| 104 | +def render_block(text: str, comment_symbol: str = '#: ') -> str: |
102 | 105 | text = remove_markup(text) |
103 | 106 | lines = text.splitlines() |
104 | | - return '\n'.join(wrapped_block(lines)) |
| 107 | + return '\n'.join(wrapped_block(lines, comment_symbol)) |
105 | 108 |
|
106 | 109 |
|
107 | 110 | class CoalescedIteratorData: |
@@ -288,12 +291,17 @@ def key_text(self) -> str: |
288 | 291 |
|
289 | 292 | def as_conf(self, commented: bool = False, level: int = 0, action_group: List['Mapping'] = []) -> List[str]: |
290 | 293 | ans: List[str] = [] |
| 294 | + if not self.documented: |
| 295 | + return ans |
291 | 296 | a = ans.append |
| 297 | + if self.short_text: |
| 298 | + a(render_block(self.short_text.strip())), a('') |
292 | 299 | for sc in [self] + action_group: |
293 | 300 | if sc.documented and sc.add_to_default: |
294 | 301 | a(sc.setting_name + ' ' + sc.parseable_text) |
295 | | - if self.documented and self.long_text: |
296 | | - a(''), a(render_block(self.long_text.strip())), a('') |
| 302 | + if self.long_text: |
| 303 | + a(''), a(render_block(self.long_text.strip(), '#:: ')) |
| 304 | + a('') |
297 | 305 | return ans |
298 | 306 |
|
299 | 307 | def as_rst( |
|
0 commit comments