mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 01:06:35 -04:00
add hooks for arbitrary field modifiers
This commit is contained in:
parent
262e04ed4f
commit
1c2b6b746f
1 changed files with 34 additions and 14 deletions
|
@ -2,6 +2,7 @@ import re
|
||||||
import cgi
|
import cgi
|
||||||
import collections
|
import collections
|
||||||
from anki.utils import stripHTML
|
from anki.utils import stripHTML
|
||||||
|
from anki.hooks import runFilter
|
||||||
|
|
||||||
clozeReg = r"\{\{c%s::(.*?)(::(.*?))?\}\}"
|
clozeReg = r"\{\{c%s::(.*?)(::(.*?))?\}\}"
|
||||||
|
|
||||||
|
@ -148,28 +149,47 @@ class Template(object):
|
||||||
@modifier(None)
|
@modifier(None)
|
||||||
def render_unescaped(self, tag_name=None, context=None):
|
def render_unescaped(self, tag_name=None, context=None):
|
||||||
"""Render a tag without escaping it."""
|
"""Render a tag without escaping it."""
|
||||||
if tag_name.startswith("text:"):
|
txt = get_or_attr(context, tag_name)
|
||||||
|
if txt is not None:
|
||||||
|
# some field names could have colons in them
|
||||||
|
# avoid interpreting these as field modifiers
|
||||||
|
# better would probably be to put some restrictions on field names
|
||||||
|
return txt
|
||||||
|
|
||||||
|
# field modifiers
|
||||||
|
parts = tag_name.split(':',2)
|
||||||
|
extra = None
|
||||||
|
if len(parts) == 1 or parts[0] == '':
|
||||||
|
return '{unknown field %s}' % tag_name
|
||||||
|
elif len(parts) == 2:
|
||||||
|
(mod, tag) = parts
|
||||||
|
elif len(parts) == 3:
|
||||||
|
(mod, extra, tag) = parts
|
||||||
|
|
||||||
|
txt = get_or_attr(context, tag)
|
||||||
|
|
||||||
|
# built-in modifiers
|
||||||
|
if mod == 'text':
|
||||||
# strip html
|
# strip html
|
||||||
tag = tag_name[5:]
|
|
||||||
txt = get_or_attr(context, tag)
|
|
||||||
if txt:
|
if txt:
|
||||||
return stripHTML(txt)
|
return stripHTML(txt)
|
||||||
return ""
|
return ""
|
||||||
elif tag_name.startswith("type:"):
|
elif mod == 'type':
|
||||||
# type answer field; convert it to [[type:...]] for the gui code
|
# type answer field; convert it to [[type:...]] for the gui code
|
||||||
# to process
|
# to process
|
||||||
return "[[%s]]" % tag_name
|
return "[[%s]]" % tag_name
|
||||||
elif (tag_name.startswith("cq:") or
|
elif mod == 'cq' or mod == 'ca':
|
||||||
tag_name.startswith("ca:")):
|
|
||||||
# cloze deletion
|
# cloze deletion
|
||||||
m = re.match("c(.+):(\d+):(.+)", tag_name)
|
if txt and extra:
|
||||||
(type, ord, tag) = (m.group(1), m.group(2), m.group(3))
|
return self.clozeText(txt, extra, mod[1])
|
||||||
txt = get_or_attr(context, tag)
|
else:
|
||||||
if txt:
|
return ""
|
||||||
return self.clozeText(txt, ord, type)
|
else:
|
||||||
return ""
|
# hook-based modifier
|
||||||
# regular field
|
txt = runFilter('fieldModifier_' + mod, txt, extra, context, tag, tag_name);
|
||||||
return get_or_attr(context, tag_name, '{unknown field %s}' % tag_name)
|
if txt is None:
|
||||||
|
return '{unknown field %s}' % tag_name
|
||||||
|
return txt
|
||||||
|
|
||||||
def clozeText(self, txt, ord, type):
|
def clozeText(self, txt, ord, type):
|
||||||
reg = clozeReg
|
reg = clozeReg
|
||||||
|
|
Loading…
Reference in a new issue