diff --git a/manual/template_lang.rst b/manual/template_lang.rst index 78878829c3..c18f95b4c1 100644 --- a/manual/template_lang.rst +++ b/manual/template_lang.rst @@ -579,6 +579,19 @@ In `GPM` the functions described in `Single Function Mode` all require an additi rof; ans +* ``get_note(field_name, field_value, plain_text)`` -- fetch the note for field 'field_name' with value 'field_value'. If `plain_text` is empty, return the note's HTML including images. If `plain_text` is `1` (or `'1'`), return the note's plain text. If the note doesn't exist, return the empty string in both cases. Example: + + * Return the HTML of the note attached to the tag `Fiction`:: + + program: + get_note('tags', 'Fiction', '') + + * Return the plain text of the note attached to the author `Isaac Asimov`:: + + program: + get_note('authors', 'Isaac Asimov', 1) + + * ``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 ``identifier:value`` pair, otherwise the empty string (``''``). diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index 6666b01283..6350b9976d 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -2576,43 +2576,45 @@ class BuiltinGetNote(BuiltinFormatterFunction): category = 'Template database functions' __doc__ = doc = _("get_note(field_name, field_value, plain_text) -- fetch the " "note for field 'field_name' with value 'field_value'. If " - "'plain_text' is empty, return the note's HTML. If 'plain_text' " - "is non-empty, return the note's plain text. If the note " - "doesn't exist, return '' in both cases. Example: " + "'plain_text' is empty, return the note's HTML including" + "images. If 'plain_text' is 1 (or '1'), return the " + "note's plain text. If the note doesn't exist, return the " + "empty string in both cases. Example: " "get_note('tags', 'Fiction', '') returns the HTML of the " "note attached to the tag 'Fiction'.") def evaluate(self, formatter, kwargs, mi, locals, field_name, field_value, plain_text): db = self.get_database(mi).new_api try: - note = '' + note = None item_id = db.get_item_id(field_name, field_value) if item_id is not None: - note_data = db.notes_data_for(field_name, item_id) - if note_data is not None: + note = db.notes_data_for(field_name, item_id) + if note is not None: if plain_text == '1': - return note['searchable_text'].partition('\n')[2] - # Return the full HTML of the note, including all images as - # data: URLs. Reason: non-exported note html contains - # "calres://" URLs for images. These images won't render - # outside the context of the library where the note "lives". - # For example, they don't work in book jackets and book - # details from a different library. They also don't work in - # tooltips. + note = note['searchable_text'].partition('\n')[2] + else: + # Return the full HTML of the note, including all images + # as data: URLs. Reason: non-exported note html contains + # "calres://" URLs for images. These images won't render + # outside the context of the library where the note + # "lives". For example, they don't work in book jackets + # and book details from a different library. They also + # don't work in tooltips. - # This code depends on the note being wrapped in
tags - # by parse_html. The body is changed to a