mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
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:
parent
41fa9a9896
commit
5cbe93b63d
4 changed files with 32 additions and 14 deletions
|
@ -451,10 +451,7 @@ select id from notes where id in %s and id not in (select nid from cards)""" %
|
||||||
flist = splitFields(data[6])
|
flist = splitFields(data[6])
|
||||||
fields = {}
|
fields = {}
|
||||||
model = self.models.get(data[2])
|
model = self.models.get(data[2])
|
||||||
firstName = None
|
|
||||||
for (name, (idx, conf)) in self.models.fieldMap(model).items():
|
for (name, (idx, conf)) in self.models.fieldMap(model).items():
|
||||||
if idx == 0:
|
|
||||||
firstName = name
|
|
||||||
fields[name] = flist[idx]
|
fields[name] = flist[idx]
|
||||||
fields['Tags'] = data[5]
|
fields['Tags'] = data[5]
|
||||||
fields['Type'] = model['name']
|
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])
|
d = dict(id=data[0])
|
||||||
for (type, format) in (("q", template['qfmt']), ("a", template['afmt'])):
|
for (type, format) in (("q", template['qfmt']), ("a", template['afmt'])):
|
||||||
if type == "q":
|
if type == "q":
|
||||||
format = format.replace("{{Cloze}}", "{{cq:%d:%s}}" % (
|
format = format.replace("{{cloze:", "{{cq:%d:" % (
|
||||||
data[4]+1, firstName))
|
data[4]+1))
|
||||||
else:
|
else:
|
||||||
format = format.replace("{{Cloze}}", "{{ca:%d:%s}}" % (
|
format = format.replace("{{cloze:", "{{ca:%d:" % (
|
||||||
data[4]+1, firstName))
|
data[4]+1))
|
||||||
fields = runFilter("mungeFields", fields, model, data, self)
|
fields = runFilter("mungeFields", fields, model, data, self)
|
||||||
html = anki.template.render(format, fields)
|
html = anki.template.render(format, fields)
|
||||||
d[type] = runFilter(
|
d[type] = runFilter(
|
||||||
|
|
|
@ -509,7 +509,7 @@ select id from notes where mid = ?)""" % " ".join(map),
|
||||||
|
|
||||||
def _availClozeOrds(self, m, flds):
|
def _availClozeOrds(self, m, flds):
|
||||||
ret = [int(m)-1 for m in re.findall(
|
ret = [int(m)-1 for m in re.findall(
|
||||||
"{{c(\d)::[^}]*?}}", splitFields(flds)[0])]
|
"{{c(\d+)::[^}]*?}}", splitFields(flds)[0])]
|
||||||
return list(set(ret))
|
return list(set(ret))
|
||||||
|
|
||||||
# Sync handling
|
# Sync handling
|
||||||
|
|
|
@ -33,12 +33,13 @@ def addClozeModel(col):
|
||||||
mm = col.models
|
mm = col.models
|
||||||
m = mm.new(_("Cloze"))
|
m = mm.new(_("Cloze"))
|
||||||
m['type'] = MODEL_CLOZE
|
m['type'] = MODEL_CLOZE
|
||||||
fm = mm.newField(_("Text"))
|
txt = _("Text")
|
||||||
|
fm = mm.newField(txt)
|
||||||
mm.addField(m, fm)
|
mm.addField(m, fm)
|
||||||
fm = mm.newField(_("Extra"))
|
fm = mm.newField(_("Extra"))
|
||||||
mm.addField(m, fm)
|
mm.addField(m, fm)
|
||||||
t = mm.newTemplate(_("Cloze"))
|
t = mm.newTemplate(_("Cloze"))
|
||||||
fmt = "{{Cloze}}"
|
fmt = "{{cloze:%s}}" % txt
|
||||||
t['css'] += """
|
t['css'] += """
|
||||||
.cloze {
|
.cloze {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
|
|
|
@ -2,9 +2,9 @@
|
||||||
# Copyright: Damien Elmes <anki@ichi2.net>
|
# Copyright: Damien Elmes <anki@ichi2.net>
|
||||||
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
# 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.lang import _
|
||||||
from anki.utils import intTime
|
from anki.utils import intTime, ids2str
|
||||||
from anki.db import DB
|
from anki.db import DB
|
||||||
from anki.collection import _Collection
|
from anki.collection import _Collection
|
||||||
from anki.consts import *
|
from anki.consts import *
|
||||||
|
@ -81,11 +81,31 @@ def _upgrade(col, ver):
|
||||||
d['collapsed'] = False
|
d['collapsed'] = False
|
||||||
col.decks.save(d)
|
col.decks.save(d)
|
||||||
if ver < 4:
|
if ver < 4:
|
||||||
|
col.modSchema()
|
||||||
for m in col.models.all():
|
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
|
m['type'] = MODEL_STD
|
||||||
else:
|
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
|
# Creating a new collection
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
Loading…
Reference in a new issue