From 670ae611c43e87661762e93c481ba39316ebe778 Mon Sep 17 00:00:00 2001 From: Julien Chol <62134861+chel-ou@users.noreply.github.com> Date: Tue, 11 Mar 2025 14:07:32 +0100 Subject: [PATCH 1/3] Add hooks associated to compare answer feature --- qt/aqt/reviewer.py | 13 +++++++++++-- qt/tools/genhooks_gui.py | 42 +++++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/qt/aqt/reviewer.py b/qt/aqt/reviewer.py index 1a868277322..bc95d594e6e 100644 --- a/qt/aqt/reviewer.py +++ b/qt/aqt/reviewer.py @@ -753,9 +753,18 @@ def typeAnsAnswerFilter(self, buf: str) -> str: origSize = len(buf) buf = buf.replace("
", "") hadHR = len(buf) != origSize - expected = self.typeCorrect - provided = self.typedAnswer + initial_expected = self.typeCorrect + initial_provided = self.typedAnswer + expected, provided = gui_hooks.reviewer_will_compare_answer( + (initial_expected, initial_provided) + ) + output = self.mw.col.compare_answer(expected, provided, self._combining) + output = gui_hooks.reviewer_will_render_compared_answer( + output, + initial_expected, + initial_provided, + ) # and update the type answer area def repl(match: Match) -> str: diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index d0136fa3dbb..b4f1f64c743 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -100,6 +100,38 @@ def custom_link_handler(url): legacy_hook="showQuestion", legacy_no_args=True, ), + Hook( + name="reviewer_will_compare_answer", + args=["expected_provided_tuple: tuple[str, str]"], + return_type="tuple[str, str]", + doc="""Modify expected answer and provided answer before comparing + + expected_provided_tuple is a tuple composed of: + - expected answer + - provided answer + + Return a tuple composed of: + - modified expected answer + - modified provided answer + """, + ), + Hook( + name="reviewer_will_render_compared_answer", + args=[ + "output: str", + "initial_expected: str", + "initial_provided: str", + ], + return_type="str", + doc="""Modify the output of default compare answer feature + + output is the result of default compare answer function + initial_expected is the expected answer from the card + initial_provided is the answer provided during review + + Return a string comparing expected and provided answers + """, + ), Hook( name="reviewer_did_show_answer", args=["card: Card"], @@ -846,7 +878,7 @@ def on_top_toolbar_did_init_links(links, toolbar): 'content' is a list of HTML strings added by add-ons which you can append your own components or elements to. To equip your components with logic and styling please see `webview_will_set_content` and `webview_did_receive_js_message`. - + Please note that Anki's main screen is due to undergo a significant refactor in the future and, as a result, add-ons subscribing to this hook will likely require changes to continue working. @@ -861,7 +893,7 @@ def on_top_toolbar_did_init_links(links, toolbar): 'content' is a list of HTML strings added by add-ons which you can append your own components or elements to. To equip your components with logic and styling please see `webview_will_set_content` and `webview_did_receive_js_message`. - + Please note that Anki's main screen is due to undergo a significant refactor in the future and, as a result, add-ons subscribing to this hook will likely require changes to continue working. @@ -1161,7 +1193,7 @@ def might_reject_empty_tag(optional_problems, note): args=["editor: aqt.editor.Editor", "path_or_nid: str | anki.notes.NoteId"], doc="""Called when the image occlusion mask editor has completed loading an image. - + When adding new notes `path_or_nid` will be the path to the image file. When editing existing notes `path_or_nid` will be the note id.""", ), @@ -1250,7 +1282,7 @@ def might_reject_empty_tag(optional_problems, note): name="addon_manager_will_install_addon", args=["manager: aqt.addons.AddonManager", "module: str"], doc="""Called before installing or updating an addon. - + Can be used to release DB connections or open files that would prevent an update from succeeding.""", ), @@ -1258,7 +1290,7 @@ def might_reject_empty_tag(optional_problems, note): name="addon_manager_did_install_addon", args=["manager: aqt.addons.AddonManager", "module: str"], doc="""Called after installing or updating an addon. - + Can be used to restore DB connections or open files after an add-on has been updated.""", ), From 6b2549a722b9849aa9cd28b3322b1097069c1d5b Mon Sep 17 00:00:00 2001 From: Julien Chol <62134861+chel-ou@users.noreply.github.com> Date: Tue, 11 Mar 2025 10:43:11 +0100 Subject: [PATCH 2/3] Update CONTRIBUTORS --- CONTRIBUTORS | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 80195b050e7..5782e230b0a 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -217,6 +217,7 @@ Mumtaz Hajjo Alrifai Thomas Graves Jakub Fidler Valerie Enfys +Julien Chol ******************** From 60d652b18bfa3ad1a5915b493f98c33b49c08833 Mon Sep 17 00:00:00 2001 From: Julien Chol <62134861+chel-ou@users.noreply.github.com> Date: Tue, 11 Mar 2025 12:12:46 +0100 Subject: [PATCH 3/3] Add type pattern to compare answer related hooks --- qt/aqt/reviewer.py | 5 ++++- qt/tools/genhooks_gui.py | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/qt/aqt/reviewer.py b/qt/aqt/reviewer.py index bc95d594e6e..da4b37985fe 100644 --- a/qt/aqt/reviewer.py +++ b/qt/aqt/reviewer.py @@ -749,6 +749,8 @@ def typeAnsQuestionFilter(self, buf: str) -> str: def typeAnsAnswerFilter(self, buf: str) -> str: if not self.typeCorrect: return re.sub(self.typeAnsPat, "", buf) + m = re.search(self.typeAnsPat, buf) + type_pattern = m.group(1) if m else "" orig = buf origSize = len(buf) buf = buf.replace("
", "") @@ -756,7 +758,7 @@ def typeAnsAnswerFilter(self, buf: str) -> str: initial_expected = self.typeCorrect initial_provided = self.typedAnswer expected, provided = gui_hooks.reviewer_will_compare_answer( - (initial_expected, initial_provided) + (initial_expected, initial_provided), type_pattern ) output = self.mw.col.compare_answer(expected, provided, self._combining) @@ -764,6 +766,7 @@ def typeAnsAnswerFilter(self, buf: str) -> str: output, initial_expected, initial_provided, + type_pattern, ) # and update the type answer area diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index b4f1f64c743..1acfab1f95c 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -102,13 +102,17 @@ def custom_link_handler(url): ), Hook( name="reviewer_will_compare_answer", - args=["expected_provided_tuple: tuple[str, str]"], + args=[ + "expected_provided_tuple: tuple[str, str]", + "type_pattern: str", + ], return_type="tuple[str, str]", doc="""Modify expected answer and provided answer before comparing expected_provided_tuple is a tuple composed of: - expected answer - provided answer + type_pattern is the detail of the type tag on the card Return a tuple composed of: - modified expected answer @@ -121,6 +125,7 @@ def custom_link_handler(url): "output: str", "initial_expected: str", "initial_provided: str", + "type_pattern: str", ], return_type="str", doc="""Modify the output of default compare answer feature @@ -128,6 +133,7 @@ def custom_link_handler(url): output is the result of default compare answer function initial_expected is the expected answer from the card initial_provided is the answer provided during review + type_pattern is the detail of the type tag on the card Return a string comparing expected and provided answers """,