From 432bc5e50bdff91055b25615454c77f21268abeb Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sat, 4 Jan 2025 01:28:59 +0100 Subject: [PATCH] Add a class option to the autosummary directive (#13144) Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- CHANGES.rst | 2 ++ doc/usage/extensions/autosummary.rst | 10 ++++++++++ sphinx/ext/autosummary/__init__.py | 5 ++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 2a8b468722a..1d61320343c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -24,6 +24,8 @@ Features added * #11824: linkcode: Allow extensions to add support for a domain by defining the keys that should be present. Patch by Nicolas Peugnet. +* #13144: Add a ``class`` option to the :rst:dir:`autosummary` directive. + Patch by Tim Hoffmann. Bugs fixed ---------- diff --git a/doc/usage/extensions/autosummary.rst b/doc/usage/extensions/autosummary.rst index 0b2b0c69cf8..117c46a6487 100644 --- a/doc/usage/extensions/autosummary.rst +++ b/doc/usage/extensions/autosummary.rst @@ -63,6 +63,16 @@ The :mod:`sphinx.ext.autosummary` extension does this in two parts: .. rubric:: Options + .. rst:directive:option:: class: class names + :type: a list of class names, separated by spaces + + Assign `class attributes`_ to the table. + This is a :dudir:`common option `. + + .. _class attributes: https://docutils.sourceforge.io/docs/ref/doctree.html#classes + + .. versionadded:: 8.2 + .. rst:directive:option:: toctree: optional directory name If you want the :rst:dir:`autosummary` table to also serve as a diff --git a/sphinx/ext/autosummary/__init__.py b/sphinx/ext/autosummary/__init__.py index 699a534303e..f103e4921b7 100644 --- a/sphinx/ext/autosummary/__init__.py +++ b/sphinx/ext/autosummary/__init__.py @@ -234,6 +234,7 @@ class Autosummary(SphinxDirective): has_content = True option_spec: ClassVar[OptionSpec] = { 'caption': directives.unchanged_required, + 'class': directives.class_option, 'toctree': directives.unchanged, 'nosignatures': directives.flag, 'recursive': directives.flag, @@ -431,7 +432,9 @@ def get_table(self, items: list[tuple[str, str, str, str]]) -> list[Node]: table_spec['spec'] = r'\X{1}{2}\X{1}{2}' table = autosummary_table('') - real_table = nodes.table('', classes=['autosummary longtable']) + real_table = nodes.table( + '', classes=['autosummary', 'longtable', *self.options.get('class', ())] + ) table.append(real_table) group = nodes.tgroup('', cols=2) real_table.append(group)