From e610eeeb420c8a36a66d9280e0ec37a3284a4de0 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 17 Dec 2011 22:01:50 +0900 Subject: [PATCH] tweak template upgrading, replace repeated spaces with non-breaking ones --- anki/template/template.py | 2 +- anki/upgrade.py | 59 +++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/anki/template/template.py b/anki/template/template.py index 7135d99a6..3eb306533 100644 --- a/anki/template/template.py +++ b/anki/template/template.py @@ -138,7 +138,7 @@ class Template(object): # {{{ functions just like {{ in anki @modifier('{') def render_tag(self, tag_name, context): - return render_unescaped(tag_name, context) + return self.render_unescaped(tag_name, context) @modifier('!') def render_comment(self, tag_name=None, context=None): diff --git a/anki/upgrade.py b/anki/upgrade.py index 64fe4c98a..f6926f981 100644 --- a/anki/upgrade.py +++ b/anki/upgrade.py @@ -157,6 +157,7 @@ select id, id, modelId, 1, cast(created*1000 as int), cast(modified as int), "select factId, ordinal, value from fields order by factId, ordinal"): if fid not in fields: fields[fid] = [] + val = self._mungeField(val) fields[fid].append((ord, val)) # build insert data and transform ids, and minimize qt's # bold/italics/underline cruft. @@ -415,6 +416,16 @@ order by ordinal""", mid)): tmpls.append(conf) return tmpls + # Field munging + ###################################################################### + + def _mungeField(self, val): + # we no longer wrap fields in white-space: pre-wrap, so we need to + # convert previous whitespace into non-breaking spaces + def repl(match): + return match.group(1).replace(" ", " ") + return re.sub("( +)", repl, val) + # Template upgrading ###################################################################### # - {{field}} no longer inserts an implicit span, so we make the span @@ -426,17 +437,18 @@ order by ordinal""", mid)): # cache field styles styles = {} for f in m['flds']: - attrs = [ - "font-family:%s" % f['font'], - "font-size:%spx" % f['qsize'], - "color:%s" % f['qcol'], - "white-space:pre-wrap", - ] + attrs = [] + if f['font'].lower() != 'arial': + attrs.append("font-family: %s" % f['font']) + if f['qsize'] != 20: + attrs.append("font-size: %spx" % f['qsize']) + if f['qcol'] not in ("black", "#000"): + attrs.append("color: %s" % f['qcol']) if f['rtl']: - attrs.append("direction:rtl; unicode-bidi:embed") - attrs.append() - styles[f['name']] = '{{%s}}' % ( - "; ".join(attrs), f['name']) + attrs.append("direction: rtl; unicode-bidi: embed") + if attrs: + styles[f['name']] = '{{%s}}' % ( + "; ".join(attrs), f['name']) # obsolete del f['qcol'] del f['qsize'] @@ -451,20 +463,19 @@ order by ordinal""", mid)): for k in 'qfmt', 'afmt': # replace old field references t[k] = re.sub("(^|[^{]){{([^{}]+)?}}", repl, t[k]) - # then template properties. - if t['bg'].lower() == "#ffffff": - # a bit more intuitive default - bg = "white" - else: - bg = t['bg'] - t[k] = '''\ -\n\n%s''' % (("center", "left", "right")[t['align']], - bg, t[k]) + # then strip extra {}s from other fields + t[k] = t[k].replace("{{{", "{{").replace("}}}", "}}") + if "{{{" in t[k]: + print t[k] + raise Exception() + # adjust css + if t['bg'].lower() == "#ffffff": + # a bit more intuitive default + bg = "white" + else: + bg = t['bg'] + t['css'] = t['css'].replace("white", bg).replace( + "center", ("center", "left", "right")[t['align']]) # remove obsolete del t['bg'] del t['align']