From a11ccd8598d3ba3cf34599647d58666b49038302 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Fri, 24 Sep 2010 17:20:26 +0100 Subject: [PATCH] Added 'contains' function to templates --- src/calibre/manual/template_lang.rst | 1 + src/calibre/utils/formatter.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/calibre/manual/template_lang.rst b/src/calibre/manual/template_lang.rst index 5f672c4989..0c3a87a157 100644 --- a/src/calibre/manual/template_lang.rst +++ b/src/calibre/manual/template_lang.rst @@ -108,6 +108,7 @@ The functions available are: * ``capitalize()`` -- return the value as capitalized. * ``ifempty(text)`` -- if the field is not empty, return the value of the field. Otherwise return `text`. * ``test(text if not empty, text if empty)`` -- return `text if not empty` if the field is not empty, otherwise return `text if empty`. + * ``contains(pattern, text if match, text if not match`` -- checks if field contains matches for the regular expression `pattern`. Returns `text if match` if matches are found, otherwise it returns `text if no match`. * ``shorten(left chars, middle text, right chars)`` -- Return a shortened version of the field, consisting of `left chars` characters from the beginning of the field, followed by `middle text`, followed by `right chars` characters from the end of the string. `Left chars` and `right chars` must be integers. For example, assume the title of the book is `Ancient English Laws in the Times of Ivanhoe`, and you want it to fit in a space of at most 15 characters. If you use ``{title:shorten(9,-,5)}``, the result will be `Ancient E-nhoe`. If the field's length is less than ``left chars`` + ``right chars`` + the length of ``middle text``, then the field will be used intact. For example, the title `The Dome` would not be changed. * ``lookup(field if not empty, field if empty)`` -- like test, except the arguments are field (metadata) names, not text. The value of the appropriate field will be fetched and used. Note that because composite columns are fields, you can use this function in one composite field to use the value of some other composite field. This is extremely useful when constructing variable save paths (more later). * ``re(pattern, replacement)`` -- return the field after applying the regular expression. All instances of `pattern` are replaced with `replacement`. As in all of |app|, these are python-compatible regular expressions. diff --git a/src/calibre/utils/formatter.py b/src/calibre/utils/formatter.py index c6bcaa1c3e..6fed4e157a 100644 --- a/src/calibre/utils/formatter.py +++ b/src/calibre/utils/formatter.py @@ -29,6 +29,15 @@ class TemplateFormatter(string.Formatter): else: return value_not_set + def _contains(self, val, test, value_if_present, value_if_not): + if re.search(test, val): + return value_if_present + else: + return value_if_not + + def _re(self, val, pattern, replacement): + return re.sub(pattern, replacement, val) + def _ifempty(self, val, value_if_empty): if val: return val @@ -43,14 +52,12 @@ class TemplateFormatter(string.Formatter): else: return val - def _re(self, val, pattern, replacement): - return re.sub(pattern, replacement, val) - functions = { 'uppercase' : (0, lambda s,x: x.upper()), 'lowercase' : (0, lambda s,x: x.lower()), 'titlecase' : (0, lambda s,x: x.title()), 'capitalize' : (0, lambda s,x: x.capitalize()), + 'contains' : (3, _contains), 'ifempty' : (1, _ifempty), 'lookup' : (2, _lookup), 're' : (2, _re),