From 1af28bd3bae30744d41a704eebb77b5aa7d0fcf7 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Wed, 17 Feb 2021 14:43:42 +0000 Subject: [PATCH] Add template function 'is_marked()' --- manual/template_lang.rst | 3 +++ src/calibre/utils/formatter_functions.py | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/manual/template_lang.rst b/manual/template_lang.rst index 2e6bf56bc1..b24d49fbb8 100644 --- a/manual/template_lang.rst +++ b/manual/template_lang.rst @@ -460,6 +460,9 @@ parameters can be statements (sequences of expressions). Note that the definitiv * ``fractional_part(x)`` -- returns the value after the decimal point. For example, ``fractional_part(3.14)`` returns ``0.14``. Throws an exception if ``x`` is not a number. * ``has_cover()`` -- return ``Yes`` if the book has a cover, otherwise return the empty string. + * ``is_marked()`` -- check whether the book is `marked` in calibre. If it is then return the value of the mark, + either `true` (lower case) or the comma-separated list of named marks. Returns '' (the empty string) if the book is not marked. + This function works only in the GUI. * ``list_contains(separator, pattern, found_val, ..., not_found_val)`` -- (Alias of ``in_list``) Interpret the field as a list of items separated by `separator`, evaluating the `pattern` against each value in the list. If the `pattern` matches a value, return `found_val`, otherwise return `not_found_val`. The `pattern` and `found_value` can be repeated as many times as desired, diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index 54c7ba6327..71ba94e50a 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -1228,6 +1228,27 @@ class BuiltinAnnotationCount(BuiltinFormatterFunction): return _('This function can be used only in the GUI') +class BuiltinIsMarked(BuiltinFormatterFunction): + name = 'is_marked' + arg_count = 0 + category = 'Get values from metadata' + __doc__ = doc = _("is_marked() -- check whether the book is 'marked' in " + "calibre. If it is then return the value of the mark, " + "either 'true' or the comma-separated list of named " + "marks. Returns '' if the book is not marked.") + + def evaluate(self, formatter, kwargs, mi, locals): + if hasattr(mi, '_proxy_metadata'): + try: + from calibre.gui2.ui import get_gui + c = get_gui().current_db.data.get_marked(mi.id) + return c if c else '' + except: + return _('Failed to get marked status') + return '' + return _('This function can be used only in the GUI') + + class BuiltinSeriesSort(BuiltinFormatterFunction): name = 'series_sort' arg_count = 0 @@ -1925,7 +1946,7 @@ _formatter_builtins = [ BuiltinGlobals(), BuiltinHasCover(), BuiltinHumanReadable(), BuiltinIdentifierInList(), BuiltinIfempty(), BuiltinLanguageCodes(), BuiltinLanguageStrings(), - BuiltinInList(), BuiltinListDifference(), BuiltinListEquals(), + BuiltinInList(), BuiltinIsMarked(), BuiltinListDifference(), BuiltinListEquals(), BuiltinListIntersection(), BuiltinListitem(), BuiltinListRe(), BuiltinListReGroup(), BuiltinListSort(), BuiltinListSplit(), BuiltinListUnion(), BuiltinLookup(),