Anki/anki/template/furigana.py
Damien Elmes fec37958f4 tweak furigana regex
try to support text like:
今日[きょう]も <font color="#0000ff">元気[げんき]</font>
2012-06-11 22:19:56 +09:00

34 lines
906 B
Python

# -*- coding: utf-8 -*-
# Copyright: Damien Elmes <anki@ichi2.net>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
# Based off Kieran Clancy's initial implementation.
import re
from anki.hooks import addHook
r = r' ?([^[]+?)\[(.+?)\]([^ ]+?|$)'
ruby = r'<ruby><rb>\1</rb><rt>\2</rt></ruby>'
def noSound(repl):
repl += r"\3"
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, noSound(r'\1'), txt)
def kana(txt, *args):
return re.sub(r, noSound(r'\2'), txt)
def furigana(txt, *args):
return re.sub(r, noSound(ruby), txt)
def install():
addHook('fmod_kanji', kanji)
addHook('fmod_kana', kana)
addHook('fmod_furigana', furigana)