|
| 1 | + |
| 2 | +# ********* |
| 3 | +# |docname| |
| 4 | +# ********* |
| 5 | +# Copyright (C) 2011 Bradley N. Miller |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +# |
| 20 | +__author__ = "jaltekruse" |
| 21 | + |
| 22 | +from docutils import nodes |
| 23 | +from docutils.parsers.rst import directives |
| 24 | +from sqlalchemy import Table |
| 25 | +from runestone.server.componentdb import ( |
| 26 | + addQuestionToDB, |
| 27 | + addHTMLToDB, |
| 28 | + maybeAddToAssignment, |
| 29 | +) |
| 30 | +from runestone.common.runestonedirective import ( |
| 31 | + RunestoneIdDirective, |
| 32 | + RunestoneIdNode, |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +def setup(app): |
| 37 | + app.add_directive("doenet", DoenetDirective) |
| 38 | + app.add_node(DoenetNode, html=(visit_hp_html, depart_hp_html)) |
| 39 | + |
| 40 | + |
| 41 | +TEMPLATE_START = """ |
| 42 | +<div class="runestone"> |
| 43 | +<div data-component="hparsons" id=%(divid)s data-question_label="%(question_label)s" class="alert alert-warning hparsons_section"> |
| 44 | +<div class="hp_question"> |
| 45 | +""" |
| 46 | + |
| 47 | +TEMPLATE_END = """ |
| 48 | +</div> |
| 49 | +<div class='hparsons'></div> |
| 50 | +<textarea |
| 51 | + %(language)s |
| 52 | + %(optional)s |
| 53 | + %(dburl)s |
| 54 | + %(reuse)s |
| 55 | + %(randomize)s |
| 56 | + %(blockanswer)s |
| 57 | + style="visibility: hidden;"> |
| 58 | +%(initialsetting)s |
| 59 | +</textarea> |
| 60 | +</div> |
| 61 | +</div> |
| 62 | +""" |
| 63 | + |
| 64 | + |
| 65 | +class DoenetNode(nodes.General, nodes.Element, RunestoneIdNode): |
| 66 | + pass |
| 67 | + |
| 68 | + |
| 69 | +# self for these functions is an instance of the writer class. For example |
| 70 | +# in html, self is sphinx.writers.html.SmartyPantsHTMLTranslator |
| 71 | +# The node that is passed as a parameter is an instance of our node class. |
| 72 | +def visit_hp_html(self, node): |
| 73 | + |
| 74 | + node["delimiter"] = "_start__{}_".format(node["runestone_options"]["divid"]) |
| 75 | + |
| 76 | + self.body.append(node["delimiter"]) |
| 77 | + |
| 78 | + res = TEMPLATE_START % node["runestone_options"] |
| 79 | + self.body.append(res) |
| 80 | + |
| 81 | + |
| 82 | +def depart_hp_html(self, node): |
| 83 | + res = TEMPLATE_END % node["runestone_options"] |
| 84 | + self.body.append(res) |
| 85 | + |
| 86 | + addHTMLToDB( |
| 87 | + node["runestone_options"]["divid"], |
| 88 | + node["runestone_options"]["basecourse"], |
| 89 | + "".join(self.body[self.body.index(node["delimiter"]) + 1 :]), |
| 90 | + ) |
| 91 | + |
| 92 | + self.body.remove(node["delimiter"]) |
| 93 | + |
| 94 | + |
| 95 | +class DoenetDirective(RunestoneIdDirective): |
| 96 | + """ |
| 97 | + <!-- .. doenet:: doenet-1 |
| 98 | + --> |
| 99 | + 1+3000=<answer>4</answer> |
| 100 | + """ |
| 101 | + |
| 102 | + required_arguments = 1 |
| 103 | + optional_arguments = 1 |
| 104 | + has_content = True |
| 105 | + option_spec = RunestoneIdDirective.option_spec.copy() |
| 106 | + option_spec.update( |
| 107 | + { |
| 108 | + "dburl": directives.unchanged, |
| 109 | + "language": directives.unchanged, |
| 110 | + "reuse": directives.flag, |
| 111 | + "randomize": directives.flag, |
| 112 | + "blockanswer": directives.unchanged, |
| 113 | + } |
| 114 | + ) |
| 115 | + |
| 116 | + def run(self): |
| 117 | + super(DoenetDirective, self).run() |
| 118 | + addQuestionToDB(self) |
| 119 | + |
| 120 | + env = self.state.document.settings.env |
| 121 | + |
| 122 | + if "language" in self.options: |
| 123 | + self.options["language"] = "data-language='{}'".format( |
| 124 | + self.options["language"] |
| 125 | + ) |
| 126 | + else: |
| 127 | + self.options["language"] = "" |
| 128 | + |
| 129 | + if "reuse" in self.options: |
| 130 | + self.options["reuse"] = ' data-reuse="true"' |
| 131 | + else: |
| 132 | + self.options["reuse"] = "" |
| 133 | + |
| 134 | + if "randomize" in self.options: |
| 135 | + self.options["randomize"] = ' data-randomize="true"' |
| 136 | + else: |
| 137 | + self.options["randomize"] = "" |
| 138 | + |
| 139 | + if "blockanswer" in self.options: |
| 140 | + self.options["blockanswer"] = "data-blockanswer='{}'".format( |
| 141 | + self.options["blockanswer"] |
| 142 | + ) |
| 143 | + else: |
| 144 | + self.options["blockanswer"] = "" |
| 145 | + |
| 146 | + explain_text = None |
| 147 | + if self.content: |
| 148 | + if "~~~~" in self.content: |
| 149 | + idx = self.content.index("~~~~") |
| 150 | + explain_text = self.content[:idx] |
| 151 | + self.content = self.content[idx + 1 :] |
| 152 | + source = "\n".join(self.content) |
| 153 | + else: |
| 154 | + source = "\n" |
| 155 | + |
| 156 | + self.explain_text = explain_text or ["Not an Exercise"] |
| 157 | + |
| 158 | + self.options["initialsetting"] = source |
| 159 | + |
| 160 | + # SQL Options |
| 161 | + if "dburl" in self.options: |
| 162 | + self.options["dburl"] = "data-dburl='{}'".format(self.options["dburl"]) |
| 163 | + else: |
| 164 | + self.options["dburl"] = "" |
| 165 | + |
| 166 | + course_name = env.config.html_context["course_id"] |
| 167 | + divid = self.options["divid"] |
| 168 | + |
| 169 | + hpnode = DoenetNode() |
| 170 | + hpnode["runestone_options"] = self.options |
| 171 | + hpnode["source"], hpnode["line"] = self.state_machine.get_source_and_line( |
| 172 | + self.lineno |
| 173 | + ) |
| 174 | + self.add_name(hpnode) # make this divid available as a target for :ref: |
| 175 | + |
| 176 | + maybeAddToAssignment(self) |
| 177 | + if explain_text: |
| 178 | + self.updateContent() |
| 179 | + self.state.nested_parse(explain_text, self.content_offset, hpnode) |
| 180 | + |
| 181 | + return [hpnode] |
0 commit comments