-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy path__init__.py
More file actions
90 lines (70 loc) · 3.29 KB
/
__init__.py
File metadata and controls
90 lines (70 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""A small sphinx extension to add "toggle" buttons to items."""
import os
from docutils.parsers.rst import Directive, directives
from docutils import nodes
import base64
__version__ = "0.3.2"
def st_static_path(app):
static_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "_static"))
app.config.html_static_path.append(static_path)
def initialize_js_assets(app, config):
# Update the global context
app.add_js_file(None, body=f"let toggleHintShow = '{config.togglebutton_hint}';")
app.add_js_file(None, body=f"let toggleHintHide = '{config.togglebutton_hint_hide}';")
open_print = str(config.togglebutton_open_on_print).lower()
app.add_js_file(None, body=f"let toggleOpenOnPrint = '{open_print}';")
app.add_js_file("togglebutton.js")
# This function reads in a variable and inserts it into JavaScript
def insert_custom_selection_config(app):
# This is a configuration that you've specified for users in `conf.py`
selector = app.config["togglebutton_selector"]
js_text = "var togglebuttonSelector = '%s';" % selector
app.add_js_file(None, body=js_text)
class Toggle(Directive):
"""Hide a block of markup text by wrapping it in a container."""
optional_arguments = 3
final_argument_whitespace = True
has_content = True
option_spec = {
"id": directives.unchanged,
"show": directives.flag,
"hint": directives.unchanged,
"hint_hide": directives.unchanged,
}
def run(self):
self.assert_has_content()
classes = ["toggle"]
if "show" in self.options:
classes.append("toggle-shown")
# Retrieve global hints if specific hints are not provided
env = self.state.document.settings.env
hint = self.options.get('hint', env.config.togglebutton_hint)
hint_hide = self.options.get('hint_hide', env.config.togglebutton_hint_hide)
hint_encoded = base64.urlsafe_b64encode(hint.encode()).decode().replace('=', 'EQ')
classes.append(f"hint-show-{hint_encoded}")
hint_hide_encoded = base64.urlsafe_b64encode(hint_hide.encode()).decode().replace('=', 'EQ')
classes.append(f"hint-hide-{hint_hide_encoded}")
parent = nodes.container(classes=classes)
self.state.nested_parse(self.content, self.content_offset, parent)
return [parent]
# We connect this function to the step after the builder is initialized
def setup(app):
# Add our static path
app.connect("builder-inited", st_static_path)
# Add relevant code to headers
app.add_css_file("togglebutton.css")
# Add the string we'll use to select items in the JS
# Tell Sphinx about this configuration variable
app.add_config_value("togglebutton_selector", ".toggle, .admonition.dropdown", "html")
app.add_config_value("togglebutton_hint", "Click to show", "html")
app.add_config_value("togglebutton_hint_hide", "Click to hide", "html")
app.add_config_value("togglebutton_open_on_print", True, "html")
# Run the function after the builder is initialized
app.connect("builder-inited", insert_custom_selection_config)
app.connect("config-inited", initialize_js_assets)
app.add_directive("toggle", Toggle)
return {
"version": __version__,
"parallel_read_safe": True,
"parallel_write_safe": True,
}