mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 15:02:21 -04:00
Use named groups instead of numbered groups to refer to clozeReg
This commit is contained in:
parent
ed8340a4e3
commit
567b3670b7
2 changed files with 26 additions and 14 deletions
|
@ -232,23 +232,23 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
|
||||||
l.append(fname)
|
l.append(fname)
|
||||||
return l
|
return l
|
||||||
|
|
||||||
def _expandClozes(self, string) -> List[str]:
|
def _expandClozes(self, string: str) -> List[str]:
|
||||||
ords = set(re.findall(r"{{c(\d+)::.+?}}", string))
|
ords = set(re.findall(r"{{c(\d+)::.+?}}", string))
|
||||||
strings = []
|
strings = []
|
||||||
from anki.template.template import clozeReg
|
from anki.template.template import clozeReg, matchGroupHint, matchGroupContent
|
||||||
|
|
||||||
def qrepl(m):
|
def qrepl(m):
|
||||||
if m.group(4):
|
if m.group(4):
|
||||||
return "[%s]" % m.group(4)
|
return "[%s]" % m.group(matchGroupHint)
|
||||||
else:
|
else:
|
||||||
return "[...]"
|
return "[...]"
|
||||||
|
|
||||||
def arepl(m):
|
def arepl(m):
|
||||||
return m.group(2)
|
return m.group(matchGroupContent)
|
||||||
|
|
||||||
for ord in ords:
|
for ord in ords:
|
||||||
s = re.sub(clozeReg % ord, qrepl, string)
|
s = re.sub(clozeReg % ord, qrepl, string)
|
||||||
s = re.sub(clozeReg % ".+?", "\\2", s)
|
s = re.sub(clozeReg % ".+?", arepl, s)
|
||||||
strings.append(s)
|
strings.append(s)
|
||||||
strings.append(re.sub(clozeReg % ".+?", arepl, string))
|
strings.append(re.sub(clozeReg % ".+?", arepl, string))
|
||||||
return strings
|
return strings
|
||||||
|
|
|
@ -5,7 +5,17 @@ from anki.hooks import runFilter
|
||||||
from anki.utils import stripHTML, stripHTMLMedia
|
from anki.utils import stripHTML, stripHTMLMedia
|
||||||
|
|
||||||
# Matches a {{c123::clozed-out text::hint}} Cloze deletion, case-insensitively.
|
# Matches a {{c123::clozed-out text::hint}} Cloze deletion, case-insensitively.
|
||||||
clozeReg = r"(?si)\{\{(c)%s::(.*?)(::(.*?))?\}\}"
|
# The regex should be interpolated with a regex number and creates the following
|
||||||
|
# named groups:
|
||||||
|
# - tag: The lowercase or uppercase 'c' letter opening the Cloze.
|
||||||
|
# - content: Clozed-out content.
|
||||||
|
# - hint: Cloze hint, if provided.
|
||||||
|
clozeReg = r"(?si)\{\{(?P<tag>c)%s::(?P<content>.*?)(::(?P<hint>.*?))?\}\}"
|
||||||
|
|
||||||
|
# Constants referring to group names within clozeReg.
|
||||||
|
matchGroupTag = "tag"
|
||||||
|
matchGroupContent = "content"
|
||||||
|
matchGroupHint = "hint"
|
||||||
|
|
||||||
modifiers: Dict[str, Callable] = {}
|
modifiers: Dict[str, Callable] = {}
|
||||||
|
|
||||||
|
@ -96,7 +106,7 @@ class Template:
|
||||||
txt = get_or_attr(context, m.group(2), None)
|
txt = get_or_attr(context, m.group(2), None)
|
||||||
m = re.search(clozeReg % m.group(1), txt)
|
m = re.search(clozeReg % m.group(1), txt)
|
||||||
if m:
|
if m:
|
||||||
val = m.group(1)
|
val = m.group(matchGroupTag)
|
||||||
else:
|
else:
|
||||||
val = get_or_attr(context, section_name, None)
|
val = get_or_attr(context, section_name, None)
|
||||||
|
|
||||||
|
@ -200,9 +210,11 @@ class Template:
|
||||||
return "{unknown field %s}" % tag_name
|
return "{unknown field %s}" % tag_name
|
||||||
return txt
|
return txt
|
||||||
|
|
||||||
def clozeText(self, txt, ord, type) -> str:
|
def clozeText(self, txt: str, ord: str, type: str) -> str:
|
||||||
|
"""Processes the given Cloze deletion within the given template."""
|
||||||
reg = clozeReg
|
reg = clozeReg
|
||||||
if not re.search(reg % ord, txt):
|
currentRegex = clozeReg % ord
|
||||||
|
if not re.search(currentRegex, txt):
|
||||||
# No Cloze deletion was found in txt.
|
# No Cloze deletion was found in txt.
|
||||||
return ""
|
return ""
|
||||||
txt = self._removeFormattingFromMathjax(txt, ord)
|
txt = self._removeFormattingFromMathjax(txt, ord)
|
||||||
|
@ -210,18 +222,18 @@ class Template:
|
||||||
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(4):
|
if m.group(matchGroupHint):
|
||||||
buf = "[%s]" % m.group(4)
|
buf = "[%s]" % m.group(matchGroupHint)
|
||||||
else:
|
else:
|
||||||
buf = "[...]"
|
buf = "[...]"
|
||||||
else:
|
else:
|
||||||
buf = m.group(2)
|
buf = m.group(matchGroupContent)
|
||||||
# uppercase = no formatting
|
# uppercase = no formatting
|
||||||
if m.group(1) == "c":
|
if m.group(matchGroupTag) == "c":
|
||||||
buf = "<span class=cloze>%s</span>" % buf
|
buf = "<span class=cloze>%s</span>" % buf
|
||||||
return buf
|
return buf
|
||||||
|
|
||||||
txt = re.sub(reg % ord, repl, txt)
|
txt = re.sub(currentRegex, repl, txt)
|
||||||
# and display other clozes normally
|
# and display other clozes normally
|
||||||
return re.sub(reg % r"\d+", "\\2", txt)
|
return re.sub(reg % r"\d+", "\\2", txt)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue