From 7e3597fb8e1ed087261b9e93643890ae8e1b3587 Mon Sep 17 00:00:00 2001 From: Julien Baley Date: Sat, 15 Feb 2014 13:59:58 +0000 Subject: [PATCH] Adding support for chained modifiers. One can now write e.g. {{cloze:text:Field}} or {{text:cloze:Field}}, it's order independent. --- anki/collection.py | 6 ++--- anki/models.py | 2 +- anki/template/template.py | 57 ++++++++++++++++++++------------------- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/anki/collection.py b/anki/collection.py index 1991ff895..6b0882e00 100644 --- a/anki/collection.py +++ b/anki/collection.py @@ -510,13 +510,11 @@ where c.nid = n.id and c.id in %s group by nid""" % ids2str(cids)): afmt = afmt or template['afmt'] for (type, format) in (("q", qfmt), ("a", afmt)): if type == "q": - format = format.replace("{{cloze:", "{{cq:%d:" % ( - data[4]+1)) + format = re.sub("{{(.*?)cloze:", r"{{\1cq-%d:" % (data[4]+1), format) format = format.replace("<%cloze:", "<%%cq:%d:" % ( data[4]+1)) else: - format = format.replace("{{cloze:", "{{ca:%d:" % ( - data[4]+1)) + format = re.sub("{{(.*?)cloze:", r"{{\1ca-%d:" % (data[4]+1), format) format = format.replace("<%cloze:", "<%%ca:%d:" % ( data[4]+1)) fields['FrontSide'] = stripSounds(d['q']) diff --git a/anki/models.py b/anki/models.py index 8ad5b158d..599a10c92 100644 --- a/anki/models.py +++ b/anki/models.py @@ -569,7 +569,7 @@ select id from notes where mid = ?)""" % " ".join(map), sflds = splitFields(flds) map = self.fieldMap(m) ords = set() - matches = re.findall("{{cloze:(.+?)}}", m['tmpls'][0]['qfmt']) + matches = re.findall("{{[^}]*?cloze:(?:.*?:)*(.+?)}}", m['tmpls'][0]['qfmt']) matches += re.findall("<%cloze:(.+?)%>", m['tmpls'][0]['qfmt']) for fname in matches: if fname not in map: diff --git a/anki/template/template.py b/anki/template/template.py index bdddb2422..7a091d91d 100644 --- a/anki/template/template.py +++ b/anki/template/template.py @@ -158,40 +158,41 @@ class Template(object): return txt # field modifiers - parts = tag_name.split(':',2) + parts = tag_name.split(':') extra = None if len(parts) == 1 or parts[0] == '': return '{unknown field %s}' % tag_name - elif len(parts) == 2: - (mod, tag) = parts - elif len(parts) == 3: - (mod, extra, tag) = parts + else: + mods, tag = parts[:-1], parts[-1] #py3k has *mods, tag = parts txt = get_or_attr(context, tag) - - # built-in modifiers - if mod == 'text': - # strip html - if txt: - return stripHTML(txt) - return "" - elif mod == 'type': - # type answer field; convert it to [[type:...]] for the gui code - # to process - return "[[%s]]" % tag_name - elif mod == 'cq' or mod == 'ca': - # cloze deletion - if txt and extra: - return self.clozeText(txt, extra, mod[1]) + + #Since 'text:' and oths mods can affect html on which Anki relies to + #process Clozes and Types, we need to make sure cloze/type are always + #treated after all the other mods, regardless of how they're specified + #in the template, so that {{cloze:text: == {{text:cloze: + mods.sort(key=lambda s: s.startswith("cq-") or s.startswith("ca-") or s=="type") + + for mod in mods: + # built-in modifiers + if mod == 'text': + # strip html + txt = stripHTML(txt) if txt else "" + elif mod == 'type': + # type answer field; convert it to [[type:...]] for the gui code + # to process + txt = "[[%s]]" % tag_name + elif mod.startswith('cq-') or mod.startswith('ca-'): + # cloze deletion + mod, extra = mod.split("-") + txt = self.clozeText(txt, extra, mod[1]) if txt and extra else "" else: - return "" - else: - # hook-based field modifier - txt = runFilter('fmod_' + mod, txt or '', extra, context, - tag, tag_name); - if txt is None: - return '{unknown field %s}' % tag_name - return txt + # hook-based field modifier + txt = runFilter('fmod_' + mod, txt or '', extra, context, + tag, tag_name); + if txt is None: + return '{unknown field %s}' % tag_name + return txt def clozeText(self, txt, ord, type): reg = clozeReg