diff --git a/pylib/anki/collection.py b/pylib/anki/collection.py index 8d33d1c3c..3de9c0111 100644 --- a/pylib/anki/collection.py +++ b/pylib/anki/collection.py @@ -668,6 +668,10 @@ where c.nid = n.id and c.id in %s group by nid""" hooks.run_modify_fields_for_rendering_hook(fields, model, data) fields = runFilter("mungeFields", fields, model, data, self) # legacy + # and the template prior to rendering + qfmt = hooks.run_original_card_template_filter(qfmt, True) + afmt = hooks.run_original_card_template_filter(qfmt, False) + # render fields qatext = render_card(self, qfmt, afmt, fields, card_ord) ret: Dict[str, Any] = dict(q=qatext[0], a=qatext[1], id=card_id) diff --git a/pylib/anki/hooks.py b/pylib/anki/hooks.py index b15712e46..88713873d 100644 --- a/pylib/anki/hooks.py +++ b/pylib/anki/hooks.py @@ -46,6 +46,7 @@ modify_fields_for_rendering_hook: List[ ] = [] note_type_created_hook: List[Callable[[Dict[str, Any]], None]] = [] odue_invalid_hook: List[Callable[[], None]] = [] +original_card_template_filter: List[Callable[[str, bool], str]] = [] prepare_searches_hook: List[Callable[[Dict[str, Callable]], None]] = [] remove_notes_hook: List[Callable[[anki.storage._Collection, List[int]], None]] = [] rendered_card_template_filter: List[ @@ -183,6 +184,17 @@ def run_odue_invalid_hook() -> None: raise +def run_original_card_template_filter(template: str, question_side: bool) -> str: + for filter in original_card_template_filter: + try: + template = filter(template, question_side) + except: + # if the hook fails, remove it + original_card_template_filter.remove(filter) + raise + return template + + def run_prepare_searches_hook(searches: Dict[str, Callable]) -> None: for hook in prepare_searches_hook: try: diff --git a/pylib/tools/genhooks.py b/pylib/tools/genhooks.py index e55e6d03f..cee64c4b4 100644 --- a/pylib/tools/genhooks.py +++ b/pylib/tools/genhooks.py @@ -59,6 +59,11 @@ hooks = [ name="modify_fields_for_rendering", args=["fields: Dict[str, str]", "notetype: Dict[str, Any]", "data: QAData",], ), + Hook( + name="original_card_template", + args=["template: str", "question_side: bool"], + return_type="str", + ), Hook( name="rendered_card_template", args=[