mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
only strip formatting when inside mathjax
This commit is contained in:
parent
4990b2f8eb
commit
593569d0dc
1 changed files with 17 additions and 10 deletions
|
@ -4,7 +4,7 @@ from anki.hooks import runFilter
|
||||||
from anki.template import furigana; furigana.install()
|
from anki.template import furigana; furigana.install()
|
||||||
from anki.template import hint; hint.install()
|
from anki.template import hint; hint.install()
|
||||||
|
|
||||||
clozeReg = r"(?s)\{\{c%s::(.*?)(::(.*?))?\}\}"
|
clozeReg = r"(?si)\{\{(c)%s::(.*?)(::(.*?))?\}\}"
|
||||||
|
|
||||||
modifiers = {}
|
modifiers = {}
|
||||||
def modifier(symbol):
|
def modifier(symbol):
|
||||||
|
@ -178,7 +178,7 @@ class Template:
|
||||||
# hook-based field modifier
|
# hook-based field modifier
|
||||||
mod, extra = re.search("^(.*?)(?:\((.*)\))?$", mod).groups()
|
mod, extra = re.search("^(.*?)(?:\((.*)\))?$", mod).groups()
|
||||||
txt = runFilter('fmod_' + mod, txt or '', extra or '', context,
|
txt = runFilter('fmod_' + mod, txt or '', extra or '', context,
|
||||||
tag, tag_name);
|
tag, tag_name)
|
||||||
if txt is None:
|
if txt is None:
|
||||||
return '{unknown field %s}' % tag_name
|
return '{unknown field %s}' % tag_name
|
||||||
return txt
|
return txt
|
||||||
|
@ -187,25 +187,32 @@ class Template:
|
||||||
reg = clozeReg
|
reg = clozeReg
|
||||||
if not re.search(reg%ord, txt):
|
if not re.search(reg%ord, txt):
|
||||||
return ""
|
return ""
|
||||||
mathjax = False
|
txt = self._removeFormattingFromMathjax(txt, ord)
|
||||||
if '\[' in txt or '\(' in txt:
|
|
||||||
mathjax = True
|
|
||||||
def repl(m):
|
def repl(m):
|
||||||
# replace chosen cloze with type
|
# replace chosen cloze with type
|
||||||
if type == "q":
|
if type == "q":
|
||||||
if m.group(3):
|
if m.group(4):
|
||||||
buf = "[%s]" % m.group(3)
|
buf = "[%s]" % m.group(4)
|
||||||
else:
|
else:
|
||||||
buf = "[...]"
|
buf = "[...]"
|
||||||
else:
|
else:
|
||||||
buf = m.group(1)
|
buf = m.group(2)
|
||||||
if not mathjax:
|
# uppercase = no formatting
|
||||||
buf = "<span class=cloze>[%s]</span>" % buf
|
if m.group(1) == "c":
|
||||||
|
buf = "<span class=cloze>%s</span>" % buf
|
||||||
return buf
|
return buf
|
||||||
txt = re.sub(reg%ord, repl, txt)
|
txt = re.sub(reg%ord, repl, txt)
|
||||||
# and display other clozes normally
|
# and display other clozes normally
|
||||||
return re.sub(reg%"\d+", "\\1", txt)
|
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('=')
|
@modifier('=')
|
||||||
def render_delimiter(self, tag_name=None, context=None):
|
def render_delimiter(self, tag_name=None, context=None):
|
||||||
"""Changes the Mustache delimiter."""
|
"""Changes the Mustache delimiter."""
|
||||||
|
|
Loading…
Reference in a new issue