Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@chel-ou
Copy link
Contributor

@chel-ou chel-ou commented Mar 11, 2025

Following the suggestion made on the forum Hook for Collection.compare_answer and welcomed by @dae, this PR creates filter hooks associated with the compare answer feature.

The use case has been discussed in issue #40 of answerset add-on.

Two filter hooks would be created with different scopes:

  • reviewer_will_compare_answer(expected_provided_tuple: tuple[str, str]), updates expected and provided. This would enable simple modifications of the answers, while letting anki process the comparison. Examples use cases could be: reformatting the provided answer, shuffling the order of the expected answers...
  • reviewer_will_render_compared_answer(output: str, expected_initial: str, provided_initial: str), enables an update or a complete rewrite of the default compare_answer.

While answerset will mainly use reviewer_will_render_compared_answer, reviewer_will_compare_answer will enable more simple use cases.

As these new hooks would enable add-ons to provide changes to the compare_answer feature across all cards, I feel that this could also let the add-ons enabled only for specific cards, for example by having type patterns like type:UseMyAddonFieldName or type:MyAddon:FieldName. The former would just require users to rename their fields, while the latter is not possible right now because MyAddon would be filtered out.

As this may be a less critical feature, I added type_pattern to the hooks as an "optional" commit.

@user1823
Copy link
Contributor

This looks interesting. Is it possible to update this PR to also support modifying the behaviour of the compare_answer feature using the card templates so that it would also work on mobile?

@chel-ou
Copy link
Contributor Author

chel-ou commented Mar 11, 2025

@user1823 thanks for your comment. Could you please elaborate what you mean? When you talk about mobile, which client are you referring to?

@user1823
Copy link
Contributor

It looks like you are using an add-on to modify the answers compared by Anki. But add-ons don't work on mobile. So, is it possible to use JS in a card template to achieve the same?

When you talk about mobile, which client are you referring to?

Card templates should work on any client, whether it is Anki Desktop, AnkiMobile, AnkiWeb or AnkiDroid.

@chel-ou
Copy link
Contributor Author

chel-ou commented Mar 11, 2025

You are correct, this PR will only work for addons.

It is already possible to modify the output of compare_answer, aka the <code id=typeans>...</code> block, in javascript within a template.

The original need comes from an existing addon written in Python. So it would be possible to rewrite it in Javascript, but this is outside of the scope of this PR.

That’s why I do not really understand how modifying the compared answer within a card template is related to this PR. Any additional pointer is welcome.

@user1823
Copy link
Contributor

It is already possible to modify the output of compare_answer, aka the ... block, in javascript within a template.

Yeah, one can modify the <code id=typeans>...</code> block with JS. However, in that case, one would have to re-implement the self.mw.col.compare_answer function in JS, which would be very difficult.

If there is a way to allow JS to alter the expected and provided before they are fed into the self.mw.col.compare_answer function (just like what the add-on would be doing), it would be great from a template designer's perspective.

That’s why I do not really understand how modifying the compared answer within a card template is related to this PR.

You are introducing a feature but users who review on mobile won't be able to use it. So, I am suggesting that we provide a way for mobile users to benefit from this too. But after reviewing the code, I now think that implementing it in that way will need a drastically different approach. So, that deserves another PR (by you or someone else).

My request would probably be easier to implement after

@chel-ou
Copy link
Contributor Author

chel-ou commented Mar 11, 2025

Thanks @user1823 for this detailed explanation.
After your penultimate comment I had a look at how expected and provided are fed to compare_answer in the javascript part and in AnkiDroid. The issue is that most of the logic before feeding compare_answer is not in the javascript part.
I agree that this cross-platform add-on system will help, whenever available.
Anyhow, I really appreciate your recommendations.

@dae
Copy link
Member

dae commented Mar 14, 2025

A JS-based solution is not practical at this time, but happy to add some Python hooks to avoid the need for monkey-patching. Changes look fine, but the CONTRIBUTORS file took a conflict - would you mind updating it again?

@chel-ou chel-ou force-pushed the add_hooks_for_comparing_answers branch from 059df22 to 60d652b Compare March 14, 2025 15:35
@chel-ou
Copy link
Contributor Author

chel-ou commented Mar 14, 2025

Thanks @dae. Conflict resolved.

@dae dae merged commit 122980e into ankitects:main Mar 15, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants