Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add collapsible option to admonition directives #12507

Merged
merged 14 commits into from
Jan 29, 2025
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Features added
* #13271: Support the ``:abstract:`` option for
classes, methods, and properties in the Python domain.
Patch by Adam Turner.
* #12507: Add the :ref:`collapsible <collapsible-admonitions>` option
to admonition directives.
Patch by Chris Sewell.

Bugs fixed
----------
Expand Down
2 changes: 2 additions & 0 deletions doc/usage/restructuredtext/directives.rst
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ and the generic :rst:dir:`admonition` directive.

.. rubric:: Collapsible text

.. versionadded:: 8.2.0
AA-Turner marked this conversation as resolved.
Show resolved Hide resolved

Each admonition directive supports a ``:collapsible:`` option,
to make the content of the admonition collapsible
(where supported by the output format).
Expand Down
Empty file.
22 changes: 22 additions & 0 deletions tests/roots/test-directives-admonition-collapse/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
test-directives-admonition-collapse
===================================

.. note::
:class: standard

This is a standard note.

.. note::
:collapsible:

This note is collapsible, and initially open by default.

.. admonition:: Example
:collapsible: open

This example is collapsible, and initially open.

.. hint::
:collapsible: closed

This hint is collapsible, but initially closed.
44 changes: 44 additions & 0 deletions tests/test_builders/test_build_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,3 +715,47 @@ def __call__(self, nodes):
r'.//dt[@id="MyList"][1]',
chk('class MyList[\nT\n](list[T])'),
)


@pytest.mark.sphinx('html', testroot='directives-admonition-collapse')
def test_html_admonition_collapse(app):
app.build()
fname = app.outdir / 'index.html'
etree = etree_parse(fname)

def _create_check(text: str, open: bool): # type: ignore[no-untyped-def]
def _check(els):
assert len(els) == 1
el = els[0]
if open:
assert el.attrib['open'] == 'open'
else:
assert 'open' not in el.attrib
assert el.find('p').text == text

return _check

check_xpath(
etree,
fname,
r'.//div[@class="standard admonition note"]//p',
'This is a standard note.',
)
check_xpath(
etree,
fname,
r'.//details[@class="admonition note"]',
_create_check('This note is collapsible, and initially open by default.', True),
)
check_xpath(
etree,
fname,
r'.//details[@class="admonition-example admonition"]',
_create_check('This example is collapsible, and initially open.', True),
)
check_xpath(
etree,
fname,
r'.//details[@class="admonition hint"]',
_create_check('This hint is collapsible, but initially closed.', False),
)
Loading