Use the full formatter for the catalog templates so the full template language and all custom columns can be used.

I have tested this in epub and mobi and it works as far as I can tell. Unfortunately this code is very complicated so there may be problems. We will see...
This commit is contained in:
Charles Haley 2014-08-23 12:37:32 +02:00
parent f349cc358b
commit 626d127cc3
2 changed files with 62 additions and 18 deletions

View File

@ -9,13 +9,11 @@ __docformat__ = 'restructuredtext en'
These templates control the content of titles displayed in the various sections These templates control the content of titles displayed in the various sections
Available fields: Available fields:
{title} Title of the book Any column, custom or otherwise, defined for the book, plus
{series} Series name {rating_parens} Rating, in parentheses
{series_index} Number of the book in the series {pubyear} Year the book was published
{rating} Rating {pubyear_parens} Year the book was published, in parentheses
{rating_parens} Rating, in parentheses
{pubyear} Year the book was published
{pubyear_parens} Year the book was published, in parentheses
''' '''
# Books by Author # Books by Author

View File

@ -20,10 +20,22 @@ from calibre.library.catalogs import AuthorSortMismatchException, EmptyCatalogEx
from calibre.ptempfile import PersistentTemporaryDirectory from calibre.ptempfile import PersistentTemporaryDirectory
from calibre.utils.date import format_date, is_date_undefined, now as nowf, as_local_time from calibre.utils.date import format_date, is_date_undefined, now as nowf, as_local_time
from calibre.utils.filenames import ascii_text, shorten_components_to from calibre.utils.filenames import ascii_text, shorten_components_to
from calibre.utils.formatter import TemplateFormatter
from calibre.utils.icu import capitalize, collation_order, sort_key from calibre.utils.icu import capitalize, collation_order, sort_key
from calibre.utils.magick.draw import thumbnail from calibre.utils.magick.draw import thumbnail
from calibre.utils.zipfile import ZipFile from calibre.utils.zipfile import ZipFile
class Formatter(TemplateFormatter):
def get_value(self, key, args, kwargs):
if not key:
return ''
if key in kwargs:
return kwargs[key]
if key not in self.book.all_field_keys():
raise Exception(_('column not in book: ') + key)
return self.book.format_field(key, series_with_index=False)[1]
class CatalogBuilder(object): class CatalogBuilder(object):
''' '''
@ -103,6 +115,7 @@ class CatalogBuilder(object):
stylesheet="content/stylesheet.css", stylesheet="content/stylesheet.css",
init_resources=True): init_resources=True):
self.formatter = Formatter()
self.db = db self.db = db
self.opts = _opts self.opts = _opts
self.plugin = plugin self.plugin = plugin
@ -1542,10 +1555,16 @@ class CatalogBuilder(object):
args = self.generate_format_args(book) args = self.generate_format_args(book)
if current_series: if current_series:
#aTag.insert(0,'%s%s' % (escape(book['title'][len(book['series'])+1:]),pubyear)) #aTag.insert(0,'%s%s' % (escape(book['title'][len(book['series'])+1:]),pubyear))
formatted_title = self.by_authors_series_title_template.format(**args).rstrip() formatted_title = self.formatter.safe_format(
self.by_authors_series_title_template, args,
_('error in') + ' by_authors_series_title_template:',
self.db.new_api.get_proxy_metadata(book['id']))
else: else:
#aTag.insert(0,'%s%s' % (escape(book['title']), pubyear)) #aTag.insert(0,'%s%s' % (escape(book['title']), pubyear))
formatted_title = self.by_authors_normal_title_template.format(**args).rstrip() formatted_title = self.formatter.safe_format(
self.by_authors_normal_title_template, args,
_('error in') + ' by_authors_normal_title_template:',
self.db.new_api.get_proxy_metadata(book['id']))
non_series_books += 1 non_series_books += 1
aTag.insert(0, NavigableString(escape(formatted_title))) aTag.insert(0, NavigableString(escape(formatted_title)))
@ -1688,9 +1707,15 @@ class CatalogBuilder(object):
# Generate the title from the template # Generate the title from the template
args = self.generate_format_args(new_entry) args = self.generate_format_args(new_entry)
if current_series: if current_series:
formatted_title = self.by_month_added_series_title_template.format(**args).rstrip() formatted_title = self.formatter.safe_format(
self.by_month_added_series_title_template, args,
_('error in') + ' by_month_added_series_title_template:',
self.db.new_api.get_proxy_metadata(book['id']))
else: else:
formatted_title = self.by_month_added_normal_title_template.format(**args).rstrip() formatted_title = self.formatter.safe_format(
self.by_month_added_normal_title_template, args,
_('error in') + ' by_month_added_normal_title_template:',
self.db.new_api.get_proxy_metadata(book['id']))
non_series_books += 1 non_series_books += 1
aTag.insert(0, NavigableString(escape(formatted_title))) aTag.insert(0, NavigableString(escape(formatted_title)))
spanTag.insert(stc, aTag) spanTag.insert(stc, aTag)
@ -1734,9 +1759,15 @@ class CatalogBuilder(object):
# Generate the title from the template # Generate the title from the template
args = self.generate_format_args(new_entry) args = self.generate_format_args(new_entry)
if new_entry['series']: if new_entry['series']:
formatted_title = self.by_recently_added_series_title_template.format(**args).rstrip() formatted_title = self.formatter.safe_format(
self.by_recently_added_series_title_template, args,
_('error in') + ' by_recently_added_series_title_template:',
self.db.new_api.get_proxy_metadata(book['id']))
else: else:
formatted_title = self.by_recently_added_normal_title_template.format(**args).rstrip() formatted_title = self.formatter.safe_format(
self.by_recently_added_normal_title_template, args,
_('error in') + ' by_recently_added_normal_title_template:',
self.db.new_api.get_proxy_metadata(book['id']))
aTag.insert(0, NavigableString(escape(formatted_title))) aTag.insert(0, NavigableString(escape(formatted_title)))
spanTag.insert(stc, aTag) spanTag.insert(stc, aTag)
stc += 1 stc += 1
@ -2248,10 +2279,16 @@ class CatalogBuilder(object):
args = self.generate_format_args(book) args = self.generate_format_args(book)
if current_series: if current_series:
#aTag.insert(0,escape(book['title'][len(book['series'])+1:])) #aTag.insert(0,escape(book['title'][len(book['series'])+1:]))
formatted_title = self.by_genres_series_title_template.format(**args).rstrip() formatted_title = self.formatter.safe_format(
self.by_genres_series_title_template, args,
_('error in') + ' by_genres_series_title_template:',
self.db.new_api.get_proxy_metadata(book['id']))
else: else:
#aTag.insert(0,escape(book['title'])) #aTag.insert(0,escape(book['title']))
formatted_title = self.by_genres_normal_title_template.format(**args).rstrip() formatted_title = self.formatter.safe_format(
self.by_genres_normal_title_template, args,
_('error in') + ' by_genres_normal_title_template:',
self.db.new_api.get_proxy_metadata(book['id']))
non_series_books += 1 non_series_books += 1
aTag.insert(0, NavigableString(escape(formatted_title))) aTag.insert(0, NavigableString(escape(formatted_title)))
@ -2374,7 +2411,10 @@ class CatalogBuilder(object):
#aTag.insert(0,'%d. %s · %s' % (book['series_index'],escape(book['title']), ' & '.join(book['authors']))) #aTag.insert(0,'%d. %s · %s' % (book['series_index'],escape(book['title']), ' & '.join(book['authors'])))
args = self.generate_format_args(book) args = self.generate_format_args(book)
formatted_title = self.by_series_title_template.format(**args).rstrip() formatted_title = self.formatter.safe_format(
self.by_series_title_template, args,
_('error in') + ' by_series_title_template:',
self.db.new_api.get_proxy_metadata(book['id']))
aTag.insert(0, NavigableString(escape(formatted_title))) aTag.insert(0, NavigableString(escape(formatted_title)))
spanTag.insert(stc, aTag) spanTag.insert(stc, aTag)
@ -2532,9 +2572,15 @@ class CatalogBuilder(object):
# Generate the title from the template # Generate the title from the template
args = self.generate_format_args(book) args = self.generate_format_args(book)
if book['series']: if book['series']:
formatted_title = self.by_titles_series_title_template.format(**args).rstrip() formatted_title = self.formatter.safe_format(
self.by_titles_series_title_template, args,
_('error in') + ' by_titles_series_title_template:',
self.db.new_api.get_proxy_metadata(book['id']))
else: else:
formatted_title = self.by_titles_normal_title_template.format(**args).rstrip() formatted_title = self.formatter.safe_format(
self.by_titles_normal_title_template, args,
_('error in') + ' by_titles_normal_title_template:',
self.db.new_api.get_proxy_metadata(book['id']))
aTag.insert(0, NavigableString(escape(formatted_title))) aTag.insert(0, NavigableString(escape(formatted_title)))
spanTag.insert(stc, aTag) spanTag.insert(stc, aTag)
stc += 1 stc += 1