only strip formatting when inside mathjax

This commit is contained in:
Damien Elmes 2017-09-08 19:35:34 +10:00
parent 4990b2f8eb
commit 593569d0dc

View File

@ -4,7 +4,7 @@ from anki.hooks import runFilter
from anki.template import furigana; furigana.install()
from anki.template import hint; hint.install()
clozeReg = r"(?s)\{\{c%s::(.*?)(::(.*?))?\}\}"
clozeReg = r"(?si)\{\{(c)%s::(.*?)(::(.*?))?\}\}"
modifiers = {}
def modifier(symbol):
@ -178,7 +178,7 @@ class Template:
# hook-based field modifier
mod, extra = re.search("^(.*?)(?:\((.*)\))?$", mod).groups()
txt = runFilter('fmod_' + mod, txt or '', extra or '', context,
tag, tag_name);
tag, tag_name)
if txt is None:
return '{unknown field %s}' % tag_name
return txt
@ -187,25 +187,32 @@ class Template:
reg = clozeReg
if not re.search(reg%ord, txt):
return ""
mathjax = False
if '\[' in txt or '\(' in txt:
mathjax = True
txt = self._removeFormattingFromMathjax(txt, ord)
def repl(m):
# replace chosen cloze with type
if type == "q":
if m.group(3):
buf = "[%s]" % m.group(3)
if m.group(4):
buf = "[%s]" % m.group(4)
else:
buf = "[...]"
else:
buf = m.group(1)
if not mathjax:
buf = "<span class=cloze>[%s]</span>" % buf
buf = m.group(2)
# uppercase = no formatting
if m.group(1) == "c":
buf = "<span class=cloze>%s</span>" % buf
return buf
txt = re.sub(reg%ord, repl, txt)
# and display other clozes normally
return re.sub(reg%"\d+", "\\1", txt)
# look for clozes wrapped in mathjax, and change {{cx to {{Cx
def _removeFormattingFromMathjax(self, txt, ord):
regex = r"(\\[([]).*?"+(clozeReg%ord)+r".*?(\\[\])])"
def repl(m):
return m.group(0).replace("{{c", "{{C")
txt = re.sub(regex, repl, txt)
return txt
@modifier('=')
def render_delimiter(self, tag_name=None, context=None):
"""Changes the Mustache delimiter."""