mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Improvements to template function identifier_in_list()
This commit is contained in:
parent
dcd5978707
commit
942bceaa61
@ -558,6 +558,7 @@ In `GPM` the functions described in `Single Function Mode` all require an additi
|
||||
* ``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 the empty string.
|
||||
* ``has_extra_files([pattern])`` -- returns the count of extra files, otherwise '' (the empty string). If the optional parameter ``pattern`` (a regular expression) is supplied then the list is filtered to files that match ``pattern`` before the files are counted. The pattern match is case insensitive. See also the functions ``extra_file_names()``, ``extra_file_size()`` and ``extra_file_modtime()``. This function can be used only in the GUI.
|
||||
* ``identifier_in_list(val, id_name [, found_val, not_found_val])`` -- treat ``val`` as a list of identifiers separated by commas. An identifier has the format ``id_name:value``. The ``id_name`` parameter is the id_name text to search for, either ``id_name`` or ``id_name:regexp``. The first case matches if there is any identifier matching that id_name. The second case matches if id_name matches an identifier and the regexp matches the identifier's value. If ``found_val`` and ``not_found_val`` are provided then if there is a match then return ``found_val``, otherwise return ``not_found_val``. If ``found_val`` and ``not_found_val`` are not provided then if there is a match then return the ``indentfier:value`` pair, otherwise 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 a comma-separated list of named marks. Returns ``''`` (the empty string) if the book is not marked. This function works only in the GUI.
|
||||
* ``language_codes(lang_strings)`` -- return the `language codes <https://www.loc.gov/standards/iso639-2/php/code_list.php>`_ for the language names passed in `lang_strings`. The strings must be in the language of the current locale. ``Lang_strings`` is a comma-separated list.
|
||||
* ``list_contains(value, separator, [ pattern, found_val, ]* not_found_val)`` -- (Alias of ``in_list``) Interpreting the value as a list of items separated by ``separator``, evaluate the ``pattern`` against each value in the list. If the ``pattern`` matches any value then return ``found_val``, otherwise return ``not_found_val``. The ``pattern`` and ``found_value`` can be repeated as many times as desired, permitting returning different values depending on the search. The patterns are checked in order. The first match is returned. Aliases: ``in_list()``, ``list_contains()``
|
||||
|
@ -796,28 +796,40 @@ class BuiltinStrInList(BuiltinFormatterFunction):
|
||||
|
||||
class BuiltinIdentifierInList(BuiltinFormatterFunction):
|
||||
name = 'identifier_in_list'
|
||||
arg_count = 4
|
||||
arg_count = -1
|
||||
category = 'List lookup'
|
||||
__doc__ = doc = _('identifier_in_list(val, id, found_val, not_found_val) -- '
|
||||
'treat val as a list of identifiers separated by commas, '
|
||||
'comparing the string against each value in the list. An identifier '
|
||||
'has the format "identifier:value". The id parameter should be '
|
||||
'either "id" or "id:regexp". The first case matches if there is any '
|
||||
'identifier with that id. The second case matches if the regexp '
|
||||
'matches the identifier\'s value. If there is a match, '
|
||||
'return found_val, otherwise return not_found_val.')
|
||||
__doc__ = doc = _('identifier_in_list(val, id_name [, found_val, not_found_val]) -- '
|
||||
'treat val as a list of identifiers separated by commas. An identifier '
|
||||
'has the format "id_name:value". The id_name parameter is the id_name '
|
||||
'text to search for, either "id_name" or "id_name:regexp". The first case '
|
||||
'matches if there is any identifier matching that id_name. The second '
|
||||
'case matches if id_name matches an identifier and the regexp '
|
||||
'matches the identifier\'s value. If found_val and not_found_val '
|
||||
'are provided then if there is a match then return found_val, otherwise '
|
||||
'return not_found_val. If found_val and not_found_val are not '
|
||||
'provided then if there is a match then return the indentfier:value '
|
||||
'pair, otherwise the empty string.')
|
||||
|
||||
def evaluate(self, formatter, kwargs, mi, locals, val, ident, *args):
|
||||
if len(args) == 0:
|
||||
fv_is_id = True
|
||||
nfv = ''
|
||||
elif len(args) == 2:
|
||||
fv_is_id = False
|
||||
fv = args[0]
|
||||
nfv = args[1]
|
||||
else:
|
||||
raise ValueError(_("{} requires 2 or 4 arguments").format(self.name))
|
||||
|
||||
def evaluate(self, formatter, kwargs, mi, locals, val, ident, fv, nfv):
|
||||
l = [v.strip() for v in val.split(',') if v.strip()]
|
||||
(id, _, regexp) = ident.partition(':')
|
||||
if not id:
|
||||
(id_, _, regexp) = ident.partition(':')
|
||||
if not id_:
|
||||
return nfv
|
||||
id += ':'
|
||||
if l:
|
||||
for v in l:
|
||||
if v.startswith(id):
|
||||
if not regexp or re.search(regexp, v[len(id):], flags=re.I):
|
||||
return fv
|
||||
for candidate in l:
|
||||
i, _, v = candidate.partition(':')
|
||||
if v and i == id_:
|
||||
if not regexp or re.search(regexp, v, flags=re.I):
|
||||
return candidate if fv_is_id else fv
|
||||
return nfv
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user