mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add a 'test' function to templates. Analogous to lookup, but inserts plain text instead of a template.
This commit is contained in:
parent
67c7555fd0
commit
ad69ef985a
@ -23,6 +23,12 @@ class TemplateFormatter(string.Formatter):
|
|||||||
else:
|
else:
|
||||||
return self.vformat('{'+field_not_set.strip()+'}', [], self.kwargs)
|
return self.vformat('{'+field_not_set.strip()+'}', [], self.kwargs)
|
||||||
|
|
||||||
|
def _test(self, val, value_if_set, value_not_set):
|
||||||
|
if val:
|
||||||
|
return value_if_set
|
||||||
|
else:
|
||||||
|
return value_not_set
|
||||||
|
|
||||||
def _ifempty(self, val, value_if_empty):
|
def _ifempty(self, val, value_if_empty):
|
||||||
if val:
|
if val:
|
||||||
return val
|
return val
|
||||||
@ -45,6 +51,7 @@ class TemplateFormatter(string.Formatter):
|
|||||||
'ifempty' : (1, _ifempty),
|
'ifempty' : (1, _ifempty),
|
||||||
'lookup' : (2, _lookup),
|
'lookup' : (2, _lookup),
|
||||||
'shorten' : (3, _shorten),
|
'shorten' : (3, _shorten),
|
||||||
|
'test' : (2, _lookup),
|
||||||
}
|
}
|
||||||
|
|
||||||
format_string_re = re.compile(r'^(.*)\|(.*)\|(.*)$')
|
format_string_re = re.compile(r'^(.*)\|(.*)\|(.*)$')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user