Rename constants

This commit is contained in:
Michal Pokorný (Rai) 2019-12-26 00:20:19 +01:00
parent 020fa0b2f8
commit a0d3c242af
2 changed files with 17 additions and 13 deletions

View file

@ -235,16 +235,20 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
def _expandClozes(self, string: str) -> 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, matchGroupHint, matchGroupContent from anki.template.template import (
clozeReg,
CLOZE_REGEX_MATCH_GROUP_HINT,
CLOZE_REGEX_MATCH_GROUP_CONTENT,
)
def qrepl(m): def qrepl(m):
if m.group(4): if m.group(CLOZE_REGEX_MATCH_GROUP_HINT):
return "[%s]" % m.group(matchGroupHint) return "[%s]" % m.group(CLOZE_REGEX_MATCH_GROUP_HINT)
else: else:
return "[...]" return "[...]"
def arepl(m): def arepl(m):
return m.group(matchGroupContent) return m.group(CLOZE_REGEX_MATCH_GROUP_CONTENT)
for ord in ords: for ord in ords:
s = re.sub(clozeReg % ord, qrepl, string) s = re.sub(clozeReg % ord, qrepl, string)

View file

@ -13,9 +13,9 @@ from anki.utils import stripHTML, stripHTMLMedia
clozeReg = r"(?si)\{\{(?P<tag>c)%s::(?P<content>.*?)(::(?P<hint>.*?))?\}\}" clozeReg = r"(?si)\{\{(?P<tag>c)%s::(?P<content>.*?)(::(?P<hint>.*?))?\}\}"
# Constants referring to group names within clozeReg. # Constants referring to group names within clozeReg.
matchGroupTag = "tag" CLOZE_REGEX_MATCH_GROUP_TAG = "tag"
matchGroupContent = "content" CLOZE_REGEX_MATCH_GROUP_CONTENT = "content"
matchGroupHint = "hint" CLOZE_REGEX_MATCH_GROUP_HINT = "hint"
modifiers: Dict[str, Callable] = {} modifiers: Dict[str, Callable] = {}
@ -106,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(matchGroupTag) val = m.group(CLOZE_REGEX_MATCH_GROUP_TAG)
else: else:
val = get_or_attr(context, section_name, None) val = get_or_attr(context, section_name, None)
@ -211,7 +211,7 @@ class Template:
return txt return txt
@classmethod @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.""" """Processe the given Cloze deletion within the given template."""
reg = clozeReg reg = clozeReg
currentRegex = clozeReg % ord currentRegex = clozeReg % ord
@ -223,14 +223,14 @@ 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(matchGroupHint): if m.group(CLOZE_REGEX_MATCH_GROUP_HINT):
buf = "[%s]" % m.group(matchGroupHint) buf = "[%s]" % m.group(CLOZE_REGEX_MATCH_GROUP_HINT)
else: else:
buf = "[...]" buf = "[...]"
else: else:
buf = m.group(matchGroupContent) buf = m.group(CLOZE_REGEX_MATCH_GROUP_CONTENT)
# uppercase = no formatting # uppercase = no formatting
if m.group(matchGroupTag) == "c": if m.group(CLOZE_REGEX_MATCH_GROUP_TAG) == "c":
buf = "<span class=cloze>%s</span>" % buf buf = "<span class=cloze>%s</span>" % buf
return buf return buf