From c76c08069e6040aa8432a96e0d7bc60a321a6445 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 18 Nov 2011 02:50:47 +0900 Subject: [PATCH] change template replacement behaviour Previously {{field}} wrapped the field in a span with the field's font properties. This wasn't obvious, and caused frequent problems with people trying to combine field and template text, or use field content in dictionary links. Now that AnkiWeb has a wizard for configuring the front & back layout, we can just put the formatting in the template instead. --- anki/deck.py | 5 ----- anki/template/template.py | 6 +++--- anki/upgrade.py | 35 +++++++++++++++++++++++++++++++++++ tests/test_upgrade.py | 1 - 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/anki/deck.py b/anki/deck.py index 07bcbb244..de9cb80d5 100644 --- a/anki/deck.py +++ b/anki/deck.py @@ -413,11 +413,6 @@ select id from facts where id in %s and id not in (select fid from cards)""" % model = self.models.get(data[2]) for (name, (idx, conf)) in self.models.fieldMap(model).items(): fields[name] = flist[idx] - if fields[name]: - fields[name] = '%s' % ( - hexifyID(data[2]), hexifyID(idx), fields[name]) - else: - fields[name] = "" fields['Tags'] = data[5] fields['Model'] = model['name'] fields['Group'] = self.groups.name(data[3]) diff --git a/anki/template/template.py b/anki/template/template.py index bc433e1d6..a165b4799 100644 --- a/anki/template/template.py +++ b/anki/template/template.py @@ -135,13 +135,13 @@ class Template(object): return template + # {{{ functions just like {{ in anki @modifier('{') def render_tag(self, tag_name, context): - """Given a tag name and context, finds, escapes, and renders the tag.""" raw = get_or_attr(context, tag_name, '') if not raw and raw is not 0: return '' - return re.sub("^(.*)", "\\1", raw) + return raw @modifier('!') def render_comment(self, tag_name=None, context=None): @@ -188,7 +188,7 @@ class Template(object): ans = [""+a[0]+"" for a in ans] ans = ", ".join(ans) # but we want to preserve the outer field styling - return re.sub("(^)(.*)", "\\1"+ans+"", txt) + return ans # and display other clozes normally return re.sub(reg%".*?", "\\1", txt) diff --git a/anki/upgrade.py b/anki/upgrade.py index 9a5ddd5de..ee3d14af9 100644 --- a/anki/upgrade.py +++ b/anki/upgrade.py @@ -416,6 +416,39 @@ order by ordinal""", mid)): tmpls.append(conf) return tmpls + # Template upgrading + ###################################################################### + # {{field}} no longer inserts an implicit span, so we make the span + # explicit on upgrade. + def _upgradeTemplates(self): + d = self.deck + for m in d.models.all(): + # cache field styles + styles = {} + for f in m['flds']: + attrs = [ + "font-family:%s" % f['font'], + "font-size:%spx" % f['qsize'], + "color:%s" % f['qcol']] + if f['rtl']: + attrs.append("direction:rtl;unicode-bidi:embed") + if f['pre']: + attrs.append("white-space:pre-wrap") + styles[f['name']] = '\n{{%s}}\n' % ( + ";".join(attrs), f['name']) + # then for each template + for t in m['tmpls']: + def repl(match): + field = match.group(1) + if field in styles: + return styles[field] + # special or non-existant field; leave alone + return match.group(0) + for k in 'qfmt', 'afmt': + t[k] = re.sub("(?:^|[^{]){{([^{}]+)?}}", repl, t[k]) + # save model + d.models.save(m) + # Media references ###################################################################### # In 2.0 we drop support for media and latex references in the template, @@ -519,6 +552,8 @@ and ord = ? limit 1""", m['id'], t['ord']): self._removeInactive() # rewrite media references in card template self._rewriteMediaRefs() + # template handling has changed + self._upgradeTemplates() # regenerate css, and set new card order for m in deck.models.all(): m['newOrder'] = deck.conf['oldNewOrder'] diff --git a/tests/test_upgrade.py b/tests/test_upgrade.py index 353bc95e0..6f6df08ef 100644 --- a/tests/test_upgrade.py +++ b/tests/test_upgrade.py @@ -19,7 +19,6 @@ def test_upgrade(): dst = getUpgradeDeckPath() csum = checksum(open(dst).read()) u = Upgrader() - print "upgrade to", dst deck = u.upgrade(dst) # src file must not have changed assert csum == checksum(open(dst).read())