Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sphinx_design/tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand All @@ -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()),
Expand Down
32 changes: 31 additions & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
=======
Expand Down