diff --git a/anki/template/furigana.py b/anki/template/furigana.py new file mode 100644 index 000000000..1e99e19f5 --- /dev/null +++ b/anki/template/furigana.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Copyright: Damien Elmes +# 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'\1\2' + +def kanji(txt, *args): + return re.sub(r, r'\1', txt) + +def kana(txt, *args): + return re.sub(r, r'\2', txt) + +def furigana(txt, *args): + return re.sub(r, ruby, txt) + +def install(): + addHook('fmod_kanji', kanji) + addHook('fmod_kana', kana) + addHook('fmod_furigana', furigana) diff --git a/anki/template/template.py b/anki/template/template.py index dce750f70..8a6e9aec0 100644 --- a/anki/template/template.py +++ b/anki/template/template.py @@ -3,10 +3,10 @@ import cgi import collections from anki.utils import stripHTML from anki.hooks import runFilter +from anki.template import furigana; furigana.install() clozeReg = r"\{\{c%s::(.*?)(::(.*?))?\}\}" - modifiers = {} def modifier(symbol): """Decorator for associating a function with a Mustache tag modifier. @@ -185,8 +185,8 @@ class Template(object): else: return "" else: - # hook-based modifier - txt = runFilter('fieldModifier_' + mod, txt, extra, context, tag, tag_name); + # hook-based field modifier + txt = runFilter('fmod_' + mod, txt, extra, context, tag, tag_name); if txt is None: return '{unknown field %s}' % tag_name return txt