mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Merge branch 'master' of https://github.com/cbhaley/calibre
This commit is contained in:
commit
825c6e69fa
@ -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 (``''``).
|
||||
|
@ -2576,35 +2576,37 @@ 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
|
||||
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.
|
||||
# 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 <body> tags
|
||||
# by parse_html. The body is changed to a <div>. That means
|
||||
# we often end up with <div><div> or some such, but that is
|
||||
# OK
|
||||
root = parse_html(note_data['doc'])
|
||||
# This code depends on the note being wrapped in <body>
|
||||
# tags by parse_html. The body is changed to a <div>.
|
||||
# That means we often end up with <div><div> or some
|
||||
# such, but that is OK
|
||||
root = parse_html(note['doc'])
|
||||
# There should be only one <body>
|
||||
root = root.xpath('//body')[0]
|
||||
# Change the body to a div
|
||||
@ -2612,7 +2614,7 @@ class BuiltinGetNote(BuiltinFormatterFunction):
|
||||
# Expand all the resources in the note
|
||||
root = expand_note_resources(root, db.get_notes_resource)
|
||||
note = html.tostring(root, encoding='unicode')
|
||||
return note
|
||||
return '' if note is None else note
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
raise ValueError(e)
|
||||
|
Loading…
x
Reference in New Issue
Block a user