more cloze work

- upgrade old decks
- specify the field in the cloze, so the user can have an id in the first
  field
This commit is contained in:
Damien Elmes 2012-04-19 08:32:19 +09:00
parent 41fa9a9896
commit 5cbe93b63d
4 changed files with 32 additions and 14 deletions

View file

@ -451,10 +451,7 @@ select id from notes where id in %s and id not in (select nid from cards)""" %
flist = splitFields(data[6])
fields = {}
model = self.models.get(data[2])
firstName = None
for (name, (idx, conf)) in self.models.fieldMap(model).items():
if idx == 0:
firstName = name
fields[name] = flist[idx]
fields['Tags'] = data[5]
fields['Type'] = model['name']
@ -468,11 +465,11 @@ select id from notes where id in %s and id not in (select nid from cards)""" %
d = dict(id=data[0])
for (type, format) in (("q", template['qfmt']), ("a", template['afmt'])):
if type == "q":
format = format.replace("{{Cloze}}", "{{cq:%d:%s}}" % (
data[4]+1, firstName))
format = format.replace("{{cloze:", "{{cq:%d:" % (
data[4]+1))
else:
format = format.replace("{{Cloze}}", "{{ca:%d:%s}}" % (
data[4]+1, firstName))
format = format.replace("{{cloze:", "{{ca:%d:" % (
data[4]+1))
fields = runFilter("mungeFields", fields, model, data, self)
html = anki.template.render(format, fields)
d[type] = runFilter(

View file

@ -509,7 +509,7 @@ select id from notes where mid = ?)""" % " ".join(map),
def _availClozeOrds(self, m, flds):
ret = [int(m)-1 for m in re.findall(
"{{c(\d)::[^}]*?}}", splitFields(flds)[0])]
"{{c(\d+)::[^}]*?}}", splitFields(flds)[0])]
return list(set(ret))
# Sync handling

View file

@ -33,12 +33,13 @@ def addClozeModel(col):
mm = col.models
m = mm.new(_("Cloze"))
m['type'] = MODEL_CLOZE
fm = mm.newField(_("Text"))
txt = _("Text")
fm = mm.newField(txt)
mm.addField(m, fm)
fm = mm.newField(_("Extra"))
mm.addField(m, fm)
t = mm.newTemplate(_("Cloze"))
fmt = "{{Cloze}}"
fmt = "{{cloze:%s}}" % txt
t['css'] += """
.cloze {
font-weight: bold;

View file

@ -2,9 +2,9 @@
# Copyright: Damien Elmes <anki@ichi2.net>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import os, simplejson, copy
import os, simplejson, copy, re
from anki.lang import _
from anki.utils import intTime
from anki.utils import intTime, ids2str
from anki.db import DB
from anki.collection import _Collection
from anki.consts import *
@ -81,11 +81,31 @@ def _upgrade(col, ver):
d['collapsed'] = False
col.decks.save(d)
if ver < 4:
col.modSchema()
for m in col.models.all():
if not "{{cloze::" in m['tmpls'][0]['qfmt']:
if not "{{cloze:" in m['tmpls'][0]['qfmt']:
m['type'] = MODEL_STD
else:
pass
_upgradeClozeModel(col, m)
col.models.save(m)
col.db.execute("update col set ver = 4")
def _upgradeClozeModel(col, m):
m['type'] = MODEL_CLOZE
# convert first template
t = m['tmpls'][0]
for type in 'qfmt', 'afmt':
t[type] = re.sub("{{cloze:1:(.+?)}}", r"{{cloze:\1}}", t[type])
t['name'] = _("Cloze")
# delete non-cloze cards for the model
rem = []
for t in m['tmpls'][1:]:
if "{{cloze:" not in t['qfmt']:
rem.append(t)
for r in rem:
col.models.remTemplate(m, r)
del m['tmpls'][1:]
col.models._updateTemplOrds(m)
# Creating a new collection
######################################################################