From ad69ef985a04b5485550f83661ee0b56723605f1 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Fri, 24 Sep 2010 12:27:39 +0100 Subject: [PATCH] Add a 'test' function to templates. Analogous to lookup, but inserts plain text instead of a template. --- src/calibre/utils/formatter.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/calibre/utils/formatter.py b/src/calibre/utils/formatter.py index a98f0e7f45..5c5893576c 100644 --- a/src/calibre/utils/formatter.py +++ b/src/calibre/utils/formatter.py @@ -23,6 +23,12 @@ class TemplateFormatter(string.Formatter): else: 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): if val: return val @@ -45,6 +51,7 @@ class TemplateFormatter(string.Formatter): 'ifempty' : (1, _ifempty), 'lookup' : (2, _lookup), 'shorten' : (3, _shorten), + 'test' : (2, _lookup), } format_string_re = re.compile(r'^(.*)\|(.*)\|(.*)$')