mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Close #8573: napoleon: Add more custom section styles
This commit is contained in:
parent
1b7d16505e
commit
7ad85cd1e0
@ -547,3 +547,27 @@ sure that "sphinx.ext.napoleon" is enabled in `conf.py`::
|
||||
has an annotation in the class body, that type is used.
|
||||
|
||||
.. versionadded:: 3.4
|
||||
|
||||
.. confval:: napoleon_custom_sections
|
||||
|
||||
Add a list of custom sections to include, expanding the list of parsed sections.
|
||||
*Defaults to None.*
|
||||
|
||||
The entries can either be strings or tuples, depending on the intention:
|
||||
|
||||
* To create a custom "generic" section, just pass a string.
|
||||
* To create an alias for an existing section, pass a tuple containing the
|
||||
alias name and the original, in that order.
|
||||
* To create a custom section that displays like the parameters or returns
|
||||
section, pass a tuple containing the custom section name and a string
|
||||
value, "params_style" or "returns_style".
|
||||
|
||||
If an entry is just a string, it is interpreted as a header for a generic
|
||||
section. If the entry is a tuple/list/indexed container, the first entry
|
||||
is the name of the section, the second is the section key to emulate. If the
|
||||
second entry value is "params_style" or "returns_style", the custom section
|
||||
will be displayed like the parameters section or returns section.
|
||||
|
||||
.. versionadded:: 1.8
|
||||
.. versionchanged:: 3.5
|
||||
Support ``params_style`` and ``returns_style``
|
@ -253,10 +253,15 @@ class Config:
|
||||
* To create a custom "generic" section, just pass a string.
|
||||
* To create an alias for an existing section, pass a tuple containing the
|
||||
alias name and the original, in that order.
|
||||
* To create a custom section that displays like the parameters or returns
|
||||
section, pass a tuple containing the custom section name and a string
|
||||
value, "params_style" or "returns_style".
|
||||
|
||||
If an entry is just a string, it is interpreted as a header for a generic
|
||||
section. If the entry is a tuple/list/indexed container, the first entry
|
||||
is the name of the section, the second is the section key to emulate.
|
||||
is the name of the section, the second is the section key to emulate. If the
|
||||
second entry value is "params_style" or "returns_style", the custom section
|
||||
will be displayed like the parameters section or returns section.
|
||||
|
||||
napoleon_attr_annotations : :obj:`bool` (Defaults to True)
|
||||
Use the type annotations of class attributes that are documented in the docstring
|
||||
|
@ -549,6 +549,13 @@ class GoogleDocstring:
|
||||
self._sections[entry.lower()] = self._parse_custom_generic_section
|
||||
else:
|
||||
# otherwise, assume entry is container;
|
||||
if entry[1] == "params_style":
|
||||
self._sections[entry[0].lower()] = \
|
||||
self._parse_custom_params_style_section
|
||||
elif entry[1] == "returns_style":
|
||||
self._sections[entry[0].lower()] = \
|
||||
self._parse_custom_returns_style_section
|
||||
else:
|
||||
# [0] is new section, [1] is the section to alias.
|
||||
# in the case of key mismatch, just handle as generic section.
|
||||
self._sections[entry[0].lower()] = \
|
||||
@ -641,6 +648,13 @@ class GoogleDocstring:
|
||||
# for now, no admonition for simple custom sections
|
||||
return self._parse_generic_section(section, False)
|
||||
|
||||
def _parse_custom_params_style_section(self, section: str) -> List[str]:
|
||||
return self._format_fields(section, self._consume_fields())
|
||||
|
||||
def _parse_custom_returns_style_section(self, section: str) -> List[str]:
|
||||
fields = self._consume_returns_section()
|
||||
return self._format_fields(section, fields)
|
||||
|
||||
def _parse_usage_section(self, section: str) -> List[str]:
|
||||
header = ['.. rubric:: Usage:', '']
|
||||
block = ['.. code-block:: python', '']
|
||||
|
@ -1072,10 +1072,27 @@ You should listen to me!
|
||||
Sooper Warning:
|
||||
Stop hitting yourself!
|
||||
""", """:Warns: **Stop hitting yourself!**
|
||||
"""),
|
||||
("""\
|
||||
Params Style:
|
||||
arg1 (int): Description of arg1
|
||||
arg2 (str): Description of arg2
|
||||
|
||||
""", """\
|
||||
:Params Style: * **arg1** (*int*) -- Description of arg1
|
||||
* **arg2** (*str*) -- Description of arg2
|
||||
"""),
|
||||
("""\
|
||||
Returns Style:
|
||||
description of custom section
|
||||
|
||||
""", """:Returns Style: description of custom section
|
||||
"""))
|
||||
|
||||
testConfig = Config(napoleon_custom_sections=['Really Important Details',
|
||||
('Sooper Warning', 'warns')])
|
||||
('Sooper Warning', 'warns'),
|
||||
('Params Style', 'params_style'),
|
||||
('Returns Style', 'returns_style')])
|
||||
|
||||
for docstring, expected in docstrings:
|
||||
actual = str(GoogleDocstring(docstring, testConfig))
|
||||
|
Loading…
Reference in New Issue
Block a user