Skip to content

Commit 3fffdb8

Browse files
authored
Merge pull request #7 from choldgraf/directive
adding toggle directive
2 parents e3025a3 + 2f2bd88 commit 3fffdb8

File tree

7 files changed

+203
-76
lines changed

7 files changed

+203
-76
lines changed

.circleci/config.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
version: 2
2+
jobs:
3+
build_docs:
4+
docker:
5+
- image: circleci/python:3.6-stretch
6+
steps:
7+
# Get our data and merge with upstream
8+
- run: sudo apt-get update
9+
- checkout
10+
11+
- restore_cache:
12+
keys:
13+
- cache-pip
14+
15+
- run: pip install --user .[sphinx]
16+
17+
- save_cache:
18+
key: cache-pip
19+
paths:
20+
- ~/.cache/pip
21+
22+
# Build the docs
23+
- run:
24+
name: Build docs to store
25+
command: |
26+
cd docs
27+
make html
28+
29+
- store_artifacts:
30+
path: docs/_build/html/
31+
destination: html
32+
33+
34+
workflows:
35+
version: 2
36+
default:
37+
jobs:
38+
- build_docs

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: 80 additions & 63 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,20 +50,75 @@ 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.
61+
62+
The toggle directive
63+
--------------------
64+
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:
67+
68+
.. code-block:: rst
69+
70+
.. toggle::
71+
72+
Here is my toggle-able content!
73+
74+
The code above results in:
75+
76+
.. toggle::
77+
78+
Here is my toggle-able content!
79+
80+
You can also add titles to your toggle-able content:
81+
82+
.. code-block:: rst
83+
84+
.. toggle:: My title
85+
86+
Here is my toggle-able content!
87+
88+
Which results in:
89+
90+
91+
.. toggle:: My title
92+
93+
Here is my toggle-able content!
94+
95+
To show the toggle-able content by default, use the ``:show:`` flag.
96+
97+
.. code-block:: rst
98+
99+
.. toggle::
100+
:show:
101+
102+
Here is my toggle-able content!
103+
104+
It results in the following:
105+
106+
.. toggle::
107+
:show:
108+
109+
Here is my toggle-able content!
110+
111+
112+
Toggling content by adding classes
113+
----------------------------------
114+
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.
60118

61119
For example, this code would create a toggle-able "note" admonition:
62120

63-
.. code:: rst
121+
.. code-block:: rst
64122
65123
.. note::
66124
:class: toggle
@@ -77,14 +135,9 @@ Here's how it looks:
77135
Clicking on the toggle button will toggle the item's visibility.
78136

79137

80-
Show content by default
81-
-----------------------
138+
To show the content by default, add a ``toggle-shown`` class as well.
82139

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:
86-
87-
.. code:: rst
140+
.. code-block:: rst
88141
89142
.. note::
90143
:class: toggle, toggle-shown
@@ -98,28 +151,6 @@ This will generate the following block:
98151

99152
This is my note.
100153

101-
Toggle any container of content
102-
-------------------------------
103-
104-
You can also use **containers** to add arbitrary toggle-able code. For example,
105-
here's a container with an image inside:
106-
107-
.. container:: toggle
108-
109-
.. admonition:: Look at that, an image!
110-
111-
.. image:: https://media.giphy.com/media/mW05nwEyXLP0Y/giphy.gif
112-
113-
It was generated with this code:
114-
115-
.. code:: rst
116-
117-
.. container:: toggle
118-
119-
.. admonition:: Look at that, an image!
120-
121-
.. image:: https://media.giphy.com/media/mW05nwEyXLP0Y/giphy.gif
122-
123154
Here's how they look right after one another:
124155

125156
.. note::
@@ -132,28 +163,14 @@ Here's how they look right after one another:
132163

133164
This is my second.
134165

135-
Customize the selector words used to toggle content
136-
---------------------------------------------------
137-
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``):
141166

142-
.. code-block:: python
143-
144-
togglebutton_selector = "<your-selector>
167+
Configuration
168+
=============
145169

146-
For example, the documentation with this site uses the following configuration
147-
value:
170+
You can control the "hint" text that is displayed next to togglebuttons when
171+
their content is collapsed. To do so, use the following configuration variable
172+
in your ``conf.py`` file:
148173

149174
.. code-block:: python
150175
151-
togglebutton_selector = ".toggle, .secondtoggle"
152-
153-
This means that any element with either of these classes will have toggle
154-
buttons added to them.
155-
156-
.. note::
157-
:class: secondtoggle
158-
159-
A note with a ``.secondtoggle`` class.
176+
togglebutton_hint = "My text"

setup.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import os
2-
2+
from pathlib import Path
33
from setuptools import setup, find_packages
4-
from sphinx_togglebutton import __version__
4+
5+
version = [
6+
line
7+
for line in Path("sphinx_togglebutton/__init__.py").read_text().split()
8+
if "__version__" in line
9+
]
10+
version = version[0].split(" = ")[-1]
511

612
with open("./README.rst", "r") as ff:
713
readme_text = ff.read()
814

915
setup(
1016
name="sphinx-togglebutton",
11-
version=__version__,
17+
version=version,
1218
description="Add a toggle button to items on a page.",
1319
long_description=readme_text,
1420
long_description_content_type="text/x-rst",
@@ -18,9 +24,9 @@
1824
license="MIT License",
1925
packages=find_packages(),
2026
package_data={
21-
"sphinx_togglebutton": ["_static/togglebutton.css", "_static/togglebutton.js"]
27+
"sphinx_togglebutton": ["_static/togglebutton.css_t", "_static/togglebutton.js"]
2228
},
23-
install_requires=["setuptools", "wheel", "sphinx"],
24-
extras_require={"sphinx": ["sphinx"]},
29+
install_requires=["setuptools", "wheel", "sphinx", "docutils"],
30+
extras_require={"sphinx": ["myst_nb"]},
2531
classifiers=["License :: OSI Approved :: MIT License"],
2632
)

sphinx_togglebutton/__init__.py

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

46
__version__ = "0.0.3dev0"
57

@@ -9,6 +11,11 @@ def st_static_path(app):
911
app.config.html_static_path.append(static_path)
1012

1113

14+
def add_to_context(app, config):
15+
# Update the global context
16+
config.html_context.update({'togglebutton_hint': config.togglebutton_hint})
17+
18+
1219
# This function reads in a variable and inserts it into JavaScript
1320
def insert_custom_selection_config(app):
1421
# This is a configuration that you've specified for users in `conf.py`
@@ -17,6 +24,34 @@ def insert_custom_selection_config(app):
1724
app.add_js_file(None, body=js_text)
1825

1926

27+
class Toggle(Directive):
28+
"""Hide a block of markup text by wrapping it in a container."""
29+
30+
optional_arguments = 1
31+
final_argument_whitespace = True
32+
has_content = True
33+
34+
option_spec = {"id": directives.unchanged, "show": directives.flag}
35+
36+
def run(self):
37+
self.assert_has_content()
38+
classes = ["toggle"]
39+
if "show" in self.options:
40+
classes.append("toggle-shown")
41+
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
52+
return [parent]
53+
54+
2055
# We connect this function to the step after the builder is initialized
2156
def setup(app):
2257
# Add our static path
@@ -28,11 +63,13 @@ def setup(app):
2863
# Add the string we'll use to select items in the JS
2964
# Tell Sphinx about this configuration variable
3065
app.add_config_value("togglebutton_selector", ".toggle", "html")
66+
app.add_config_value("togglebutton_hint", "Click to show", "html")
3167
app.add_js_file("togglebutton.js")
3268

3369
# Run the function after the builder is initialized
3470
app.connect("builder-inited", insert_custom_selection_config)
35-
71+
app.connect("config-inited", add_to_context)
72+
app.add_directive("toggle", Toggle)
3673
return {
3774
"version": __version__,
3875
"parallel_read_safe": True,

0 commit comments

Comments
 (0)