From fe431159303c4f65130004479af6d77c16892603 Mon Sep 17 00:00:00 2001 From: Abhishek Date: Fri, 13 Nov 2026 01:10:17 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Keep=20captioned=20code=20blocks?= =?UTF-8?q?=20in=20tab-set-code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sphinx_design/tabs.py | 8 ++++++-- tests/test_misc.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/sphinx_design/tabs.py b/sphinx_design/tabs.py index 524e9f80..ba05f8cd 100644 --- a/sphinx_design/tabs.py +++ b/sphinx_design/tabs.py @@ -191,7 +191,11 @@ def run_with_defaults(self) -> list[nodes.Node]: if isinstance(item, nodes.target): new_children.append(item) continue - if not isinstance(item, nodes.literal_block): + code_block = item + if isinstance(item, nodes.container) and item.get("literal_block"): + # a code block with a caption is wrapped in a container + code_block = item.next_node(nodes.literal_block) + if not isinstance(code_block, nodes.literal_block): LOGGER.warning( f"All children of a 'tab-code-set' " f"should be a 'literal_block' [{WARNING_TYPE}.tab_code]", @@ -200,7 +204,7 @@ def run_with_defaults(self) -> list[nodes.Node]: subtype="tab_code", ) continue - language = item.get("language", "unknown") + language = code_block.get("language", "unknown") tab_label = nodes.rubric( language.upper(), nodes.Text(language.upper()), diff --git a/tests/test_misc.py b/tests/test_misc.py index 11bf1132..980dffc9 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -9,7 +9,7 @@ from sphinx_design.config import SdConfig, get_sd_config from sphinx_design.icons import get_material_icon_data, get_octicon_data from sphinx_design.shared import is_component -from sphinx_design.tabs import sd_tab_input +from sphinx_design.tabs import sd_tab_input, sd_tab_label try: import myst_parser # noqa: F401 @@ -407,6 +407,36 @@ def test_tab_set_code_with_target(sphinx_builder): assert f'id="{refid}"' in html +def test_tab_set_code_with_caption(sphinx_builder): + """A code block with a caption (wrapped in a container) should get a tab.""" + builder = sphinx_builder(conf_kwargs={"extensions": ["sphinx_design"]}) + builder.src_path.joinpath("index.rst").write_text( + """ +Test +==== + +.. tab-set-code:: + + .. code-block:: python + :caption: example.py + + a = 1 + + .. code-block:: javascript + + a = 1 +""", + encoding="utf8", + ) + builder.build() # asserts no warnings + doctree = builder.get_doctree("index", post_transforms=True) + labels = [label.astext() for label in doctree.findall(sd_tab_label)] + assert labels == ["PYTHON", "JAVASCRIPT"] + captions = list(doctree.findall(nodes.caption)) + assert len(captions) == 1 + assert captions[0].astext() == "example.py" + + I18N_INDEX_RST = """\ Heading =======