detect renamed cloze fields in note type preview

and cloze fields that is also other special field type
such as '{{cloze:hint:Text}}'
This commit is contained in:
BlueGreenMagick 2020-04-06 14:33:46 +09:00
parent 484377b809
commit 12b69af9b2

View file

@ -1,6 +1,7 @@
# Copyright: Ankitects Pty Ltd and contributors # Copyright: Ankitects Pty Ltd and contributors
# 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 collections import collections
import re
from operator import itemgetter from operator import itemgetter
import aqt.clayout import aqt.clayout
@ -137,14 +138,21 @@ class Models(QDialog):
def _tmpNote(self): def _tmpNote(self):
self.mm.setCurrent(self.model) self.mm.setCurrent(self.model)
n = self.col.newNote(forDeck=False) n = self.col.newNote(forDeck=False)
for name in list(n.keys()): field_names = list(n.keys())
for name in field_names:
n[name] = "(" + name + ")" n[name] = "(" + name + ")"
try:
if "{{cloze:Text}}" in self.model["tmpls"][0]["qfmt"]: cloze_re = re.compile(r"{{(?:[^}:]*:)*cloze:(?:[^}:]*:)*([^}]+)}}")
n["Text"] = _("This is a {{c1::sample}} cloze deletion.") q_template = self.model["tmpls"][0]["qfmt"]
except: a_template = self.model["tmpls"][0]["afmt"]
# invalid cloze
pass used_cloze_fields = []
used_cloze_fields.extend(cloze_re.findall(q_template))
used_cloze_fields.extend(cloze_re.findall(a_template))
for field in used_cloze_fields:
if field in field_names:
n[field] = f"{field}: " + _("This is a {{c1::sample}} cloze deletion.")
return n return n
def onFields(self): def onFields(self):