diff --git a/anki/collection.py b/anki/collection.py index 36b95858c..4e4c20f35 100644 --- a/anki/collection.py +++ b/anki/collection.py @@ -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( diff --git a/anki/models.py b/anki/models.py index 0499d0371..528b9b602 100644 --- a/anki/models.py +++ b/anki/models.py @@ -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 diff --git a/anki/stdmodels.py b/anki/stdmodels.py index eb3931607..cde9e4da2 100644 --- a/anki/stdmodels.py +++ b/anki/stdmodels.py @@ -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; diff --git a/anki/storage.py b/anki/storage.py index a646f1f5f..82db8aa0e 100644 --- a/anki/storage.py +++ b/anki/storage.py @@ -2,9 +2,9 @@ # Copyright: Damien Elmes # 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 ######################################################################