Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 7e363ac

Browse files
committed
DRY - refactor common parts of node visit/depart
1 parent fd33ff6 commit 7e363ac

File tree

1 file changed

+22
-48
lines changed

1 file changed

+22
-48
lines changed

runestone/mchoice/multiplechoice.py

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ class MChoiceNode(nodes.General, nodes.Element, RunestoneIdNode):
3535
pass
3636

3737

38-
# TODO: refactor the common parts of visit_mc_xml and visit_mc_node -- copy/paste ok for
39-
# proof of concept but not long term.
40-
def visit_mc_xml(self, node):
38+
def visit_mc_common(self, node, node_type):
4139

4240
res = ""
4341
if "random" in node["runestone_options"]:
@@ -53,10 +51,10 @@ def visit_mc_xml(self, node):
5351
node["runestone_options"]["multipleAnswers"] = "false"
5452
res = node["template_start"] % node["runestone_options"]
5553

56-
self.output.append(res)
54+
return res
5755

5856

59-
def depart_mc_xml(self, node):
57+
def depart_mc_common(self, node):
6058
res = ""
6159
currFeedback = ""
6260
# Add all of the possible answers
@@ -78,53 +76,29 @@ def depart_mc_xml(self, node):
7876
res += node["template_option"] % node["runestone_options"]
7977

8078
res += node["template_end"] % node["runestone_options"]
79+
return res
80+
81+
82+
def visit_mc_xml(self, node):
83+
84+
res = visit_mc_common(self, node, "xml")
8185
self.output.append(res)
8286

8387

84-
def visit_mc_node(self, node):
88+
def depart_mc_xml(self, node):
89+
res = depart_mc_common(self, node)
90+
self.output.append(res)
91+
8592

93+
def visit_mc_html(self, node):
8694
node["delimiter"] = "_start__{}_".format(node["runestone_options"]["divid"])
8795
self.body.append(node["delimiter"])
88-
89-
res = ""
90-
if "random" in node["runestone_options"]:
91-
node["runestone_options"]["random"] = "data-random"
92-
else:
93-
node["runestone_options"]["random"] = ""
94-
# Use multiple_answers behavior if explicitly required or if multiple correct answers were provided.
95-
if ("multiple_answers" in node["runestone_options"]) or (
96-
"," in node["runestone_options"]["correct"]
97-
):
98-
node["runestone_options"]["multipleAnswers"] = "true"
99-
else:
100-
node["runestone_options"]["multipleAnswers"] = "false"
101-
res = node["template_start"] % node["runestone_options"]
102-
96+
res = visit_mc_common(self, node)
10397
self.body.append(res)
10498

10599

106-
def depart_mc_node(self, node):
107-
res = ""
108-
currFeedback = ""
109-
# Add all of the possible answers
110-
okeys = list(node["runestone_options"].keys())
111-
okeys.sort()
112-
for k in okeys:
113-
if "answer_" in k:
114-
x, label = k.split("_")
115-
node["runestone_options"]["alabel"] = label
116-
node["runestone_options"]["atext"] = node["runestone_options"][k]
117-
currFeedback = "feedback_" + label
118-
node["runestone_options"]["feedtext"] = node["runestone_options"].get(
119-
currFeedback, ""
120-
) # node["runestone_options"][currFeedback]
121-
if label in node["runestone_options"]["correct"]:
122-
node["runestone_options"]["is_correct"] = "data-correct"
123-
else:
124-
node["runestone_options"]["is_correct"] = ""
125-
res += node["template_option"] % node["runestone_options"]
126-
127-
res += node["template_end"] % node["runestone_options"]
100+
def depart_mc_html(self, node):
101+
res = depart_mc_common(self, node)
128102
self.body.append(res)
129103

130104
addHTMLToDB(
@@ -384,16 +358,16 @@ class FeedbackListItem(nodes.list_item, RunestoneNode):
384358

385359

386360
# The ``<ul>`` tag will be generated already -- don't output it.
387-
def visit_answers_bullet_node(self, node):
361+
def visit_answers_bullet_html(self, node):
388362
# Prevent the list items, which are wrapped in ``<paragraph>`` tags, from emitting the ``<p>``. See similar code in ``docutils.writers._html_base.HTMLTranslator.visit_bullet_list`` and its use in ``docutils.writer.html4css1.HTMLTranslator.visit_paragraph``.
389363
self.context.append((self.compact_simple, self.compact_p))
390364
self.compact_p = None
391365
self.compact_simple = True
392366

393367

394368
# The ``</ul>`` tag will be generated already -- don't output it.
395-
def depart_answers_bullet_node(self, node):
396-
# Restore the state modified in ``visit_answers_bullet_node``.
369+
def depart_answers_bullet_html(self, node):
370+
# Restore the state modified in ``visit_answers_bullet_html``.
397371
self.compact_simple, self.compact_p = self.context.pop()
398372

399373

@@ -424,12 +398,12 @@ def depart_answer_list_item(self, node):
424398

425399

426400
# Nothing to output, since feedback isn't nested under an answer in the HTML.
427-
def visit_feedback_bullet_node(self, node):
401+
def visit_feedback_bullet_html(self, node):
428402
pass
429403

430404

431405
# Nothing to output, since feedback isn't nested under an answer in the HTML.
432-
def depart_feedback_bullet_node(self, node):
406+
def depart_feedback_bullet_html(self, node):
433407
pass
434408

435409

0 commit comments

Comments
 (0)