ToggleDirective.py
Go to the documentation of this file.
1 # Directive that shows a toggle link between sections.
2 # Those sections cannot contain titles
3 # The first one is initially showed. Here is the syntax:
4 # .. toggle_table::
5 # :arg1: text in button 1
6 # :arg2: text in button 2
7 #
8 # .. toggle:: text in button 1
9 #
10 # some RST text
11 #
12 #.. toggle:: text in button 2
13 #
14 # some more RST text
15 import docutils
16 from docutils import nodes
17 from docutils.parsers.rst import directives, roles, states
18 
19 def setup(app):
20  app.add_directive('toggle', ToggleDirective)
21  app.add_directive('toggle_table', ToggleTableDirective)
22 
23 class ToggleDirective(docutils.parsers.rst.Directive):
24  """
25  Base class that will be used by the two toggle directives
26  """
27  required_arguments = 1
28  optional_arguments = 0
29  final_argument_whitespace = True
30  option_spec = {}
31  has_content = True
32  node_class = nodes.container
33 
34  def run(self):
35  self.assert_has_content()
36  text = '\n'.join(self.content)
37  # Create the node, to be populated by `nested_parse`.
38  node = self.node_class(rawsource=text)
39  label = self.arguments[0]
40  label_strip = label.replace(' ', '')
41  node += nodes.raw(self.arguments[0], '<div class="toggleable_div label_%s">' % label_strip, format="html")
42 
43  # Parse the directive contents.
44  self.state.nested_parse(self.content, self.content_offset, node)
45  node += nodes.raw(self.arguments[0], '</div>', format="html")
46  return [node]
47 
48 class ToggleTableDirective(docutils.parsers.rst.Directive):
49  """
50  Class used to create a set of buttons to toggle different sections
51  """
52  required_arguments = 0
53  optional_arguments = 10
54  final_argument_whitespace = True
55  option_spec = {}
56  for i in xrange(0, 100):
57  option_spec['arg' + str(i)] = str
58  has_content = True
59  node_class = nodes.container
60 
61  def run(self):
62  js_toggle = """
63 function toggle(label) {
64  $('.toggleable_button').css({border: '2px outset', 'border-radius': '4px'});
65  $('.toggleable_button.label_' + label).css({border: '2px inset', 'border-radius': '4px'});
66  $('.toggleable_div').css('display', 'none');
67  $('.toggleable_div.label_' + label).css('display', 'block');
68 };
69 """
70 
71  js_ready = """
72 <script>
73 %s
74 $(document).ready(function() {
75  var classList =$('.toggleable_button').attr('class').split(/\s+/);
76  $.each( classList, function(index, item){
77  if (item.substring(0, 5) === 'label') {
78  toggle(item.substring(6));
79  };
80  });
81 });
82 </script>
83 """ % js_toggle
84 
85  # Create the node, to be populated by `nested_parse`.
86  node = self.node_class()
87  for key in self.options.keys():
88  if key not in self.option_spec:
89  raise RuntimeError(key + ' not in the contructor of ToggleTableDirective, use arg0 to arg99')
90  label = self.options[key]
91  label_strip = label.replace(' ', '')
92  str1 = '<button class="toggleable_button label_%s" onclick="' % label_strip
93  str2 = js_toggle + "toggle('%s')" % label_strip
94  str3 = '">%s</button>' % label
95  node += nodes.raw(key, str1 + str2 + str3 + js_ready, format="html")
96 
97  return [node]


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sat Feb 15 2020 03:24:26