Add a class option to the toctree directive (#12524)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
Tim Hoffmann 2024-07-10 12:07:49 +02:00 committed by GitHub
parent c67ef51df9
commit a95d716f7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 32 additions and 1 deletions

View File

@ -56,6 +56,8 @@ Features added
* #12258: Support ``typing_extensions.Unpack`` * #12258: Support ``typing_extensions.Unpack``
Patch by Bénédikt Tran and Adam Turner. Patch by Bénédikt Tran and Adam Turner.
* #12524: Add a ``class`` option to the :rst:dir:`toctree` directive.
Patch by Tim Hoffmann.
Bugs fixed Bugs fixed
---------- ----------

View File

@ -380,6 +380,12 @@ Docutils supports the following directives:
When the default domain contains a ``class`` directive, this directive When the default domain contains a ``class`` directive, this directive
will be shadowed. Therefore, Sphinx re-exports it as ``rst-class``. will be shadowed. Therefore, Sphinx re-exports it as ``rst-class``.
.. tip::
If you want to add a class to a directive,
you may consider the ``:class:`` :dudir:`option <common-options>` instead,
which is supported by most directives and allows for a more compact notation.
* HTML specifics: * HTML specifics:
- :dudir:`meta` - :dudir:`meta`

View File

@ -124,6 +124,14 @@ tables of contents. The ``toctree`` directive is the central element.
foo foo
As with :dudir:`most directives <common-options>`,
you can use the ``class`` option to assign `class attributes`_::
.. toctree::
:class: custom-toc
.. _class attributes: https://docutils.sourceforge.io/docs/ref/doctree.html#classes
If you want only the titles of documents in the tree to show up, not other If you want only the titles of documents in the tree to show up, not other
headings of the same level, you can use the ``titlesonly`` option:: headings of the same level, you can use the ``titlesonly`` option::

View File

@ -53,6 +53,7 @@ class TocTree(SphinxDirective):
option_spec = { option_spec = {
'maxdepth': int, 'maxdepth': int,
'name': directives.unchanged, 'name': directives.unchanged,
'class': directives.class_option,
'caption': directives.unchanged_required, 'caption': directives.unchanged_required,
'glob': directives.flag, 'glob': directives.flag,
'hidden': directives.flag, 'hidden': directives.flag,
@ -78,7 +79,9 @@ class TocTree(SphinxDirective):
subnode['numbered'] = self.options.get('numbered', 0) subnode['numbered'] = self.options.get('numbered', 0)
subnode['titlesonly'] = 'titlesonly' in self.options subnode['titlesonly'] = 'titlesonly' in self.options
self.set_source_info(subnode) self.set_source_info(subnode)
wrappernode = nodes.compound(classes=['toctree-wrapper']) wrappernode = nodes.compound(
classes=['toctree-wrapper', *self.options.get('class', ())],
)
wrappernode.append(subnode) wrappernode.append(subnode)
self.add_name(wrappernode) self.add_name(wrappernode)

View File

@ -136,6 +136,18 @@ def test_reversed_toctree(app):
includefiles=['baz', 'bar/index', 'foo']) includefiles=['baz', 'bar/index', 'foo'])
@pytest.mark.sphinx(testroot='toctree-glob')
def test_toctree_class(app):
text = ('.. toctree::\n'
' :class: custom-toc\n'
'\n'
' foo\n')
app.env.find_files(app.config, app.builder)
doctree = restructuredtext.parse(app, text, 'index')
assert_node(doctree, [nodes.document, nodes.compound, addnodes.toctree])
assert doctree[0].attributes['classes'] == ['toctree-wrapper', 'custom-toc']
@pytest.mark.sphinx(testroot='toctree-glob') @pytest.mark.sphinx(testroot='toctree-glob')
def test_toctree_twice(app): def test_toctree_twice(app):
text = (".. toctree::\n" text = (".. toctree::\n"