|
| 1 | +# Copyright (C) 2011 Bradley N. Miller |
| 2 | +# |
| 3 | +# This program is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License |
| 14 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | +# |
| 16 | + |
| 17 | +__author__ = 'isaiahmayerchak' |
| 18 | + |
| 19 | +from docutils import nodes |
| 20 | +from docutils.parsers.rst import directives |
| 21 | +from docutils.parsers.rst import Directive |
| 22 | + |
| 23 | +def setup(app): |
| 24 | + app.add_directive('dragndrop',DragNDrop) |
| 25 | + app.add_javascript('dragndrop.js') |
| 26 | + app.add_stylesheet('dragndrop.css') |
| 27 | + |
| 28 | + app.add_node(DragNDropNode, html=(visit_dnd_node, depart_dnd_node)) |
| 29 | + |
| 30 | + |
| 31 | +TEMPLATE_START = """<ul data-component="dragndrop" id="%(divid)s"> |
| 32 | + <span data-component="question">%(question)s</span> |
| 33 | + %(feedback)s |
| 34 | +""" |
| 35 | + |
| 36 | +TEMPLATE_OPTION = """ |
| 37 | + <li data-component="draggable" id="%(divid)s_drag%(dnd_label)s">%(dragText)s</li> |
| 38 | + <li data-component="dropzone" for="%(divid)s_drag%(dnd_label)s">%(dropText)s</li> |
| 39 | +""" |
| 40 | +TEMPLATE_END = """</ul>""" |
| 41 | + |
| 42 | + |
| 43 | +class DragNDropNode(nodes.General, nodes.Element): |
| 44 | + def __init__(self,content): |
| 45 | + """ |
| 46 | + Arguments: |
| 47 | + - `self`: |
| 48 | + - `content`: |
| 49 | + """ |
| 50 | + super(DragNDropNode,self).__init__() |
| 51 | + self.dnd_options = content |
| 52 | + |
| 53 | +# self for these functions is an instance of the writer class. For example |
| 54 | +# in html, self is sphinx.writers.html.SmartyPantsHTMLTranslator |
| 55 | +# The node that is passed as a parameter is an instance of our node class. |
| 56 | +def visit_dnd_node(self,node): |
| 57 | + res = TEMPLATE_START |
| 58 | + |
| 59 | + if "feedback" in node.dnd_options: |
| 60 | + node.dnd_options["feedback"] = "<span data-component=feedback>" + node.dnd_options["feedback"] + "</span>" |
| 61 | + else: |
| 62 | + node.dnd_options["feedback"] = "" |
| 63 | + |
| 64 | + res = res % node.dnd_options |
| 65 | + |
| 66 | + self.body.append(res) |
| 67 | + |
| 68 | +def depart_dnd_node(self,node): |
| 69 | + res = "" |
| 70 | + # Add all of the possible answers |
| 71 | + okeys = list(node.dnd_options.keys()) |
| 72 | + okeys.sort() |
| 73 | + print(okeys) |
| 74 | + for k in okeys: |
| 75 | + if 'match' in k: |
| 76 | + x,label = k.split('_') |
| 77 | + node.dnd_options['dnd_label'] = label |
| 78 | + dragE, dropE = node.dnd_options[k].split("|||") |
| 79 | + node.dnd_options["dragText"] = dragE |
| 80 | + node.dnd_options['dropText'] = dropE |
| 81 | + res += node.template_option % node.dnd_options |
| 82 | + res += node.template_end % node.dnd_options |
| 83 | + self.body.append(res) |
| 84 | + |
| 85 | + |
| 86 | +class DragNDrop(Directive): |
| 87 | + required_arguments = 1 |
| 88 | + optional_arguments = 0 |
| 89 | + has_content = True |
| 90 | + option_spec = {"feedback":directives.unchanged, |
| 91 | + "match_1":directives.unchanged, |
| 92 | + "match_2":directives.unchanged, |
| 93 | + "match_3":directives.unchanged, |
| 94 | + "match_4":directives.unchanged, |
| 95 | + "match_5":directives.unchanged, |
| 96 | + "match_6":directives.unchanged, |
| 97 | + "match_7":directives.unchanged, |
| 98 | + "match_8":directives.unchanged, |
| 99 | + "match_9":directives.unchanged, |
| 100 | + "match_10":directives.unchanged, |
| 101 | + "match_11":directives.unchanged, |
| 102 | + "match_12":directives.unchanged, |
| 103 | + "match_13":directives.unchanged, |
| 104 | + "match_14":directives.unchanged, |
| 105 | + "match_15":directives.unchanged, |
| 106 | + "match_16":directives.unchanged, |
| 107 | + "match_17":directives.unchanged, |
| 108 | + "match_18":directives.unchanged, |
| 109 | + "match_19":directives.unchanged, |
| 110 | + "match_20":directives.unchanged, |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + def run(self): |
| 115 | + """ |
| 116 | + process the multiplechoice directive and generate html for output. |
| 117 | + :param self: |
| 118 | + :return: |
| 119 | + .. dragndrop:: identifier |
| 120 | + :feedback: Feedback that is displayed if things are incorrectly matched--is optional |
| 121 | + :match_1: Draggable element text|||Dropzone to be matched with text |
| 122 | + :match_2: Drag to Answer B|||Answer B |
| 123 | + :match_3: Draggable text|||Text of dropzone |
| 124 | + ...etc...(up to 20 matches) |
| 125 | +
|
| 126 | + The question goes here. |
| 127 | + """ |
| 128 | + self.options['divid'] = self.arguments[0] |
| 129 | + |
| 130 | + if self.content: |
| 131 | + source = "\n".join(self.content) |
| 132 | + else: |
| 133 | + source = '\n' |
| 134 | + |
| 135 | + self.options['question'] = source |
| 136 | + |
| 137 | + |
| 138 | + dndNode = DragNDropNode(self.options) |
| 139 | + dndNode.template_start = TEMPLATE_START |
| 140 | + dndNode.template_option = TEMPLATE_OPTION |
| 141 | + dndNode.template_end = TEMPLATE_END |
| 142 | + |
| 143 | + return [dndNode] |
0 commit comments