support typing in a cloze deletion

This commit is contained in:
Damien Elmes 2012-02-13 13:22:21 +09:00
parent fa7a88b534
commit d9fdf6dc01

View file

@ -240,13 +240,28 @@ img { max-width: 95%; max-height: 95%; }
def typeAnsQuestionFilter(self, buf): def typeAnsQuestionFilter(self, buf):
self.typeCorrect = None self.typeCorrect = None
clozeIdx = None
m = re.search(self.typeAnsPat, buf) m = re.search(self.typeAnsPat, buf)
if not m: if not m:
return buf return buf
fld = m.group(1) fld = m.group(1)
# if it's a cloze, extract data
if fld.startswith("cq:"):
# get field and cloze position
m = re.match("cq:(\d+):(.+)", fld)
if not m:
return re.sub(
self.typeAnsPat, _("Type answer: invalid cloze pattern"),
buf)
clozeIdx = m.group(1)
fld = m.group(2)
# loop through fields for a match
for f in self.card.model()['flds']: for f in self.card.model()['flds']:
if f['name'] == fld: if f['name'] == fld:
self.typeCorrect = self.card.note()[f['name']] self.typeCorrect = self.card.note()[f['name']]
if clozeIdx:
# narrow to cloze
self.typeCorrect = self._contentForCloze(self.typeCorrect, clozeIdx)
self.typeFont = f['font'] self.typeFont = f['font']
self.typeSize = f['size'] self.typeSize = f['size']
break break
@ -278,6 +293,13 @@ img { max-width: 95%; max-height: 95%; }
<span style="font-family: '%s'; font-size: %spx">%s</span>""" % <span style="font-family: '%s'; font-size: %spx">%s</span>""" %
(self.typeFont, self.typeSize, res), buf) (self.typeFont, self.typeSize, res), buf)
def _contentForCloze(self, txt, idx):
matches = re.findall("\{\{c%s::(.+?)\}\}"%idx, txt)
if len(matches) > 1:
txt = ", ".join(matches)
else:
txt = matches[0]
return txt
# following type answer functions thanks to Bernhard # following type answer functions thanks to Bernhard
def calculateOkBadStyle(self): def calculateOkBadStyle(self):