Skip to content

Commit 8cebb39

Browse files
committed
using classes for admonition toggles
1 parent 7ff88d9 commit 8cebb39

File tree

5 files changed

+139
-140
lines changed

5 files changed

+139
-140
lines changed

docs/conf.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
# ones.
4141
extensions = ["myst_nb", "sphinx_togglebutton"]
4242

43-
togglebutton_selector = ".toggle, .toggle-this-item"
44-
4543
# Add any paths that contain templates here, relative to this directory.
4644
templates_path = ["_templates"]
4745

@@ -76,13 +74,12 @@
7674
# a list of builtin themes.
7775
#
7876
html_theme = "sphinx_book_theme"
79-
# html_theme = 'sphinx_rtd_theme'
77+
# html_theme = "sphinx_rtd_theme"
78+
# html_theme = "alabaster"
8079

81-
# Theme options are theme-specific and customize the look and feel of a theme
82-
# further. For a list of options available for each theme, see the
83-
# documentation.
84-
#
85-
# html_theme_options = {}
80+
html_theme_options = {
81+
"single_page": True,
82+
}
8683

8784
# Add any paths that contain custom static files (such as style sheets) here,
8885
# relative to this directory. They are copied after the builtin static files,

docs/index.rst

Lines changed: 93 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,32 @@ sphinx-togglebutton
55
A small sphinx extension to make it possible to add a "toggle button" to
66
sections of your page.
77

8-
For example, click the "+" button to the right:
8+
.. admonition:: For example, click the "+" button to the right:
9+
:class: dropdown
910

10-
.. toggle::
11-
12-
.. note:: Here's a toggled admonition
13-
14-
It was created with this code:
11+
Here's a toggled note! You can put all kinds of stuff in here!
1512

16-
.. code-block:: rst
13+
You can also add a toggle button to arbitrary chunks of content.
14+
For example, click the toggle button to the right just below.
1715

18-
.. toggle::
16+
.. toggle::
1917

20-
.. note:: Here's a toggled admonition
18+
.. admonition:: Wow!
19+
:class: tip
2120

22-
You can also add a title to your toggled block. The title will show up,
23-
and the toggle button will change the block's content. For example:
21+
It's a code block!
2422

25-
.. toggle:: Toggle to see what's inside
23+
.. code-block:: python
2624
27-
It's a code block!
25+
a = "wow, very python"
2826
29-
.. code-block:: python
27+
See :ref:`usage` for more information.
3028

31-
a = "wow, very python"
32-
print("this code should be toggle-able!")
29+
.. caution::
3330

31+
``sphinx-togglebutton`` is designed for the
32+
`sphinx-book-theme <https://sphinx-book-theme.readthedocs.io/>`_. It should work
33+
properly on other themes, but if you notice any CSS bugs, please open an issue!
3434

3535
Installation
3636
============
@@ -41,12 +41,8 @@ You can install `sphinx-togglebutton` with `pip`:
4141
4242
pip install sphinx-togglebutton
4343
44-
45-
Usage
46-
=====
47-
48-
In your ``conf.py`` configuration file, add ``sphinx_togglebutton``
49-
to your extensions list.
44+
Then, activate it in your ``sphinx`` build by adding it to your ``conf.py`` configuration
45+
file, like so:
5046

5147
E.g.:
5248

@@ -58,110 +54,113 @@ E.g.:
5854
...
5955
]
6056
57+
See :ref:`usage` for information about how to use ``sphinx-togglebutton``.
6158

62-
The toggle directive
63-
--------------------
59+
.. _usage:
6460

65-
To add toggle-able content, use the **toggle directive**. This directive
66-
will wrap its content in a toggle-able container. You can call it like so:
61+
Usage
62+
=====
6763

68-
.. code-block:: rst
64+
There are two main ways to use ``sphinx-togglebutton``:
6965

70-
.. toggle::
66+
* Create dropdown admonitions with the ``dropdown`` class
67+
* Make arbitrary chunks of content "toggle-able" with the ``toggle::`` directive
7168

72-
Here is my toggle-able content!
69+
.. _dropdown-admonitions:
7370

74-
The code above results in:
71+
Dropdown admonitions by adding classes
72+
--------------------------------------
7573

76-
.. toggle::
74+
Making dropdown admonitions allows you to insert extra information in your document
75+
without forcing the user to see that content. For example:
7776

78-
Here is my toggle-able content!
77+
.. admonition:: What could be inside this warning?
78+
:class: warning, dropdown
7979

80-
You can also add titles to your toggle-able content:
80+
A whale of a joke!
8181

82-
.. code-block:: rst
82+
.. image:: https://media.giphy.com/media/FaKV1cVKlVRxC/giphy.gif
8383

84-
.. toggle:: My title
84+
(sorry)
8585

86-
Here is my toggle-able content!
86+
Create a dropdown admonition by adding the ``dropdown`` class to an admonition directive.
87+
For example, like so:
8788

88-
Which results in:
89+
.. code-block:: rst
8990
91+
.. note::
92+
:class: dropdown
9093
91-
.. toggle:: My title
94+
My note
9295
93-
Here is my toggle-able content!
94-
95-
To show the toggle-able content by default, use the ``:show:`` flag.
96+
Note that you can use a custom admonition title and apply the style of a "built-in"
97+
admonition (e.g., ``note``, ``warning``, etc) with the ``admonition::`` directive:
9698

9799
.. code-block:: rst
98100
99-
.. toggle::
100-
:show:
101-
102-
Here is my toggle-able content!
103-
104-
It results in the following:
105-
106-
.. toggle::
107-
:show:
101+
.. admonition:: Here's my title
102+
:class: dropdown, warning
108103
109-
Here is my toggle-able content!
104+
My note
110105
106+
Creates:
111107

112-
Toggling content by adding classes
113-
----------------------------------
108+
.. admonition:: Here's my title
109+
:class: dropdown, warning
114110

115-
You can also make elements toggle-able by adding the ``toggle`` class to
116-
them. This can be done with admonitions and containers with the
117-
``:class: my, classes`` keyword.
111+
My custom admonition!
118112

119-
For example, this code would create a toggle-able "note" admonition:
113+
To show the content by default, add a ``toggle-shown`` class as well.
120114

121115
.. code-block:: rst
122116
123117
.. note::
124-
:class: toggle
118+
:class: dropdown, toggle-shown
125119
126120
This is my note.
127121
128-
Here's how it looks:
122+
This will generate the following block:
129123

130124
.. note::
131-
:class: toggle
125+
:class: dropdown, toggle-shown
132126

133127
This is my note.
134128

135-
Clicking on the toggle button will toggle the item's visibility.
129+
.. _toggle-directive:
136130

131+
Toggle any content with the toggle directive
132+
--------------------------------------------
137133

138-
To show the content by default, add a ``toggle-shown`` class as well.
134+
To add toggle-able content, use the **toggle directive**. This directive
135+
will wrap its content in a toggle-able container. You can call it like so:
139136

140137
.. code-block:: rst
141138
142-
.. note::
143-
:class: toggle, toggle-shown
139+
.. toggle::
144140
145-
This is my note.
141+
Here is my toggle-able content!
146142
147-
This will generate the following block:
143+
The code above results in:
148144

149-
.. note::
150-
:class: toggle, toggle-shown
145+
.. toggle::
151146

152-
This is my note.
147+
Here is my toggle-able content!
153148

154-
Here's how they look right after one another:
149+
To show the toggle-able content by default, use the ``:show:`` flag.
155150

156-
.. note::
157-
:class: toggle
151+
.. code-block:: rst
158152
159-
This is my note.
153+
.. toggle::
154+
:show:
160155
161-
.. note::
162-
:class: toggle
156+
Here is my toggle-able content!
163157
164-
This is my second.
158+
It results in the following:
159+
160+
.. toggle::
161+
:show:
162+
163+
Here is my toggle-able content!
165164

166165

167166
Configuration
@@ -170,42 +169,38 @@ Configuration
170169
Below are a few configuration points for ``sphinx-togglebutton``.
171170

172171

173-
Control the selector text used to make elements toggle-able
174-
-----------------------------------------------------------
172+
Control the togglebutton hover text
173+
-----------------------------------
175174

176-
By default, ``sphinx-togglebutton`` selects any items that have the class ``.toggle``
177-
and adds a toggle-button to them. If you'd like to change this class, for example to
178-
select a different kind of HTML element, you may configure this field manually like so
175+
You can control the "hint" text that is displayed next to togglebuttons when
176+
their content is collapsed. To do so, use the following configuration variable
179177
in your ``conf.py`` file:
180178

181179
.. code-block:: python
182180
183-
togglebutton_selector = "your-selector"
181+
togglebutton_hint = "My text"
184182
185-
For example, if you wanted to add toggle-buttons to all HTML elements that had a
186-
``toggle-this-item`` class, in *addition* to the default class of ``toggle``,
187-
you could do so with the following configuration:
183+
Reference
184+
=========
188185

189-
.. code-block:: python
186+
This is a simple reference section to double-check styling etc.
190187

191-
togglebutton_selector = ".toggle, .toggle-this-item"
188+
Here's how they look right after one another:
192189

190+
.. note::
191+
:class: toggle
193192

194-
This is what has been done for the toggle-able section below:
193+
This is my note.
195194

196195
.. note::
197-
:class: toggle-this-item
198-
199-
A toggled note with a custom class to trigger toggling.
196+
:class: toggle
200197

198+
This is my second.
201199

202-
Control the togglebutton hover text
203-
-----------------------------------
200+
.. toggle::
204201

205-
You can control the "hint" text that is displayed next to togglebuttons when
206-
their content is collapsed. To do so, use the following configuration variable
207-
in your ``conf.py`` file:
202+
This is my first.
208203

209-
.. code-block:: python
204+
.. toggle::
210205

211-
togglebutton_hint = "My text"
206+
This is my second.

sphinx_togglebutton/__init__.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,8 @@ def run(self):
3939
if "show" in self.options:
4040
classes.append("toggle-shown")
4141

42-
if len(self.arguments) == 0:
43-
parent = nodes.container(classes=classes)
44-
self.state.nested_parse(self.content, self.content_offset, parent)
45-
else:
46-
parent = nodes.admonition(classes=["toggle-body"])
47-
title = nodes.title(self.arguments[0], self.arguments[0])
48-
body = nodes.container(classes=classes)
49-
self.state.nested_parse(self.content, self.content_offset, body)
50-
parent += title
51-
parent += body
42+
parent = nodes.container(classes=classes)
43+
self.state.nested_parse(self.content, self.content_offset, parent)
5244
return [parent]
5345

5446

@@ -62,7 +54,7 @@ def setup(app):
6254

6355
# Add the string we'll use to select items in the JS
6456
# Tell Sphinx about this configuration variable
65-
app.add_config_value("togglebutton_selector", ".toggle", "html")
57+
app.add_config_value("togglebutton_selector", ".toggle, .admonition.dropdown", "html")
6658
app.add_config_value("togglebutton_hint", "Click to show", "html")
6759
app.add_js_file("togglebutton.js")
6860

0 commit comments

Comments
 (0)