Merge pull request #7492 from tk0miya/7490_autosummary_caption

Close #7490: autosummary: Add ``:caption:`` option to autosummary directive
This commit is contained in:
Takeshi KOMIYA 2020-04-20 00:52:45 +09:00 committed by GitHub
commit 12cb90c3fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 1 deletions

View File

@ -25,6 +25,8 @@ Features added
* C, added scope control directives, :rst:dir:`c:namespace`, * C, added scope control directives, :rst:dir:`c:namespace`,
:rst:dir:`c:namespace-push`, and :rst:dir:`c:namespace-pop`. :rst:dir:`c:namespace-push`, and :rst:dir:`c:namespace-pop`.
* #7466: autosummary: headings in generated documents are not translated * #7466: autosummary: headings in generated documents are not translated
* #7490: autosummary: Add ``:caption:`` option to autosummary directive to set a
caption to the toctree
* #7481: html theme: Add right margin to footnote/citation labels * #7481: html theme: Add right margin to footnote/citation labels
* #7482: html theme: CSS spacing for code blocks with captions and line numbers * #7482: html theme: CSS spacing for code blocks with captions and line numbers
* #7443: html theme: Add new options :confval:`globaltoc_collapse` and * #7443: html theme: Add new options :confval:`globaltoc_collapse` and

View File

@ -76,6 +76,12 @@ The :mod:`sphinx.ext.autosummary` extension does this in two parts:
directory. If no argument is given, output is placed in the same directory directory. If no argument is given, output is placed in the same directory
as the file that contains the directive. as the file that contains the directive.
You can also use ``caption`` option to give a caption to the toctree.
.. versionadded:: 3.1
caption option added.
* If you don't want the :rst:dir:`autosummary` to show function signatures in * If you don't want the :rst:dir:`autosummary` to show function signatures in
the listing, include the ``nosignatures`` option:: the listing, include the ``nosignatures`` option::

View File

@ -228,6 +228,7 @@ class Autosummary(SphinxDirective):
final_argument_whitespace = False final_argument_whitespace = False
has_content = True has_content = True
option_spec = { option_spec = {
'caption': directives.unchanged_required,
'toctree': directives.unchanged, 'toctree': directives.unchanged,
'nosignatures': directives.flag, 'nosignatures': directives.flag,
'template': directives.unchanged, 'template': directives.unchanged,
@ -270,9 +271,14 @@ class Autosummary(SphinxDirective):
tocnode['entries'] = [(None, docn) for docn in docnames] tocnode['entries'] = [(None, docn) for docn in docnames]
tocnode['maxdepth'] = -1 tocnode['maxdepth'] = -1
tocnode['glob'] = None tocnode['glob'] = None
tocnode['caption'] = self.options.get('caption')
nodes.append(autosummary_toc('', '', tocnode)) nodes.append(autosummary_toc('', '', tocnode))
if 'toctree' not in self.options and 'caption' in self.options:
logger.warning(__('A captioned autosummary requires :toctree: option. ignored.'),
location=nodes[-1])
return nodes return nodes
def get_items(self, names: List[str]) -> List[Tuple[str, str, str, str]]: def get_items(self, names: List[str]) -> List[Tuple[str, str, str, str]]:

View File

@ -5,6 +5,7 @@
.. autosummary:: .. autosummary::
:toctree: generated :toctree: generated
:caption: An autosummary
autosummary_dummy_module autosummary_dummy_module
autosummary_dummy_module.Foo autosummary_dummy_module.Foo

View File

@ -197,7 +197,7 @@ def test_autosummary_generate(app, status, warning):
nodes.paragraph, nodes.paragraph,
addnodes.tabular_col_spec, addnodes.tabular_col_spec,
autosummary_table, autosummary_table,
autosummary_toc)) [autosummary_toc, addnodes.toctree]))
assert_node(doctree[3], assert_node(doctree[3],
[autosummary_table, nodes.table, nodes.tgroup, (nodes.colspec, [autosummary_table, nodes.table, nodes.tgroup, (nodes.colspec,
nodes.colspec, nodes.colspec,
@ -205,6 +205,8 @@ def test_autosummary_generate(app, status, warning):
nodes.row, nodes.row,
nodes.row, nodes.row,
nodes.row)])]) nodes.row)])])
assert_node(doctree[4][0], addnodes.toctree, caption="An autosummary")
assert doctree[3][0][0][2][0].astext() == 'autosummary_dummy_module\n\n' assert doctree[3][0][0][2][0].astext() == 'autosummary_dummy_module\n\n'
assert doctree[3][0][0][2][1].astext() == 'autosummary_dummy_module.Foo()\n\n' assert doctree[3][0][0][2][1].astext() == 'autosummary_dummy_module.Foo()\n\n'
assert doctree[3][0][0][2][2].astext() == 'autosummary_dummy_module.bar(x[, y])\n\n' assert doctree[3][0][0][2][2].astext() == 'autosummary_dummy_module.bar(x[, y])\n\n'