This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
/
tabbedStuff.py
214 lines (155 loc) · 6.24 KB
/
tabbedStuff.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Copyright (C) 2011 Bradley N. Miller
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
__author__ = "isaiahmayerchak"
from docutils import nodes
from docutils.parsers.rst import directives
from runestone.common.runestonedirective import RunestoneDirective, RunestoneNode
def setup(app):
# Add directives/javascript/css
app.add_directive("tabbed", TabbedStuffDirective)
app.add_directive("tab", TabDirective)
app.add_node(TabNode, html=(visit_tab_node, depart_tab_node))
app.add_node(
TabbedStuffNode, html=(visit_tabbedstuff_node, depart_tabbedstuff_node)
)
app.add_autoversioned_javascript("tabbedstuff.js")
app.add_autoversioned_stylesheet("tabbedstuff.css")
app.add_config_value("tabbed_div_class", "alert alert-warning", "html")
# Templates to be formatted by node options
BEGIN = """<div id='%(divid)s' data-component="tabbedStuff" %(inactive)s class='%(divclass)s'>"""
TABDIV_BEGIN = """<div data-component="tab" data-tabname="%(tabname)s" %(active)s>
"""
TABDIV_END = """</div>"""
END = """
</div>
"""
class TabNode(nodes.General, nodes.Element):
def __init__(self, content, **kwargs):
super(TabNode, self).__init__(**kwargs)
self.tabnode_options = content
self.tabname = content["tabname"]
def visit_tab_node(self, node):
# Set options and format templates accordingly
divid = node.parent.divid
tabname = node.tabname
if "active" in node.tabnode_options:
node.tabnode_options["active"] = "data-active"
else:
node.tabnode_options["active"] = ""
res = TABDIV_BEGIN % {
"divid": divid,
"tabname": tabname,
"active": node.tabnode_options["active"],
}
self.body.append(res)
def depart_tab_node(self, node):
# Set options and format templates accordingly
self.body.append(TABDIV_END)
class TabbedStuffNode(nodes.General, nodes.Element, RunestoneNode):
"""A TabbedStuffNode contains one or more TabNodes"""
def __init__(self, content, **kwargs):
super(TabbedStuffNode, self).__init__(**kwargs)
self.tabbed_stuff_options = content
self.divid = content["divid"]
def visit_tabbedstuff_node(self, node):
divid = node.divid
if "inactive" in node.tabbed_stuff_options:
node.tabbed_stuff_options["inactive"] = "data-inactive"
else:
node.tabbed_stuff_options["inactive"] = ""
res = BEGIN % {
"divid": divid,
"divclass": node.tabbed_stuff_options["divclass"],
"inactive": node.tabbed_stuff_options["inactive"],
}
self.body.append(res)
def depart_tabbedstuff_node(self, node):
divid = node.divid
res = ""
# close the tab plugin div and init the Bootstrap tabs
res += END
res = res % {"divid": divid}
self.body.append(res)
class TabDirective(RunestoneDirective):
"""
.. tab:: identifier
:active: Optional flag that specifies this tab to be opened when page is loaded (default is first tab)--overridden by :inactive: flag on tabbedStuff
Content
...
config values (conf.py):
- tabbed_div_class - custom CSS class of the component's outermost div
"""
required_arguments = 1 # the name of the tab
optional_arguments = 0
final_argument_whitespace = True
has_content = True
option_spec = {"active": directives.flag}
node_class = TabNode
def run(self):
"""
process the tab directive and generate html for output.
:param self:
:return:
.. tab:: identifier
:active: Optional flag that specifies this tab to be opened when page is loaded (default is first tab)--overridden by :inactive: flag on tabbedStuff
Content
...
"""
# Raise an error if the directive does not have contents.
self.assert_has_content()
# Create the node, to be populated by "nested_parse".
self.options["tabname"] = self.arguments[0]
tab_node = TabNode(self.options, rawsource=self.block_text)
# Parse the child nodes (content of the tab)
self.state.nested_parse(self.content, self.content_offset, tab_node)
return [tab_node]
class TabbedStuffDirective(RunestoneDirective):
"""
.. tabbed:: identifier
:inactive: Optional flag that calls for no tabs to be open on page load
Content (put tabs here)
...
config values (conf.py):
- tabbed_div_class - custom CSS class of the component's outermost div
"""
required_arguments = 1 # the div to put the tabbed exhibit in
optional_arguments = 0
final_argument_whitespace = True
has_content = True
option_spec = {"inactive": directives.flag}
def run(self):
"""
process the tabbedStuff directive and generate html for output.
:param self:
:return:
.. tabbed:: identifier
:inactive: Optional flag that calls for no tabs to be open on page load
Content (put tabs here)
...
"""
# Raise an error if the directive does not have contents.
self.assert_has_content()
self.options["divid"] = self.arguments[0]
env = self.state.document.settings.env
self.options["divclass"] = env.config.tabbed_div_class
# Create the node, to be populated by "nested_parse".
tabbedstuff_node = TabbedStuffNode(self.options, rawsource=self.block_text)
tabbedstuff_node.source, tabbedstuff_node.line = self.state_machine.get_source_and_line(
self.lineno
)
# Parse the directive contents (should be 1 or more tab directives)
self.state.nested_parse(self.content, self.content_offset, tabbedstuff_node)
return [tabbedstuff_node]