diff --git a/anki/template/template.py b/anki/template/template.py index 654e6e3d4..d803f3722 100644 --- a/anki/template/template.py +++ b/anki/template/template.py @@ -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 = "[%s]" % buf + buf = m.group(2) + # uppercase = no formatting + if m.group(1) == "c": + buf = "%s" % 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."""