kerrickstaley / genanki

A Python 3 library for generating Anki decks
MIT License
1.99k stars 150 forks source link

issue - multiline cloze detection (with solution) #58

Closed mat383 closed 3 years ago

mat383 commented 3 years ago

Issue:

Multiline cloze deletions aren't detected. When exporting deck to Anki, cards with multiline clozes (probably only the ones with multiline clozes, but I didn't test that) don't show. Only when database check is preformed they are detected and put into deck named "Default".

Solution:

Edit3: I tested my solution and seems that there is also some other problem Edit4: it seems to be working (I forgot to change model to cloze in my test) Change in line 101 of file genanki/note.py:

      card_ords.update(int(m)-1 for m in re.findall(r"{{c(\d+)::.+?}}", field_value) if int(m) > 0)

to:

      card_ords.update(int(m)-1 for m in re.findall(r"{{c(\d+)::.+?}}", field_value, re.DOTALL) if int(m) > 0)

or:

      card_ords.update(int(m)-1 for m in re.findall(r"{{c(\d+)::[\s\S]+?}}", field_value) if int(m) > 0)

Explanation:

. matches anything except a newline character (relevant python3 doc).

Example:

test_str_part = \
"""
{{c1:: test 
}}
"""
clozes = re.findall(r"{{c(\d+)::.+?}}", test_str_part)
for e in clozes:
    print(e)
#cloze isn't detected

Other:

I'm new to git and currently I don't have much time, but if I find any I will try to send commit with fixed code (if that's possible).

With regards mat383

Edit1: expanded issue description Edit2: grammar

kerrickstaley commented 3 years ago

This should be fixed in genanki 0.9.1; let me know if it works for you!