|
| 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('clickablearea',ClickableArea) |
| 25 | + app.add_javascript('clickable.js') |
| 26 | + app.add_javascript('timedclickable.js') |
| 27 | + app.add_stylesheet('clickable.css') |
| 28 | + |
| 29 | + app.add_node(ClickableAreaNode, html=(visit_ca_node, depart_ca_node)) |
| 30 | + |
| 31 | + |
| 32 | +TEMPLATE = """ |
| 33 | +<div data-component="clickablearea" id="%(divid)s" %(table)s %(correct)s %(incorrect)s> |
| 34 | +<span data-question>%(question)s</span>%(feedback)s%(clickcode)s |
| 35 | +""" |
| 36 | +TEMPLATE_END = """ |
| 37 | +</div> |
| 38 | +""" |
| 39 | + |
| 40 | +class ClickableAreaNode(nodes.General, nodes.Element): |
| 41 | + def __init__(self,content): |
| 42 | + """ |
| 43 | + Arguments: |
| 44 | + - `self`: |
| 45 | + - `content`: |
| 46 | + """ |
| 47 | + super(ClickableAreaNode,self).__init__() |
| 48 | + self.ca_options = content |
| 49 | + |
| 50 | +# self for these functions is an instance of the writer class. For example |
| 51 | +# in html, self is sphinx.writers.html.SmartyPantsHTMLTranslator |
| 52 | +# The node that is passed as a parameter is an instance of our node class. |
| 53 | +def visit_ca_node(self,node): |
| 54 | + res = TEMPLATE |
| 55 | + |
| 56 | + if "feedback" in node.ca_options: |
| 57 | + node.ca_options["feedback"] = "<span data-feedback>" + node.ca_options["feedback"] + "</span>" |
| 58 | + else: |
| 59 | + node.ca_options["feedback"] = "" |
| 60 | + |
| 61 | + if "iscode" not in node.ca_options: |
| 62 | + node.ca_options["correct"] = "data-cc=" + '"' + node.ca_options["correct"] + '"' |
| 63 | + node.ca_options["incorrect"] = "data-ci=" + '"' + node.ca_options["incorrect"] + '"' |
| 64 | + else: |
| 65 | + node.ca_options["correct"] = "" |
| 66 | + node.ca_options["incorrect"] = "" |
| 67 | + |
| 68 | + res = res % node.ca_options |
| 69 | + |
| 70 | + self.body.append(res) |
| 71 | + |
| 72 | +def depart_ca_node(self,node): |
| 73 | + res = "" |
| 74 | + res = TEMPLATE_END % node.ca_options |
| 75 | + self.body.append(res) |
| 76 | + |
| 77 | + |
| 78 | +class ClickableArea(Directive): |
| 79 | + required_arguments = 1 |
| 80 | + optional_arguments = 0 |
| 81 | + has_content = True |
| 82 | + final_argument_whitespace = True |
| 83 | + option_spec = {"question":directives.unchanged, |
| 84 | + "feedback":directives.unchanged, |
| 85 | + "iscode":directives.flag, |
| 86 | + "correct":directives.unchanged, |
| 87 | + "incorrect":directives.unchanged, |
| 88 | + "table":directives.flag |
| 89 | + } |
| 90 | + |
| 91 | + def run(self): |
| 92 | + """ |
| 93 | + process the clickablearea directive and generate html for output. |
| 94 | + :param self: |
| 95 | + :return: |
| 96 | + .. clickablearea:: identifier |
| 97 | + :question: Question text |
| 98 | + :feedback: Optional feedback for incorrect answer |
| 99 | + :iscode: Boolean that if present will put the content into a <pre> |
| 100 | + :table: Boolean that indicates that the content is a table. |
| 101 | + :correct: An array of the indices of the correct elements, separated by semicolons--if this is a table, each item in the array is a tuple |
| 102 | + with the first number being the row and the second number being the column--use a column number of 0 to make the whole row correct (ex: 1,2;3,0 makes the 2nd data cell in the first row correct as well as the entire 3rd row) |
| 103 | + :incorrect: An array of the indices of the incorrect elements--same format as the correct elements. |
| 104 | + --Content-- |
| 105 | + """ |
| 106 | + self.assert_has_content() |
| 107 | + self.options['divid'] = self.arguments[0] |
| 108 | + if "iscode" in self.options: |
| 109 | + source = "\n".join(self.content) |
| 110 | + source = source.replace(":click-correct:", "<span data-correct>") |
| 111 | + source = source.replace(":click-incorrect:", "<span data-incorrect>") |
| 112 | + source = source.replace(":endclick:", "</span>") |
| 113 | + source = "<pre>" + source + "</pre>" |
| 114 | + self.options['clickcode'] = source |
| 115 | + else: |
| 116 | + self.options['clickcode'] = '' |
| 117 | + clickNode = ClickableAreaNode(self.options) |
| 118 | + clickNode.template_start = TEMPLATE |
| 119 | + |
| 120 | + if "table" in self.options: |
| 121 | + self.options["table"] = "data-table" |
| 122 | + else: |
| 123 | + self.options["table"] = "" |
| 124 | + |
| 125 | + if "iscode" not in self.options: |
| 126 | + self.state.nested_parse(self.content, self.content_offset, clickNode) |
| 127 | + |
| 128 | + return [clickNode] |
0 commit comments