diff --git a/anki/template/furigana.py b/anki/template/furigana.py index 1e99e19f5..a41e1f56d 100644 --- a/anki/template/furigana.py +++ b/anki/template/furigana.py @@ -9,14 +9,23 @@ from anki.hooks import addHook r = r' ?([^ ]+?)\[(.+?)\]' ruby = r'\1\2' +def noSound(repl): + def func(match): + if match.group(2).startswith("sound:"): + # return without modification + return match.group(0) + else: + return re.sub(r, repl, match.group(0)) + return func + def kanji(txt, *args): - return re.sub(r, r'\1', txt) + return re.sub(r, noSound(r'\1'), txt) def kana(txt, *args): - return re.sub(r, r'\2', txt) + return re.sub(r, noSound(r'\2'), txt) def furigana(txt, *args): - return re.sub(r, ruby, txt) + return re.sub(r, noSound(ruby), txt) def install(): addHook('fmod_kanji', kanji) diff --git a/tests/test_collection.py b/tests/test_collection.py index b4dd3fd00..cb175ee4f 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -143,3 +143,17 @@ def test_timestamps(): addBasicModel(deck) assert len(deck.models.models) == 102 +def test_furigana(): + deck = getEmptyDeck() + mm = deck.models + m = mm.current() + m['tmpls'][0]['qfmt'] = '{{kana:Front}}' + mm.save(m) + n = deck.newNote() + n['Front'] = 'foo[abc]' + deck.addNote(n) + c = n.cards()[0] + assert c.q().endswith("abc") + n['Front'] = 'foo[sound:abc.mp3]' + n.flush() + assert "sound:" in c.q(reload=True)