From 32b4407f701acff2a10628deb5a09cda738dfdda Mon Sep 17 00:00:00 2001 From: 3ter Date: Sat, 29 Oct 2022 21:27:16 +0200 Subject: [PATCH] Replace string literals on template with file read This fixes a bug introduced by a mismatch between the string literals on template.py and back.html. --- {card => src/multiple_choice/card}/back.html | 11 +- {card => src/multiple_choice/card}/css.css | 0 {card => src/multiple_choice/card}/front.html | 8 +- src/multiple_choice/template.py | 527 +----------------- 4 files changed, 34 insertions(+), 512 deletions(-) rename {card => src/multiple_choice/card}/back.html (97%) rename {card => src/multiple_choice/card}/css.css (100%) rename {card => src/multiple_choice/card}/front.html (97%) diff --git a/card/back.html b/src/multiple_choice/card/back.html similarity index 97% rename from card/back.html rename to src/multiple_choice/card/back.html index 5918bdc..a24b036 100644 --- a/card/back.html +++ b/src/multiple_choice/card/back.html @@ -47,12 +47,9 @@ // Check if Persistence is recognized to prevent errors when viewing note in "Manage Note Types..." if (Persistence.isAvailable && Persistence.getItem('Q_solutions') !== null) { // Parsing solutions - console.log(Persistence.getItem('Q_solutions')); - var solutions = Persistence.getItem('Q_solutions').split(" "); - for (let i = 0; i < solutions.length; i++) { - solutions[i] = Number(solutions[i]); - } + var solutions = Persistence.getItem('Q_solutions'); var answers = Persistence.getItem('user_answers'); + var type = document.getElementById('CardType').innerHTML; var qtable = document.getElementById('qtable'); qtable.innerHTML = Persistence.getItem('qtable'); @@ -78,10 +75,10 @@ if (solutions[i] && answers[i] === 1) { qrows[(type != 0) ? i : i + 1].setAttribute("class", options.qtable.colors.correctcorrect); } else if (!solutions[i] && answers[i] === 0) { - qrows[(type != 0) ? i : i + 1].setAttribute("class", options.qtable.colors.wrongwrong); + qrows[(type != 0) ? i : i + 1].setAttribute("class", options.qtable.colors.wrongwrong); } else if (!solutions[i] && answers[i] === 1) { qrows[(type != 0) ? i : i + 1].setAttribute("class", options.qtable.colors.wrongcorrect); - } else if (solutions[i] && answers[i] === 0){ + } else if (solutions[i] && answers[i] === 0) { qrows[(type != 0) ? i : i + 1].setAttribute("class", options.qtable.colors.correctwrong); } else if (type == 0 && answers[i] === 2) { qrows[(type != 0) ? i : i + 1].setAttribute("class", options.qtable.colors.eithernone); diff --git a/card/css.css b/src/multiple_choice/card/css.css similarity index 100% rename from card/css.css rename to src/multiple_choice/card/css.css diff --git a/card/front.html b/src/multiple_choice/card/front.html similarity index 97% rename from card/front.html rename to src/multiple_choice/card/front.html index 491a5c6..183c3d0 100644 --- a/card/front.html +++ b/src/multiple_choice/card/front.html @@ -164,6 +164,12 @@ return userAnswers } + function getCorrectAnswers() { + let solutions = document.getElementById("Q_solutions").innerHTML.split(" ").map(string => Number(string)); + + return solutions; + } + /** * On checking an option this collects and stores answers in between front/back of the card. * @@ -175,7 +181,7 @@ if (Persistence.isAvailable()) { Persistence.clear(); Persistence.setItem('user_answers', getUserAnswers()); - Persistence.setItem('Q_solutions', document.getElementById("Q_solutions").innerHTML); + Persistence.setItem('Q_solutions', getCorrectAnswers()); Persistence.setItem('qtable', document.getElementById("qtable").innerHTML); } } diff --git a/src/multiple_choice/template.py b/src/multiple_choice/template.py index 2923e5e..3ce0d88 100644 --- a/src/multiple_choice/template.py +++ b/src/multiple_choice/template.py @@ -36,507 +36,12 @@ Manages note type and templates """ -from __future__ import (absolute_import, division, - print_function, unicode_literals) +from __future__ import (absolute_import, division, print_function, + unicode_literals) from anki.consts import MODEL_STD - from aqt import mw -card_front = """\ - - -{{#Title}}

{{Title}}

{{/Title}} -{{#Question}}

{{Question}}

{{/Question}} - -
-
-
- - - - - - - - - - -\ -""" - -card_back = """\ - - -{{#Title}}

{{Title}}

{{/Title}} -{{#Question}}

{{Question}}

{{/Question}} -
-

- - - -

Correct answers: x %

-{{#Sources}}

Sources:
{{Sources}}

{{/Sources}} -{{#Extra 1}}

Extra 1:
{{Extra 1}}

{{/Extra 1}} - -\ -""" - -card_css = """\ -.card { - font-family: arial; - font-size: 20px; - text-align: center; - color: black; - background-color: white; -} - -.small { - font-size: 15px; -} - -table, td, th { - border-collapse: collapse; - padding: 5px; -} - -table { - display: inline-block; - text-align: left; -} - -label { - margin-left: 0.4em; -} - -.correct { - background-color: lime; -} - -.nightMode .correct { - background-color: #009900; -} - -.wrong { - background-color: OrangeRed; -} - -.hidden { - /* - This block is from Glutanimate's Cloze Overlapper card template. - The Cloze Overlapper card template is licensed under the CC BY-SA 4.0 - license (https://creativecommons.org/licenses/by-sa/4.0/). - */ - /* guarantees a consistent width across front and back */ - font-weight: bold; - display: block; - line-height: 0; - height: 0; - overflow: hidden; - visibility: hidden; -}\ -""" - aio_model = "AllInOne (kprim, mc, sc)" aio_card = "AllInOne (kprim, mc, sc)" aio_fields = { @@ -553,6 +58,19 @@ "extra": "Extra 1" } + +def fillTemplateAndModelFromFile(template, model): + addonFolderName = mw.addonManager.addonFromModule(__name__) + addonPath = mw.addonManager.addonsFolder() + '/' + addonFolderName + '/' + + with open(addonPath + 'card/front.html', encoding="utf-8") as f: + template['qfmt'] = f.read() + with open(addonPath + 'card/back.html', encoding="utf-8") as f: + template['afmt'] = f.read() + with open(addonPath + 'card/css.css', encoding="utf-8") as f: + model['css'] = f.read() + + def addModel(col): """Add add-on note type to collection""" models = col.models @@ -564,21 +82,22 @@ def addModel(col): models.addField(model, fld) # Add template template = models.newTemplate(aio_card) - template['qfmt'] = card_front - template['afmt'] = card_back - model['css'] = card_css - model['sortf'] = 0 # set sortfield to question + + fillTemplateAndModelFromFile(template, model) + + model['sortf'] = 0 # set sortfield to question models.addTemplate(model, template) models.add(model) return model + def updateTemplate(col): """Update add-on note templates""" print(f"Updating {aio_model} note template") model = col.models.by_name(aio_model) template = model['tmpls'][0] - template['qfmt'] = card_front - template['afmt'] = card_back - model['css'] = card_css + + fillTemplateAndModelFromFile(template, model) + col.models.save(model) return model