|
| 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 | +__author__ = "ziwu" |
| 17 | + |
| 18 | +from docutils import nodes |
| 19 | +from docutils.parsers.rst import directives |
| 20 | +from runestone.mchoice import Assessment |
| 21 | +from runestone.server.componentdb import ( |
| 22 | + addQuestionToDB, |
| 23 | + addHTMLToDB, |
| 24 | + maybeAddToAssignment, |
| 25 | +) |
| 26 | +from runestone.common.runestonedirective import RunestoneDirective, RunestoneIdNode |
| 27 | + |
| 28 | + |
| 29 | +def setup(app): |
| 30 | + app.add_directive("hparsons", HParsonsDirective) |
| 31 | + # adding the html |
| 32 | + app.add_node(HParsonsNode, html=(visit_hparsons_node, depart_hparsons_node)) |
| 33 | + # TODO: figure out what these means |
| 34 | + # app.add_config_value("shortanswer_div_class", "journal alert alert-warning", "html") |
| 35 | + # app.add_config_value( |
| 36 | + # "shortanswer_optional_div_class", "journal alert alert-success", "html" |
| 37 | + # ) |
| 38 | + |
| 39 | + |
| 40 | +# TODO: what is the alert and alert-warnings? |
| 41 | +TEMPLATE_START = """ |
| 42 | + <div style="max-width: none;"> |
| 43 | + <div data-component="hparsons" id="%(divid)s" data-question_label="%(question_label)s" class="alert alert-warning hparsons" > |
| 44 | + <div class="hparsons_question hparsons-text" > |
| 45 | + """ |
| 46 | + |
| 47 | +TEMPLATE_END = """ |
| 48 | + </div> |
| 49 | + <div class="hparsons"> |
| 50 | + </div> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + """ |
| 54 | + |
| 55 | + |
| 56 | +# seems to be the same for all |
| 57 | +class HParsonsNode(nodes.General, nodes.Element, RunestoneIdNode): |
| 58 | + def __init__(self, options, **kwargs): |
| 59 | + super(HParsonsNode, self).__init__(**kwargs) |
| 60 | + self.runestone_options = options |
| 61 | + |
| 62 | +# generate the first part of the html |
| 63 | +def visit_hparsons_node(self, node): |
| 64 | + div_id = node.runestone_options["divid"] |
| 65 | + components = dict(node.runestone_options) |
| 66 | + components.update({"divid": div_id}) |
| 67 | + node.delimiter = "_start__{}_".format(node.runestone_options["divid"]) |
| 68 | + self.body.append(node.delimiter) |
| 69 | + res = TEMPLATE_START % components |
| 70 | + self.body.append(res) |
| 71 | + |
| 72 | + |
| 73 | +# generate the second part of the html |
| 74 | +def depart_hparsons_node(self, node): |
| 75 | + components = dict(node.runestone_options) |
| 76 | + res = TEMPLATE_END % components |
| 77 | + self.body.append(res) |
| 78 | + addHTMLToDB( |
| 79 | + node.runestone_options["divid"], |
| 80 | + components["basecourse"], |
| 81 | + "".join(self.body[self.body.index(node.delimiter) + 1 :]), |
| 82 | + ) |
| 83 | + self.body.remove(node.delimiter) |
| 84 | + |
| 85 | + |
| 86 | +class HParsonsDirective(Assessment): |
| 87 | + """ |
| 88 | + .. hparsons:: uniqueid |
| 89 | +
|
| 90 | + nothing makes sense at all. |
| 91 | +
|
| 92 | + """ |
| 93 | + |
| 94 | + required_arguments = 0 # the div id |
| 95 | + optional_arguments = 0 |
| 96 | + final_argument_whitespace = True |
| 97 | + has_content = False |
| 98 | + option_spec = Assessment.option_spec.copy() |
| 99 | + # seem to be defining the type of the options |
| 100 | + # option_spec.update({"mathjax": directives.flag}) |
| 101 | + |
| 102 | + # just fill it with the name |
| 103 | + node_class = HParsonsNode |
| 104 | + |
| 105 | + def run(self): |
| 106 | + # same |
| 107 | + super(HParsonsDirective, self).run() |
| 108 | + addQuestionToDB(self) |
| 109 | + # Raise an error if the directive does not have contents. |
| 110 | + self.assert_has_content() |
| 111 | + |
| 112 | + # specifying default for option? |
| 113 | + # TODO: ignoring for now |
| 114 | + # self.options["mathjax"] = "data-mathjax" if "mathjax" in self.options else "" |
| 115 | + |
| 116 | + # same |
| 117 | + hparsons_node = HParsonsNode(self.options, rawsource=self.block_text) |
| 118 | + hparsons_node.source, hparsons_node.line = self.state_machine.get_source_and_line( |
| 119 | + self.lineno |
| 120 | + ) |
| 121 | + |
| 122 | + # exist in short answer and mchoice but not parsons |
| 123 | + # For MChoice its better to insert the qnum into the content before further processing. |
| 124 | + self.updateContent() |
| 125 | + |
| 126 | + # same as mchoice, different from parsons. i think it is for generating instructions. |
| 127 | + self.state.nested_parse(self.content, self.content_offset, hparsons_node) |
| 128 | + # parsons: |
| 129 | + # self.state.nested_parse( |
| 130 | + # self.options["instructions"], self.content_offset, parsons_node |
| 131 | + # ) |
| 132 | + |
| 133 | + # adding classes outside of the div based on the options |
| 134 | + env = self.state.document.settings.env |
| 135 | + if self.options["optional"]: |
| 136 | + self.options["divclass"] = env.config.shortanswer_optional_div_class |
| 137 | + else: |
| 138 | + self.options["divclass"] = env.config.shortanswer_div_class |
| 139 | + |
| 140 | + # same |
| 141 | + maybeAddToAssignment(self) |
| 142 | + |
| 143 | + # same |
| 144 | + return [hparsons_node] |
0 commit comments