ansible-doc: fix indent and line wrapping for first line of (sub-)option and (sub-)return value descriptions (#84690)

* Fix initial indent for descriptions of suboptions.
* Fix line width for initial line of option descriptions.

(cherry picked from commit 352d8ec33a)
pull/85043/head
Felix Fontein 8 months ago committed by Matt Martz
parent 6f95a618af
commit 2fcfad54b0
No known key found for this signature in database
GPG Key ID: 40832D88E9FC91D8

@ -0,0 +1,3 @@
bugfixes:
- "ansible-doc - fix indentation for first line of descriptions of suboptions and sub-return values (https://github.com/ansible/ansible/pull/84690)."
- "ansible-doc - fix line wrapping for first line of description of options and return values (https://github.com/ansible/ansible/pull/84690)."

@ -1172,12 +1172,16 @@ class DocCLI(CLI, RoleMixin):
return 'version %s' % (version_added, ) return 'version %s' % (version_added, )
@staticmethod @staticmethod
def warp_fill(text, limit, initial_indent='', subsequent_indent='', **kwargs): def warp_fill(text, limit, initial_indent='', subsequent_indent='', initial_extra=0, **kwargs):
result = [] result = []
for paragraph in text.split('\n\n'): for paragraph in text.split('\n\n'):
result.append(textwrap.fill(paragraph, limit, initial_indent=initial_indent, subsequent_indent=subsequent_indent, wrapped = textwrap.fill(paragraph, limit, initial_indent=initial_indent + ' ' * initial_extra, subsequent_indent=subsequent_indent,
break_on_hyphens=False, break_long_words=False, drop_whitespace=True, **kwargs)) break_on_hyphens=False, break_long_words=False, drop_whitespace=True, **kwargs)
if initial_extra and wrapped.startswith(' ' * initial_extra):
wrapped = wrapped[initial_extra:]
result.append(wrapped)
initial_indent = subsequent_indent initial_indent = subsequent_indent
initial_extra = 0
return '\n'.join(result) return '\n'.join(result)
@staticmethod @staticmethod
@ -1209,20 +1213,23 @@ class DocCLI(CLI, RoleMixin):
text.append('') text.append('')
# TODO: push this to top of for and sort by size, create indent on largest key? # TODO: push this to top of for and sort by size, create indent on largest key?
inline_indent = base_indent + ' ' * max((len(opt_indent) - len(o)) - len(base_indent), 2) inline_indent = ' ' * max((len(opt_indent) - len(o)) - len(base_indent), 2)
sub_indent = inline_indent + ' ' * (len(o) + 3) extra_indent = base_indent + ' ' * (len(o) + 3)
sub_indent = inline_indent + extra_indent
if is_sequence(opt['description']): if is_sequence(opt['description']):
for entry_idx, entry in enumerate(opt['description'], 1): for entry_idx, entry in enumerate(opt['description'], 1):
if not isinstance(entry, string_types): if not isinstance(entry, string_types):
raise AnsibleError("Expected string in description of %s at index %s, got %s" % (o, entry_idx, type(entry))) raise AnsibleError("Expected string in description of %s at index %s, got %s" % (o, entry_idx, type(entry)))
if entry_idx == 1: if entry_idx == 1:
text.append(key + DocCLI.warp_fill(DocCLI.tty_ify(entry), limit, initial_indent=inline_indent, subsequent_indent=sub_indent)) text.append(key + DocCLI.warp_fill(DocCLI.tty_ify(entry), limit,
initial_indent=inline_indent, subsequent_indent=sub_indent, initial_extra=len(extra_indent)))
else: else:
text.append(DocCLI.warp_fill(DocCLI.tty_ify(entry), limit, initial_indent=sub_indent, subsequent_indent=sub_indent)) text.append(DocCLI.warp_fill(DocCLI.tty_ify(entry), limit, initial_indent=sub_indent, subsequent_indent=sub_indent))
else: else:
if not isinstance(opt['description'], string_types): if not isinstance(opt['description'], string_types):
raise AnsibleError("Expected string in description of %s, got %s" % (o, type(opt['description']))) raise AnsibleError("Expected string in description of %s, got %s" % (o, type(opt['description'])))
text.append(key + DocCLI.warp_fill(DocCLI.tty_ify(opt['description']), limit, initial_indent=inline_indent, subsequent_indent=sub_indent)) text.append(key + DocCLI.warp_fill(DocCLI.tty_ify(opt['description']), limit,
initial_indent=inline_indent, subsequent_indent=sub_indent, initial_extra=len(extra_indent)))
del opt['description'] del opt['description']
suboptions = [] suboptions = []

@ -23,13 +23,13 @@ OPTIONS (= indicates it is required):
type: dict type: dict
options: options:
- subtest2 Another suboption. - subtest2 Another suboption.
default: null default: null
type: float type: float
added in: version 1.1.0 added in: version 1.1.0
suboptions: suboptions:
- subtest A suboption. - subtest A suboption.
default: null default: null
type: int type: int
added in: version 1.1.0 of testns.testcol added in: version 1.1.0 of testns.testcol
@ -67,7 +67,7 @@ RETURN VALUES:
type: dict type: dict
contains: contains:
- suboption A suboption. - suboption A suboption.
choices: [ARF, BARN, c_without_capital_first_letter] choices: [ARF, BARN, c_without_capital_first_letter]
type: str type: str
added in: version 1.4.0 of testns.testcol added in: version 1.4.0 of testns.testcol

@ -17,8 +17,8 @@ DEPRECATED:
OPTIONS (= indicates it is required): OPTIONS (= indicates it is required):
- sub Suboptions. Contains `sub.subtest', which can be set to `123'. - sub Suboptions. Contains `sub.subtest', which can be set to
You can use `TEST_ENV' to set this. `123'. You can use `TEST_ENV' to set this.
set_via: set_via:
env: env:
- deprecated: - deprecated:
@ -31,15 +31,17 @@ OPTIONS (= indicates it is required):
type: dict type: dict
options: options:
- subtest2 Another suboption. Useful when [[ansible.builtin.shuffle]] - subtest2 Another suboption. Useful when
is used with value `[a,b,),d\]'. [[ansible.builtin.shuffle]] is used with value
`[a,b,),d\]'.
default: null default: null
type: float type: float
added in: version 1.1.0 added in: version 1.1.0
suboptions: suboptions:
- subtest A suboption. Not compatible to `path=c:\foo(1).txt' (of - subtest A suboption. Not compatible to
module ansible.builtin.copy). `path=c:\foo(1).txt' (of module
ansible.builtin.copy).
default: null default: null
type: int type: int
added in: version 1.1.0 of testns.testcol added in: version 1.1.0 of testns.testcol
@ -102,7 +104,7 @@ RETURN VALUES:
type: dict type: dict
contains: contains:
- suboption A suboption. - suboption A suboption.
choices: [ARF, BARN, c_without_capital_first_letter] choices: [ARF, BARN, c_without_capital_first_letter]
type: str type: str
added in: version 1.4.0 of testns.testcol added in: version 1.4.0 of testns.testcol

@ -17,7 +17,7 @@ RETURN VALUES:
type: dict type: dict
contains: contains:
- suboption A suboption. - suboption A suboption.
choices: [ARF, BARN, c_without_capital_first_letter] choices: [ARF, BARN, c_without_capital_first_letter]
type: str type: str

@ -8,20 +8,20 @@ OPTIONS (= indicates it is required):
type: dict type: dict
suboptions: suboptions:
- a_first The first suboption. - a_first The first suboption.
default: null default: null
type: str type: str
- m_middle The suboption in the middle. - m_middle The suboption in the middle.
Has its own suboptions. Has its own suboptions.
default: null default: null
suboptions: suboptions:
- a_suboption A sub-suboption. - a_suboption A sub-suboption.
default: null default: null
type: str type: str
- z_last The last suboption. - z_last The last suboption.
default: null default: null
type: str type: str

@ -12,7 +12,7 @@ OPTIONS (= indicates it is required):
type: list type: list
suboptions: suboptions:
= port Rule port = port Rule port
type: int type: int
- ingress Ingress firewall rules - ingress Ingress firewall rules
@ -21,7 +21,7 @@ OPTIONS (= indicates it is required):
type: list type: list
suboptions: suboptions:
= port Rule port = port Rule port
type: int type: int
- last_one Short desc - last_one Short desc

Loading…
Cancel
Save