mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
move furigana/hint filters into template2
This commit is contained in:
parent
4bca26161b
commit
706ed225ca
4 changed files with 61 additions and 76 deletions
|
@ -1,12 +1,7 @@
|
|||
from typing import Any
|
||||
|
||||
from . import furigana, hint
|
||||
from .template import Template
|
||||
|
||||
furigana.install()
|
||||
|
||||
hint.install()
|
||||
|
||||
|
||||
def render(template, context=None, **kwargs) -> Any:
|
||||
context = context and context.copy() or {}
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
# Copyright: Ankitects Pty Ltd and contributors
|
||||
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
# Based off Kieran Clancy's initial implementation.
|
||||
|
||||
import re
|
||||
from typing import Any, Callable
|
||||
|
||||
from anki.hooks import addHook
|
||||
|
||||
r = r" ?([^ >]+?)\[(.+?)\]"
|
||||
ruby = r"<ruby><rb>\1</rb><rt>\2</rt></ruby>"
|
||||
|
||||
|
||||
def noSound(repl) -> Callable[[Any], Any]:
|
||||
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 _munge(s) -> Any:
|
||||
return s.replace(" ", " ")
|
||||
|
||||
|
||||
def kanji(txt, *args) -> str:
|
||||
return re.sub(r, noSound(r"\1"), _munge(txt))
|
||||
|
||||
|
||||
def kana(txt, *args) -> str:
|
||||
return re.sub(r, noSound(r"\2"), _munge(txt))
|
||||
|
||||
|
||||
def furigana(txt, *args) -> str:
|
||||
return re.sub(r, noSound(ruby), _munge(txt))
|
||||
|
||||
|
||||
def install() -> None:
|
||||
addHook("fmod_kanji", kanji)
|
||||
addHook("fmod_kana", kana)
|
||||
addHook("fmod_furigana", furigana)
|
|
@ -1,26 +0,0 @@
|
|||
# Copyright: Ankitects Pty Ltd and contributors
|
||||
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
from anki.hooks import addHook
|
||||
from anki.lang import _
|
||||
|
||||
|
||||
def hint(txt, extra, context, tag, fullname) -> str:
|
||||
if not txt.strip():
|
||||
return ""
|
||||
# random id
|
||||
domid = "hint%d" % id(txt)
|
||||
return """
|
||||
<a class=hint href="#"
|
||||
onclick="this.style.display='none';document.getElementById('%s').style.display='block';return false;">
|
||||
%s</a><div id="%s" class=hint style="display: none">%s</div>
|
||||
""" % (
|
||||
domid,
|
||||
_("Show %s") % tag,
|
||||
domid,
|
||||
txt,
|
||||
)
|
||||
|
||||
|
||||
def install() -> None:
|
||||
addHook("fmod_hint", hint)
|
|
@ -9,9 +9,11 @@ connected to pystache. It may be renamed in the future.
|
|||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Dict, Tuple
|
||||
from typing import Any, Callable, Dict, Tuple
|
||||
|
||||
import anki
|
||||
from anki.hooks import addHook
|
||||
from anki.lang import _
|
||||
from anki.sound import stripSounds
|
||||
|
||||
|
||||
|
@ -31,3 +33,61 @@ def renderFromFieldMap(
|
|||
atext = anki.template.render(format, fields)
|
||||
|
||||
return qtext, atext
|
||||
|
||||
|
||||
# Filters
|
||||
##########################################################################
|
||||
|
||||
|
||||
def hint(txt, extra, context, tag, fullname) -> str:
|
||||
if not txt.strip():
|
||||
return ""
|
||||
# random id
|
||||
domid = "hint%d" % id(txt)
|
||||
return """
|
||||
<a class=hint href="#"
|
||||
onclick="this.style.display='none';document.getElementById('%s').style.display='block';return false;">
|
||||
%s</a><div id="%s" class=hint style="display: none">%s</div>
|
||||
""" % (
|
||||
domid,
|
||||
_("Show %s") % tag,
|
||||
domid,
|
||||
txt,
|
||||
)
|
||||
|
||||
|
||||
FURIGANA_RE = r" ?([^ >]+?)\[(.+?)\]"
|
||||
RUBY_REPL = r"<ruby><rb>\1</rb><rt>\2</rt></ruby>"
|
||||
|
||||
|
||||
def replace_if_not_audio(repl: str) -> Callable[[Any], Any]:
|
||||
def func(match):
|
||||
if match.group(2).startswith("sound:"):
|
||||
# return without modification
|
||||
return match.group(0)
|
||||
else:
|
||||
return re.sub(FURIGANA_RE, repl, match.group(0))
|
||||
|
||||
return func
|
||||
|
||||
|
||||
def without_nbsp(s: str) -> str:
|
||||
return s.replace(" ", " ")
|
||||
|
||||
|
||||
def kanji(txt: str, *args) -> str:
|
||||
return re.sub(FURIGANA_RE, replace_if_not_audio(r"\1"), without_nbsp(txt))
|
||||
|
||||
|
||||
def kana(txt: str, *args) -> str:
|
||||
return re.sub(FURIGANA_RE, replace_if_not_audio(r"\2"), without_nbsp(txt))
|
||||
|
||||
|
||||
def furigana(txt: str, *args) -> str:
|
||||
return re.sub(FURIGANA_RE, replace_if_not_audio(RUBY_REPL), without_nbsp(txt))
|
||||
|
||||
|
||||
addHook("fmod_hint", hint)
|
||||
addHook("fmod_kanji", kanji)
|
||||
addHook("fmod_kana", kana)
|
||||
addHook("fmod_furigana", furigana)
|
||||
|
|
Loading…
Reference in a new issue