tweak template upgrading, replace repeated spaces with non-breaking ones

This commit is contained in:
Damien Elmes 2011-12-17 22:01:50 +09:00
parent 9e35e4acf2
commit e610eeeb42
2 changed files with 36 additions and 25 deletions

View file

@ -138,7 +138,7 @@ class Template(object):
# {{{ functions just like {{ in anki # {{{ functions just like {{ in anki
@modifier('{') @modifier('{')
def render_tag(self, tag_name, context): def render_tag(self, tag_name, context):
return render_unescaped(tag_name, context) return self.render_unescaped(tag_name, context)
@modifier('!') @modifier('!')
def render_comment(self, tag_name=None, context=None): def render_comment(self, tag_name=None, context=None):

View file

@ -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"): "select factId, ordinal, value from fields order by factId, ordinal"):
if fid not in fields: if fid not in fields:
fields[fid] = [] fields[fid] = []
val = self._mungeField(val)
fields[fid].append((ord, val)) fields[fid].append((ord, val))
# build insert data and transform ids, and minimize qt's # build insert data and transform ids, and minimize qt's
# bold/italics/underline cruft. # bold/italics/underline cruft.
@ -415,6 +416,16 @@ order by ordinal""", mid)):
tmpls.append(conf) tmpls.append(conf)
return tmpls 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 # Template upgrading
###################################################################### ######################################################################
# - {{field}} no longer inserts an implicit span, so we make the span # - {{field}} no longer inserts an implicit span, so we make the span
@ -426,17 +437,18 @@ order by ordinal""", mid)):
# cache field styles # cache field styles
styles = {} styles = {}
for f in m['flds']: for f in m['flds']:
attrs = [ attrs = []
"font-family:%s" % f['font'], if f['font'].lower() != 'arial':
"font-size:%spx" % f['qsize'], attrs.append("font-family: %s" % f['font'])
"color:%s" % f['qcol'], if f['qsize'] != 20:
"white-space:pre-wrap", attrs.append("font-size: %spx" % f['qsize'])
] if f['qcol'] not in ("black", "#000"):
attrs.append("color: %s" % f['qcol'])
if f['rtl']: if f['rtl']:
attrs.append("direction:rtl; unicode-bidi:embed") attrs.append("direction: rtl; unicode-bidi: embed")
attrs.append() if attrs:
styles[f['name']] = '<span style="%s">{{%s}}</span>' % ( styles[f['name']] = '<span style="%s">{{%s}}</span>' % (
"; ".join(attrs), f['name']) "; ".join(attrs), f['name'])
# obsolete # obsolete
del f['qcol'] del f['qcol']
del f['qsize'] del f['qsize']
@ -451,20 +463,19 @@ order by ordinal""", mid)):
for k in 'qfmt', 'afmt': for k in 'qfmt', 'afmt':
# replace old field references # replace old field references
t[k] = re.sub("(^|[^{]){{([^{}]+)?}}", repl, t[k]) t[k] = re.sub("(^|[^{]){{([^{}]+)?}}", repl, t[k])
# then template properties. # then strip extra {}s from other fields
if t['bg'].lower() == "#ffffff": t[k] = t[k].replace("{{{", "{{").replace("}}}", "}}")
# a bit more intuitive default if "{{{" in t[k]:
bg = "white" print t[k]
else: raise Exception()
bg = t['bg'] # adjust css
t[k] = '''\ if t['bg'].lower() == "#ffffff":
<style> # a bit more intuitive default
.card { bg = "white"
text-align:%s; else:
background-color:%s; bg = t['bg']
} t['css'] = t['css'].replace("white", bg).replace(
</style>\n\n%s''' % (("center", "left", "right")[t['align']], "center", ("center", "left", "right")[t['align']])
bg, t[k])
# remove obsolete # remove obsolete
del t['bg'] del t['bg']
del t['align'] del t['align']