Vocab-Apps / anki-hyper-tts

HyperTTS Addon for Anki
GNU General Public License v3.0
84 stars 10 forks source link

Advanced templates aren't loaded properly #213

Open frifix opened 1 month ago

frifix commented 1 month ago

Hi,

I have flash cards like this: Front: I don't usualy eat [[ podwieczorek ]] Back: afternoon tea

ANKI_1

I have created an advanced template in HyperTTS that replace the word in [[ ]] brackets with the word that is on the back. Here it's my advanced template script:

def format_text(raw, use):
    first = True
    while True:
        start = raw.find('[')
        end = raw.find(']')
        if start == -1 or end == -1:
            break
        if first:
            raw = raw[:start] + use + raw[end+1:]
            first = False
        else:
            raw = raw[:start] + raw[end+1:]
    return raw

front = template_fields['Przód']
back = template_fields['Tył']
result = format_text(front, back)

For some reason when I want to add a sound it doesn't work properly for the first time, it shows an error:

ANKI_2

When I click "Show Settings" and click on "Note Id" it says: Error: No "result" variable found. You must assign the final template output to a result variable.

ANKI_3

When I change something in the script (e.g. remove one letter and add it again) then the whole script will be loaded again and the error no longer appears:

ANKI_4

It is very annoying that I have to do this every time when I want to add sounds for my flash cards. It would be great if this problem could be solved.

luc-vocab commented 1 month ago

looks there's a problem when initializing after loading an advanced template preset could be related to #204 ?

JSchoreels commented 10 hours ago

looks there's a problem when initializing after loading an advanced template preset could be related to #204 ?

Not sure because in my case, I have the same bug but the source_template is still there in the configuration :

  "ccd5c15f-5d66-44e8-b1ef-a0dfcd0c42ce": {
            "name": "Reading Audio Yomitan",
            "source": {
                "mode": "advanced_template",
                "source_field": null,
                "source_template": "front = template_fields['Front']\nif front:\n  result = front\nelse:\n  result = template_fields['Reading']",
                "template_format_version": "v1",
                "use_selection": false
            },
            "target": {
                "remove_sound_tag": true,
                "target_field": "Audio",
                "text_and_sound_tag": false
            },
            "text_processing": {
                "html_to_text_line": true,
                "ignore_case": false,
                "run_replace_rules_after": true,
                "ssml_convert_characters": true,
                "strip_brackets": false,
                "text_replacement_rules": [
                    {
                        "rule_type": "Simple",
                        "source": "(",
                        "target": ""
                    },
                    {
                        "rule_type": "Simple",
                        "source": ")",
                        "target": ""
                    }
                ]
            },