# Need help porting two add-ons to newer version (\>2.1.44)

**URL:** <https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840>\
**Category:** Add-ons\
**Created:** [December 31, 2022, 12:29pm UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840 "2022-12-31T12:29:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpanno](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lpanno](https://forums.ankiweb.net/u/lpanno)\
**Post date:** [December 31, 2022, 12:29pm UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/1 "2022-12-31T12:29:58Z")

</div>

Hello,

I’m trying to upgrade two similar add-ons, but I couldn’t figure out how hooks work in the newer versions of Anki. The add-ons are the followings:

- ABC notation to MP3 (music integration) for Anki 2.1.x (add-on number 203713821)
- LilyPond Integration (Sheet Music Typesetter) for Anki 2.1.x (add-on number 123418104)

I have a hook that looks like this (_mungeFields_ is my callback function and _“mungeFields”_ is the hook):

```auto
addHook("mungeFields", mungeFields)

```

It seems that it is never called. What should I do instead?

For the other portions of code to port, I figured out to replace the depreciated API, but PyCharme - whitch is advised to use - wasn’t so helpful. For instance, the autocompletion proposed “stripHTML” instead of the new “strip\_html”… so I’ll prefer to use a simple text editor in the future.

Greetings,  
Luca

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [December 31, 2022, 9:42pm UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/2 "2022-12-31T21:42:50Z")

</div>

> [@lpanno](#):
>
> `addHook("mungeFields", mungeFields)`

This is apparently a really old hook that is no longer called. You can try replacing it with editor\_will\_munge\_html. Hooks are defined and documented in the following two pages: [genhooks\_gui.py](https://github.com/ankitects/anki/blob/main/qt/tools/genhooks_gui.py), [genhooks.py](https://github.com/ankitects/anki/blob/main/pylib/tools/genhooks.py).

> [@lpanno](#):
>
> For the other portions of code to port, I figured out to replace the depreciated API, but PyCharme - whitch is advised to use - wasn’t so helpful. For instance, the autocompletion proposed “stripHTML” instead of the new “strip\_html”… so I’ll prefer to use a simple text editor in the future.

Did you install the latest versions of the anki/aqt PyPI packages? I usually use VS Code and it suggests strip\_html if I have recent packages.

---

<div class="post-metadata">

**Author:** ![lpanno](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lpanno](https://forums.ankiweb.net/u/lpanno)\
**Post date:** [January 1, 2023, 10:02am UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/3 "2023-01-01T10:02:43Z")

</div>

Hello @abdo  
Thanks for helping.

I wasn’t sure about _editor\_will\_munge\_html_ hook, but now I feel confident. I’ll try it asap.  
Is it still ok to use _addHook()_?

I don’t know if I’ve installed the latest versions of anki/aqt PyPI packages, but I followed the instructions on the documentation. No worries, I prefer a simple text editor, even if I wouldn’t complain about autocompletion 😅.

Another question: how to deal with internationalisation _gettext_ _\_()_?  
I based my work on an add-on for Anki 2.0 and it contained i18n text like _\_(‘Edit’)_ and some longer sentences. I don’t know if Anki translate all that content, but I would like to keep this functionality.  
What should I replace _\_()_ with?  
Does Anki now uses _i18n_ python module and _i18n.t()_ instead of _\_()_?

Thanks again and Happy New Year! Best wishes for 2023!  
Luca

---

<div class="post-metadata">

**Author:** ![lpanno](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lpanno](https://forums.ankiweb.net/u/lpanno)\
**Post date:** [January 1, 2023, 10:45am UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/4 "2023-01-01T10:45:57Z")

</div>

Hello again @abdo  
I’ve tried the code below, but it seems that the hook isn’t called at all when the card is displayed. Any idea?

```auto
def mungeFields(fields, model, data, col):
    '''Parse lilypond tags before they are displayed.'''
    print(" *******mungeFields**********")
    # Ignore duplicated mungeFields call for the answer side.
    if 'FrontSide' in fields:
        return fields

    for fld in model['flds']:
        field = fld['name']

        # check field name
        match = lilypondFieldRegexp.search(field)

        if match \
                and fields[field] != "(%s)" % (field,) \
                and fields[field] != "ankiflag":
            fields[field] = _imgLink(col, match.group(2), _lyFromHtml(fields[field]))

            # autofill field for web:
            imgfield = field.replace("lilypond", "lilypondimg", 1)
            if imgfield in fields and fields[field] != fields[imgfield]:
                fields[imgfield] = fields[field]
                col.findReplace((data[1],), "^.*$", fields[field], regex=True, field=imgfield)
            continue

        # check field contents
        for match in lilypondRegexp.finditer(fields[field]):
            fields[field] = fields[field].replace(
                match.group(), _imgLink(col, match.group(2), _lyFromHtml(match.group(3)))
            )

    return fields

addHook("editor_will_munge_html", mungeFields)

```

Best greetings,  
Luca

---

<div class="post-metadata">

**Author:** ![dae](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/dae/32/65_2.png) [@dae](https://forums.ankiweb.net/u/dae)\
**Post date:** [January 1, 2023, 11:15pm UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/5 "2023-01-01T23:15:25Z")

</div>

editor hooks are called while the editor is shown. If you’re trying to do something during review, that’s not going to work. Some other hooks are mentioned on [Changes in 2.1.20 to 29 - Changes](https://changes.ankiweb.net/changes/2.1.20-29.html#changes-in-2120) that may or may not work for you.

Regarding translations, please see [For Developers - Anki Translation Guide](https://translating.ankiweb.net/anki/developers.html#add-ons)

---

<div class="post-metadata">

**Author:** ![lpanno](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lpanno](https://forums.ankiweb.net/u/lpanno)\
**Post date:** [January 2, 2023, 9:34am UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/6 "2023-01-02T09:34:39Z")

</div>

Hello @dae  
Thanks for your answer.  
It seems that the hook isn’t called even when the editor is shown (I tried to add a new card and to browse cards in the deck). I’ll do a further test today, and I’ll come back to report.  
I couldn’t find a hook similar to the old _mungeFields_ in the links you and @abdo provided me, except for _editor\_will\_munge\_html_.  
Best regards,  
Luca

---

<div class="post-metadata">

**Author:** ![lpanno](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lpanno](https://forums.ankiweb.net/u/lpanno)\
**Post date:** [January 2, 2023, 11:04am UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/7 "2023-01-02T11:04:33Z")

</div>

Hello again @dae  
I should have specified that I have Qt5 Anki 2.1.54 installed on linux. Is there any difference between Qt6 and Qt5 versions?

I’ve done more tests and I found that the hook _editor\_will\_munge\_html_ isn’t called when I edit a card (regardless if I create a new card or edit an existing one).

I have noticed that there are inconsistencies in naming conventions:

- _profileLoaded_ hook still works,
- new hooks use underscore and small caps (like _editor\_will\_munge\_html_),
- _stripHTML_ function is now _strip\_html_, but _addHook_ don’t use this convention.

Is there a chance that the names will change again in the future? The question is mainly about _profileLoaded_ hook.

Best regards,  
Luca

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [January 3, 2023, 1:47am UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/8 "2023-01-03T01:47:18Z")

</div>

> [@lpanno](#):
>
> It seems that the hook isn’t called even when the editor is shown (I tried to add a new card and to browse cards in the deck).

it works for me. You’re using it incorrectly. It should be used like this because it’s a [new-style hook](https://addon-docs.ankiweb.net/hooks-and-filters.html):

```auto
aqt.gui_hooks.editor_will_munge_html.append(my_func)

```

`editor_will_munge_html` is not the right hook for your case though, because the add-on needs to change the rendered display when reviewing rather than the the edited field contents in the editor. [card\_did\_render](https://github.com/ankitects/anki/blob/3357389309057e8172fe5b161eceb4da5de12bbc/pylib/tools/genhooks.py#LL80C15-L80C30) may work.

---

<div class="post-metadata">

**Author:** ![lpanno](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lpanno](https://forums.ankiweb.net/u/lpanno)\
**Post date:** [January 3, 2023, 9:44am UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/9 "2023-01-03T09:44:18Z")

</div>

Hello @abdo  
Thanks for you answer!

I’ve made the change you suggested according to the new system of hooks. Unfortunately, it seems that the hook _card\_did\_render_ doesn’t exist (2.1.54 Qt5).

![output-3-1-20223](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/2X/f/f3c2f5f7983e607cc3bbfb74ee39a3c99e369631.png)

I installed last version of Anki (2.1.55 Qt6) and I obtained the same error.

I found another hook _card\_will\_show_, but then I don’t know how to use it. Because the old fashion hook requires these arguments: _fields_, _model_, _data_, _col_; but the new one requires only one argument: _card_. I don’t know if it is the right hook to use. I don’t know how to handle its argument (i.e. _card_) and recover what I need (i.e. _fields_, _model_, _data_, _col_). Any idea?

I’m struggling with this port: I’m not a programmer, and I did only adapt an existing no-updated add-on for Anki 2.0. I appreciate all your support and help, it means a lot. Thank you!

Greetings,  
Luca

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [January 3, 2023, 10:04am UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/10 "2023-01-03T10:04:26Z")

</div>

> [@lpanno](#):
>
> Unfortunately, it seems that the hook _card\_did\_render_ doesn’t exist

card\_did\_render is defined under anki.hooks rather than aqt.gui\_hooks (new hooks are defined in two places). `anki.hooks.card_did_render` should work.

> [@lpanno](#):
>
> I don’t know how to handle its argument (i.e. _card_) and recover what I need (i.e. _fields_, _model_, _data_, _col_). Any idea?

Probably the add-on’s behavior need to be changed a bit for it to be possible to port it to Anki 2.1, since it apparently needs to modify individual fields, the thing which card\_did\_render doesn’t provide. I can’t help here, because I’m not familiar with the add-on and don’t have the time to look into it at the moment.

---

<div class="post-metadata">

**Author:** ![lpanno](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lpanno](https://forums.ankiweb.net/u/lpanno)\
**Post date:** [January 3, 2023, 10:32am UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/11 "2023-01-03T10:32:32Z")

</div>

Thank you @abdo !  
The hook is found now.  
I’ll figure out the arguments needed and change the functionality of the callback function. I guess I have to scale down the add-on’s capabilities, if we can’t change the fields, and if we can only change the card rendering.  
Best regards,  
Luca

---

<div class="post-metadata">

**Author:** ![lpanno](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@lpanno](https://forums.ankiweb.net/u/lpanno)\
**Post date:** [January 3, 2023, 2:15pm UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/12 "2023-01-03T14:15:49Z")

</div>

Hello,  
I finally found a solution.

**LilyPond (sheet music typesetting) integration** by Andreas Klauer (add-on 1080109152) do exactly what I want. In fact, it’s his prior code for anki 2.0 that I updated for Anki 2.1 and adapted for ABC notation sound generation as well (for 2.0, then for 2.1).

I’m going to adapt my add-on for LilyPond (because it adds a menu for templates to Andreas’ add-on) and I’ll update the add-on for ABC sound generation with an similar code.

Thank you!  
Best regards,  
Luca

---

<div class="post-metadata">

**Author:** ![cleinias](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@cleinias](https://forums.ankiweb.net/u/cleinias)\
**Post date:** [March 23, 2024, 5:33am UTC](https://forums.ankiweb.net/t/need-help-porting-two-add-ons-to-newer-version-2-1-44/25840/13 "2024-03-23T05:33:47Z")

</div>

Could you share the solution you said you found? The code for the add-on on github is still using mungeFields which, as you discuss above, is never called.
