Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 import docutils
00016 from docutils import nodes
00017 from docutils.parsers.rst import directives, roles, states
00018
00019 def setup(app):
00020 app.add_directive('toggle', ToggleDirective)
00021 app.add_directive('toggle_table', ToggleTableDirective)
00022
00023 class ToggleDirective(docutils.parsers.rst.Directive):
00024 """
00025 Base class that will be used by the two toggle directives
00026 """
00027 required_arguments = 1
00028 optional_arguments = 0
00029 final_argument_whitespace = True
00030 option_spec = {}
00031 has_content = True
00032 node_class = nodes.container
00033
00034 def run(self):
00035 self.assert_has_content()
00036 text = '\n'.join(self.content)
00037
00038 node = self.node_class(rawsource=text)
00039 label = self.arguments[0]
00040 label_strip = label.replace(' ', '')
00041 node += nodes.raw(self.arguments[0], '<div class="toggleable_div label_%s">' % label_strip, format="html")
00042
00043
00044 self.state.nested_parse(self.content, self.content_offset, node)
00045 node += nodes.raw(self.arguments[0], '</div>', format="html")
00046 return [node]
00047
00048 class ToggleTableDirective(docutils.parsers.rst.Directive):
00049 """
00050 Class used to create a set of buttons to toggle different sections
00051 """
00052 required_arguments = 0
00053 optional_arguments = 10
00054 final_argument_whitespace = True
00055 option_spec = {}
00056 for i in xrange(0, 100):
00057 option_spec['arg' + str(i)] = str
00058 has_content = True
00059 node_class = nodes.container
00060
00061 def run(self):
00062 js_toggle = """
00063 function toggle(label) {
00064 $('.toggleable_button').css({border: '2px outset', 'border-radius': '4px'});
00065 $('.toggleable_button.label_' + label).css({border: '2px inset', 'border-radius': '4px'});
00066 $('.toggleable_div').css('display', 'none');
00067 $('.toggleable_div.label_' + label).css('display', 'block');
00068 };
00069 """
00070
00071 js_ready = """
00072 <script>
00073 %s
00074 $(document).ready(function() {
00075 var classList =$('.toggleable_button').attr('class').split(/\s+/);
00076 $.each( classList, function(index, item){
00077 if (item.substring(0, 5) === 'label') {
00078 toggle(item.substring(6));
00079 };
00080 });
00081 });
00082 </script>
00083 """ % js_toggle
00084
00085
00086 node = self.node_class()
00087 for key in self.options.keys():
00088 if key not in self.option_spec:
00089 raise RuntimeError(key + ' not in the contructor of ToggleTableDirective, use arg0 to arg99')
00090 label = self.options[key]
00091 label_strip = label.replace(' ', '')
00092 str1 = '<button class="toggleable_button label_%s" onclick="' % label_strip
00093 str2 = js_toggle + "toggle('%s')" % label_strip
00094 str3 = '">%s</button>' % label
00095 node += nodes.raw(key, str1 + str2 + str3 + js_ready, format="html")
00096
00097 return [node]