From 3f8403ca84a9c9d3cb20568aac4418b806b51201 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 20 Jan 2021 19:35:07 +0530 Subject: [PATCH] Conversion: Insert metadata: Allow hiding entries in the jacket template when they are not present in the metadata. --- resources/jacket/template.xhtml | 4 +- src/calibre/ebooks/oeb/transforms/jacket.py | 44 +++++++++++++-------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/resources/jacket/template.xhtml b/resources/jacket/template.xhtml index d71499a2d7..2421f5d2c9 100644 --- a/resources/jacket/template.xhtml +++ b/resources/jacket/template.xhtml @@ -49,7 +49,9 @@ label #genre, you can add it to this template with _genre_label and _genre. Note that the # is replaced by an underscore. For example -
{_genre_label}: {_genre}
+
{_genre_label}: {_genre}
+ + If #genre is not set, then the entire
element will be hidden. -->
{comments}
diff --git a/src/calibre/ebooks/oeb/transforms/jacket.py b/src/calibre/ebooks/oeb/transforms/jacket.py index 145ecf366f..467ae51ef3 100644 --- a/src/calibre/ebooks/oeb/transforms/jacket.py +++ b/src/calibre/ebooks/oeb/transforms/jacket.py @@ -229,6 +229,12 @@ def postprocess_jacket(root, output_profile, has_data): extract_class('cbj_kindle_banner_hr') +class Attributes: + + def __getattr__(self, name): + return 'none' + + def render_jacket(mi, output_profile, alt_title=_('Unknown'), alt_tags=[], alt_comments='', alt_publisher='', rescale_fonts=False, alt_authors=None): @@ -282,20 +288,22 @@ def render_jacket(mi, output_profile, has_data = {} def generate_html(comments): + display = Attributes() args = dict(xmlns=XHTML_NS, - title_str=title_str, - css=css, - title=title, - author=author, - publisher=publisher, - pubdate_label=_('Published'), pubdate=pubdate, - series_label=ngettext('Series', 'Series', 1), series=series, - rating_label=_('Rating'), rating=rating, - tags_label=_('Tags'), tags=tags, - comments=comments, - footer='', - searchable_tags=' '.join(escape(t)+'ttt' for t in tags.tags_list), - ) + title_str=title_str, + css=css, + title=title, + author=author, + publisher=publisher, + pubdate_label=_('Published'), pubdate=pubdate, + series_label=ngettext('Series', 'Series', 1), series=series, + rating_label=_('Rating'), rating=rating, + tags_label=_('Tags'), tags=tags, + comments=comments, + footer='', + display=display, + searchable_tags=' '.join(escape(t)+'ttt' for t in tags.tags_list), + ) for key in mi.custom_field_keys(): m = mi.get_user_metadata(key, False) or {} try: @@ -322,6 +330,7 @@ def render_jacket(mi, output_profile, else: args[dkey] = escape(val) args[dkey+'_label'] = escape(display_name) + setattr(display, dkey, 'none' if mi.is_null(key) else 'initial') except Exception: # if the val (custom column contents) is None, don't add to args pass @@ -336,13 +345,16 @@ def render_jacket(mi, output_profile, # Don't change this unless you also change it in template.xhtml args['_genre_label'] = args.get('_genre_label', '{_genre_label}') args['_genre'] = args.get('_genre', '{_genre}') - - formatter = SafeFormatter() - generated_html = formatter.format(template, **args) has_data['series'] = bool(series) has_data['tags'] = bool(tags) has_data['rating'] = bool(rating) has_data['pubdate'] = bool(pubdate) + for k, v in has_data.items(): + setattr(display, k, 'initial' if v else 'none') + display.title = 'initial' + + formatter = SafeFormatter() + generated_html = formatter.format(template, **args) return strip_encoding_declarations(generated_html)