From 567b3670b7f5cb5bcd7aba836e23b302da4830ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Pokorn=C3=BD=20=28Rai=29?= Date: Wed, 25 Dec 2019 15:00:25 +0100 Subject: [PATCH 1/2] Use named groups instead of numbered groups to refer to clozeReg --- anki/media.py | 10 +++++----- anki/template/template.py | 30 +++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/anki/media.py b/anki/media.py index 9cac9d78b..86c6b1714 100644 --- a/anki/media.py +++ b/anki/media.py @@ -232,23 +232,23 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0); l.append(fname) return l - def _expandClozes(self, string) -> List[str]: + def _expandClozes(self, string: str) -> List[str]: ords = set(re.findall(r"{{c(\d+)::.+?}}", string)) strings = [] - from anki.template.template import clozeReg + from anki.template.template import clozeReg, matchGroupHint, matchGroupContent def qrepl(m): if m.group(4): - return "[%s]" % m.group(4) + return "[%s]" % m.group(matchGroupHint) else: return "[...]" def arepl(m): - return m.group(2) + return m.group(matchGroupContent) for ord in ords: s = re.sub(clozeReg % ord, qrepl, string) - s = re.sub(clozeReg % ".+?", "\\2", s) + s = re.sub(clozeReg % ".+?", arepl, s) strings.append(s) strings.append(re.sub(clozeReg % ".+?", arepl, string)) return strings diff --git a/anki/template/template.py b/anki/template/template.py index f1fc1f882..6bb61cf2f 100644 --- a/anki/template/template.py +++ b/anki/template/template.py @@ -5,7 +5,17 @@ from anki.hooks import runFilter from anki.utils import stripHTML, stripHTMLMedia # 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)\{\{(?Pc)%s::(?P.*?)(::(?P.*?))?\}\}" + +# Constants referring to group names within clozeReg. +matchGroupTag = "tag" +matchGroupContent = "content" +matchGroupHint = "hint" modifiers: Dict[str, Callable] = {} @@ -96,7 +106,7 @@ class Template: txt = get_or_attr(context, m.group(2), None) m = re.search(clozeReg % m.group(1), txt) if m: - val = m.group(1) + val = m.group(matchGroupTag) else: val = get_or_attr(context, section_name, None) @@ -200,9 +210,11 @@ class Template: return "{unknown field %s}" % tag_name 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 - if not re.search(reg % ord, txt): + currentRegex = clozeReg % ord + if not re.search(currentRegex, txt): # No Cloze deletion was found in txt. return "" txt = self._removeFormattingFromMathjax(txt, ord) @@ -210,18 +222,18 @@ class Template: def repl(m): # replace chosen cloze with type if type == "q": - if m.group(4): - buf = "[%s]" % m.group(4) + if m.group(matchGroupHint): + buf = "[%s]" % m.group(matchGroupHint) else: buf = "[...]" else: - buf = m.group(2) + buf = m.group(matchGroupContent) # uppercase = no formatting - if m.group(1) == "c": + if m.group(matchGroupTag) == "c": buf = "%s" % buf return buf - txt = re.sub(reg % ord, repl, txt) + txt = re.sub(currentRegex, repl, txt) # and display other clozes normally return re.sub(reg % r"\d+", "\\2", txt) From a0d3c242afebd9fdd491064a9aea2b94b145c2b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Pokorn=C3=BD=20=28Rai=29?= Date: Thu, 26 Dec 2019 00:20:19 +0100 Subject: [PATCH 2/2] Rename constants --- anki/media.py | 12 ++++++++---- anki/template/template.py | 18 +++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/anki/media.py b/anki/media.py index 86c6b1714..def118c77 100644 --- a/anki/media.py +++ b/anki/media.py @@ -235,16 +235,20 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0); def _expandClozes(self, string: str) -> List[str]: ords = set(re.findall(r"{{c(\d+)::.+?}}", string)) strings = [] - from anki.template.template import clozeReg, matchGroupHint, matchGroupContent + from anki.template.template import ( + clozeReg, + CLOZE_REGEX_MATCH_GROUP_HINT, + CLOZE_REGEX_MATCH_GROUP_CONTENT, + ) def qrepl(m): - if m.group(4): - return "[%s]" % m.group(matchGroupHint) + if m.group(CLOZE_REGEX_MATCH_GROUP_HINT): + return "[%s]" % m.group(CLOZE_REGEX_MATCH_GROUP_HINT) else: return "[...]" def arepl(m): - return m.group(matchGroupContent) + return m.group(CLOZE_REGEX_MATCH_GROUP_CONTENT) for ord in ords: s = re.sub(clozeReg % ord, qrepl, string) diff --git a/anki/template/template.py b/anki/template/template.py index 9be7d9dcc..b96936458 100644 --- a/anki/template/template.py +++ b/anki/template/template.py @@ -13,9 +13,9 @@ from anki.utils import stripHTML, stripHTMLMedia clozeReg = r"(?si)\{\{(?Pc)%s::(?P.*?)(::(?P.*?))?\}\}" # Constants referring to group names within clozeReg. -matchGroupTag = "tag" -matchGroupContent = "content" -matchGroupHint = "hint" +CLOZE_REGEX_MATCH_GROUP_TAG = "tag" +CLOZE_REGEX_MATCH_GROUP_CONTENT = "content" +CLOZE_REGEX_MATCH_GROUP_HINT = "hint" modifiers: Dict[str, Callable] = {} @@ -106,7 +106,7 @@ class Template: txt = get_or_attr(context, m.group(2), None) m = re.search(clozeReg % m.group(1), txt) if m: - val = m.group(matchGroupTag) + val = m.group(CLOZE_REGEX_MATCH_GROUP_TAG) else: val = get_or_attr(context, section_name, None) @@ -211,7 +211,7 @@ class Template: return txt @classmethod - def clozeText(self, txt: str, ord: str, type: str) -> str: + def clozeText(cls, txt: str, ord: str, type: str) -> str: """Processe the given Cloze deletion within the given template.""" reg = clozeReg currentRegex = clozeReg % ord @@ -223,14 +223,14 @@ class Template: def repl(m): # replace chosen cloze with type if type == "q": - if m.group(matchGroupHint): - buf = "[%s]" % m.group(matchGroupHint) + if m.group(CLOZE_REGEX_MATCH_GROUP_HINT): + buf = "[%s]" % m.group(CLOZE_REGEX_MATCH_GROUP_HINT) else: buf = "[...]" else: - buf = m.group(matchGroupContent) + buf = m.group(CLOZE_REGEX_MATCH_GROUP_CONTENT) # uppercase = no formatting - if m.group(matchGroupTag) == "c": + if m.group(CLOZE_REGEX_MATCH_GROUP_TAG) == "c": buf = "%s" % buf return buf