From e19e08ecc4f920f38ba3b78cc6e0a6c84dc9b8e4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 20 Jan 2021 20:05:28 +0530 Subject: [PATCH] Insert metadata: Allow showing identifiers such as ISBN in the jacket page template --- resources/jacket/template.xhtml | 7 ++++ src/calibre/ebooks/oeb/transforms/jacket.py | 42 +++++++++++++++++---- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/resources/jacket/template.xhtml b/resources/jacket/template.xhtml index 2421f5d2c9..9492965940 100644 --- a/resources/jacket/template.xhtml +++ b/resources/jacket/template.xhtml @@ -39,6 +39,13 @@ {searchable_tags} +
diff --git a/src/calibre/ebooks/oeb/transforms/jacket.py b/src/calibre/ebooks/oeb/transforms/jacket.py index 467ae51ef3..1d5290b3a7 100644 --- a/src/calibre/ebooks/oeb/transforms/jacket.py +++ b/src/calibre/ebooks/oeb/transforms/jacket.py @@ -6,19 +6,24 @@ __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import sys, os, re -from xml.sax.saxutils import escape +import os +import re +import sys from string import Formatter +from xml.sax.saxutils import escape -from calibre import guess_type, strftime +from calibre import guess_type, prepare_string_for_xml, strftime from calibre.constants import iswindows -from calibre.ebooks.oeb.base import XPath, XHTML_NS, XHTML, xml2text, urldefrag, urlnormalize -from calibre.library.comments import comments_to_html, markdown -from calibre.utils.date import is_date_undefined, as_local_time -from calibre.utils.icu import sort_key from calibre.ebooks.chardet import strip_encoding_declarations from calibre.ebooks.metadata import fmt_sidx, rating_to_stars -from polyglot.builtins import unicode_type, map +from calibre.ebooks.metadata.sources.identify import urls_from_identifiers +from calibre.ebooks.oeb.base import ( + XHTML, XHTML_NS, XPath, urldefrag, urlnormalize, xml2text +) +from calibre.library.comments import comments_to_html, markdown +from calibre.utils.date import as_local_time, is_date_undefined +from calibre.utils.icu import sort_key +from polyglot.builtins import map, unicode_type JACKET_XPATH = '//h:meta[@name="calibre-content" and @content="jacket"]' @@ -235,6 +240,24 @@ class Attributes: return 'none' +class Identifiers: + + def __init__(self, idents): + self.identifiers = idents or {} + self.display = Attributes() + for k in self.identifiers: + setattr(self.display, k, 'initial') + links = [] + for x in urls_from_identifiers(self.identifiers): + name, id_typ, id_val, url = (prepare_string_for_xml(e, True) for e in x) + links.append(f'{name}') + self.links = ', '.join(links) + self.display.links = 'initial' if self.links else 'none' + + def __getattr__(self, name): + return self.identifiers.get(name, '') + + def render_jacket(mi, output_profile, alt_title=_('Unknown'), alt_tags=[], alt_comments='', alt_publisher='', rescale_fonts=False, alt_authors=None): @@ -291,6 +314,7 @@ def render_jacket(mi, output_profile, display = Attributes() args = dict(xmlns=XHTML_NS, title_str=title_str, + identifiers=Identifiers(mi.identifiers), css=css, title=title, author=author, @@ -352,6 +376,8 @@ def render_jacket(mi, output_profile, for k, v in has_data.items(): setattr(display, k, 'initial' if v else 'none') display.title = 'initial' + if mi.identifiers: + display.identifiers = 'initial' formatter = SafeFormatter() generated_html = formatter.format(template, **args)