Are chained modifiers with arguments still supported?


A few years ago, I wrote an add-on to strip diacritics from Arabic and Hebrew, and introduced into Anki the possibility to do things like {{my_modifier:cloze:Text}}. The add-on ( supported arguments, so you could do {{my_modifier(arg1, arg2):cloze:Text}}.

People have asked me to port the add-on to Anki 2.1. I’ve managed to quickly adapt it to run using the first format (without parentheses nor args), but can’t quite figure how to make it work with the args. Looking at pylib/anki/, I notice a mention “# legacy hook - the second and fifth argument are no longer used.”, which I think was what I was relying on.

Is the functionality entirely gone, or do I need to use a new type of hook? I haven’t programmed with Anki for a very long time, so I would appreciate pointers :slight_smile:

You can create a custom filter with anki.hooks.field_filter in new style hooks. See this section for more details. Here is an example of a custom filter that supports arguments in parentheses:

import re
import anki

filter_pattern = re.compile(r"my_modifier_(?P<kind>.*?)\((?P<args>.*?)\)")

def on_field_filter(
    field_text: str,
    field_name: str,
    filter_name: str,
    ctx: anki.template.TemplateRenderContext,
) -> str:
    match = filter_pattern.match(filter_name)
    if match:
        kind ="kind")
        args = [i.strip() for i in"args").split(",")]
        # Do some processing on "field_text"
        field_text += f"<br>({kind=}, {args=})"

    return field_text




Thanks a lot for this, @hkr, that was very useful and I’ve now managed to update the add-on.

A quick question about publication, if I may: during the process, it asks from which version to which version (of Anki) the add-on work, defaulting to “from 2.1.0 to 2.1.0”. Is leaving it as-is the correct thing? Or should I indicate the current latest version of Anki in the “to”-field?

1 Like

If your add-on works on any Anki version from 2.1.0 onwards, then you can leave it as is.

1 Like