Skip to content

Commit 8249313

Browse files
committed
adding toggle directive
1 parent e3025a3 commit 8249313

File tree

6 files changed

+130
-72
lines changed

6 files changed

+130
-72
lines changed

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
# Add any Sphinx extension module names here, as strings. They can be
3939
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
4040
# ones.
41-
extensions = ["sphinx_togglebutton"]
41+
extensions = ["myst_nb", "sphinx_togglebutton"]
4242

43-
togglebutton_selector = ".toggle, .secondtoggle"
43+
# togglebutton_selector = ".toggle, .secondtoggle"
4444

4545
# Add any paths that contain templates here, relative to this directory.
4646
templates_path = ["_templates"]

docs/index.rst

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@ sections of your page.
77

88
For example, click the "+" button to the right:
99

10-
.. note:: Here's a toggled admonition
11-
:class: toggle
10+
.. toggle::
11+
12+
.. note:: Here's a toggled admonition
1213

1314
It was created with this code:
1415

15-
.. code:: rst
16+
.. code-block:: rst
1617
17-
.. note:: Here's a toggled admonition
18-
:class: toggle
18+
.. toggle::
19+
20+
.. note:: Here's a toggled admonition
1921
20-
(admonition body here)
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:
2124

22-
And here's a code block:
25+
.. toggle:: Toggle to see what's inside
2326

24-
.. container:: toggle
27+
It's a code block!
2528

26-
.. code:: python
29+
.. code-block:: python
2730
2831
a = "wow, very python"
2932
print("this code should be toggle-able!")
@@ -34,7 +37,7 @@ Installation
3437

3538
You can install `sphinx-togglebutton` with `pip`:
3639

37-
.. code:: bash
40+
.. code-block:: bash
3841
3942
pip install sphinx-togglebutton
4043
@@ -47,113 +50,115 @@ to your extensions list.
4750

4851
E.g.:
4952

50-
.. code:: python
53+
.. code-block:: python
5154
5255
extensions = [
5356
...
5457
'sphinx_togglebutton'
5558
...
5659
]
5760
58-
Now, whenever you wish for an admonition to be toggle-able, add the
59-
``:class: toggle`` parameter to the admonition directive that you use.
6061
61-
For example, this code would create a toggle-able "note" admonition:
62+
The toggle directive
63+
--------------------
6264

63-
.. code:: rst
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:
6467

65-
.. note::
66-
:class: toggle
68+
.. code-block:: rst
6769
68-
This is my note.
70+
.. toggle::
6971
70-
Here's how it looks:
72+
Here is my toggle-able content!
7173
72-
.. note::
73-
:class: toggle
74+
The code above results in:
7475

75-
This is my note.
76+
.. toggle::
7677

77-
Clicking on the toggle button will toggle the item's visibility.
78+
Here is my toggle-able content!
7879

80+
You can also add titles to your toggle-able content:
7981

80-
Show content by default
81-
-----------------------
82+
.. code-block:: rst
8283
83-
By default, all items with toggle buttons added to them will be hidden by
84-
default. You may also **show the content by default**. To do so, add the
85-
``toggle`` class *as well as* a ``toggle-shown`` class, like so:
84+
.. toggle:: My title
8685
87-
.. code:: rst
86+
Here is my toggle-able content!
8887
89-
.. note::
90-
:class: toggle, toggle-shown
88+
Which results in:
9189

92-
This is my note.
9390

94-
This will generate the following block:
91+
.. toggle:: My title
9592

96-
.. note::
97-
:class: toggle, toggle-shown
93+
Here is my toggle-able content!
9894

99-
This is my note.
95+
To show the toggle-able content by default, use the ``:show:`` flag.
10096

101-
Toggle any container of content
102-
-------------------------------
97+
.. code-block:: rst
10398
104-
You can also use **containers** to add arbitrary toggle-able code. For example,
105-
here's a container with an image inside:
99+
.. toggle::
100+
:show:
106101
107-
.. container:: toggle
102+
Here is my toggle-able content!
108103
109-
.. admonition:: Look at that, an image!
104+
It results in the following:
110105

111-
.. image:: https://media.giphy.com/media/mW05nwEyXLP0Y/giphy.gif
106+
.. toggle::
107+
:show:
112108

113-
It was generated with this code:
109+
Here is my toggle-able content!
114110

115-
.. code:: rst
116111

117-
.. container:: toggle
112+
Toggling content by adding classes
113+
----------------------------------
118114

119-
.. admonition:: Look at that, an image!
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.
120118

121-
.. image:: https://media.giphy.com/media/mW05nwEyXLP0Y/giphy.gif
119+
For example, this code would create a toggle-able "note" admonition:
122120

123-
Here's how they look right after one another:
121+
.. code-block:: rst
122+
123+
.. note::
124+
:class: toggle
125+
126+
This is my note.
127+
128+
Here's how it looks:
124129

125130
.. note::
126131
:class: toggle
127132

128133
This is my note.
129134

130-
.. note::
131-
:class: toggle
135+
Clicking on the toggle button will toggle the item's visibility.
132136

133-
This is my second.
134137

135-
Customize the selector words used to toggle content
136-
---------------------------------------------------
138+
To show the content by default, add a ``toggle-shown`` class as well.
137139

138-
``sphinx-togglebutton`` adds a toggle button to elements that are selected
139-
by a CSS selection query. By default, this is ``.toggle``. You can customize
140-
the query that is used with the following Sphinx parameter (in ``conf.py``):
140+
.. code-block:: rst
141141
142-
.. code-block:: python
142+
.. note::
143+
:class: toggle, toggle-shown
144+
145+
This is my note.
143146
144-
togglebutton_selector = "<your-selector>
147+
This will generate the following block:
145148

146-
For example, the documentation with this site uses the following configuration
147-
value:
149+
.. note::
150+
:class: toggle, toggle-shown
148151

149-
.. code-block:: python
152+
This is my note.
150153

151-
togglebutton_selector = ".toggle, .secondtoggle"
154+
Here's how they look right after one another:
152155

153-
This means that any element with either of these classes will have toggle
154-
buttons added to them.
156+
.. note::
157+
:class: toggle
158+
159+
This is my note.
155160

156161
.. note::
157-
:class: secondtoggle
162+
:class: toggle
158163

159-
A note with a ``.secondtoggle`` class.
164+
This is my second.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
"sphinx_togglebutton": ["_static/togglebutton.css", "_static/togglebutton.js"]
2222
},
2323
install_requires=["setuptools", "wheel", "sphinx"],
24-
extras_require={"sphinx": ["sphinx"]},
24+
extras_require={"sphinx": ["sphinx", "myst_nb"]},
2525
classifiers=["License :: OSI Approved :: MIT License"],
2626
)

sphinx_togglebutton/__init__.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""A small sphinx extension to add "toggle" buttons to items."""
22
import os
3+
import sphinx
4+
from docutils.parsers.rst import Directive, directives
5+
from docutils import nodes
36

47
__version__ = "0.0.3dev0"
58

@@ -17,6 +20,34 @@ def insert_custom_selection_config(app):
1720
app.add_js_file(None, body=js_text)
1821

1922

23+
class Toggle(Directive):
24+
"""Hide a block of markup text by wrapping it in a container."""
25+
26+
optional_arguments = 1
27+
final_argument_whitespace = True
28+
has_content = True
29+
30+
option_spec = {"id": directives.unchanged, "show": directives.flag}
31+
32+
def run(self):
33+
self.assert_has_content()
34+
classes = ["toggle"]
35+
if "show" in self.options:
36+
classes.append("toggle-shown")
37+
38+
if len(self.arguments) == 0:
39+
parent = nodes.container(classes=classes)
40+
self.state.nested_parse(self.content, self.content_offset, parent)
41+
else:
42+
parent = nodes.admonition(classes=["toggle-body"])
43+
title = nodes.title(self.arguments[0], self.arguments[0])
44+
body = nodes.container(classes=classes)
45+
self.state.nested_parse(self.content, self.content_offset, body)
46+
parent += title
47+
parent += body
48+
return [parent]
49+
50+
2051
# We connect this function to the step after the builder is initialized
2152
def setup(app):
2253
# Add our static path
@@ -32,7 +63,7 @@ def setup(app):
3263

3364
# Run the function after the builder is initialized
3465
app.connect("builder-inited", insert_custom_selection_config)
35-
66+
app.add_directive("toggle", Toggle)
3667
return {
3768
"version": __version__,
3869
"parallel_read_safe": True,

sphinx_togglebutton/_static/togglebutton.css

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
/* Visibility of the target */
2-
div.toggle-hidden {
2+
.toggle {
3+
transition: opacity .5s;
4+
}
5+
6+
.toggle-hidden {
37
visibility: hidden;
48
opacity: 0;
59
height: 1.5em;
610
margin: 0px !important;
711
padding: 0px !important;
812
}
913

14+
.toggle-body .admonition-title:after {
15+
content: "";
16+
}
17+
18+
.toggle-body button.toggle-button {
19+
margin-right: 0.5em;
20+
right: 0em;
21+
}
22+
23+
/* These should be totally hidden since they're inside an admonition */
24+
25+
.toggle-body .toggle-hidden * {
26+
margin: 0em;
27+
padding: 0em;
28+
}
29+
30+
1031
/* General button style */
1132
button.toggle-button {
1233
background: #999;

sphinx_togglebutton/_static/togglebutton.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var initToggleItems = () => {
2424
var toggleHidden = (button) => {
2525
target = button.dataset['target']
2626
var itemToToggle = document.getElementById(target);
27+
console.log(itemToToggle)
2728
if (itemToToggle.classList.contains("toggle-hidden")) {
2829
itemToToggle.classList.remove("toggle-hidden");
2930
button.classList.remove("toggle-button-hidden");

0 commit comments

Comments
 (0)