The field_filter hook doesn’t include this kind of info in the arguments passed. Is there another way this can be found?
The only (ugly) workaround I can come up with:
import re
from anki import cards, hooks, template
from aqt import gui_hooks
q_start = "###question###"
q_end = "###/question###"
a_start = "###answer###"
a_end = "###/answer###"
regex = fr"{q_start}(?P<q_text>.*?){q_end}{a_start}(?P<a_text>.*?){a_end}"
def on_field_filter(
field_text: str,
field_name: str,
filter_name: str,
ctx: template.TemplateRenderContext,
) -> str:
if filter_name == "my_filter_name":
q_text = "foo"
a_text = "bar"
field_text = f"{q_start}{q_text}{q_end}{a_start}{a_text}{a_end}"
return field_text
def on_card_will_show(text: str, card: cards.Card, kind: str) -> str:
if "Question" in kind:
text = re.sub(regex, r"\g<q_text>", text, flags=re.DOTALL)
elif "Answer" in kind:
text = re.sub(regex, r"\g<a_text>", text, flags=re.DOTALL)
return text
hooks.field_filter.append(on_field_filter)
gui_hooks.card_will_show.append(on_card_will_show)
2 Likes
Indeed, it’s an ugly, but a viable workaround Thanks!
1 Like
We could probably expose the side to the field filter, but changing the signature will require a new hook. One alternative would be to add a bool property on ctx.
Was going to do something like this. Will send a PR.
2 Likes