View Single Post
Old 01-14-2011, 02:34 PM   #395
GRiker
Comparer of the Ephemeris
GRiker ought to be getting tired of karma fortunes by now.GRiker ought to be getting tired of karma fortunes by now.GRiker ought to be getting tired of karma fortunes by now.GRiker ought to be getting tired of karma fortunes by now.GRiker ought to be getting tired of karma fortunes by now.GRiker ought to be getting tired of karma fortunes by now.GRiker ought to be getting tired of karma fortunes by now.GRiker ought to be getting tired of karma fortunes by now.GRiker ought to be getting tired of karma fortunes by now.GRiker ought to be getting tired of karma fortunes by now.GRiker ought to be getting tired of karma fortunes by now.
 
Posts: 1,496
Karma: 424697
Join Date: Mar 2009
Device: iPad
New OCD features (0.7.39)

As of 0.7.39, it's possible to do some fancy manipulations of a book's metadata to generate text that can be added to the Descriptions pages of your catalogs.

Let's say that you use the tags 'NYT #1 Fiction' and 'NYT 10 Best of the Year'. What we'd like to do is create a custom column 'Awards' whose content will be 'NYT 10 Best of <year of publication>', or 'NYT #1 <year of publication>', which can then be inserted into the Description page opposite the cover.


The top oval contains links to all books in your catalog tagged with 'NYT #1 Fiction' or 'NYT 10 Best of the Year'. The bottom oval is the result of following the steps in this post, which generate the Awards section from the tags.


Step 1: Create a custom column 'Awards'
  • Open Preferences|Interface|Add your own columns
  • Click Add custom column
  • Lookup name: awards
  • Column heading: Awards
  • Column type: Column built from other columns
  • Template: program:'' (we will add the template in Step 3)
  • Click OK to close the dialog
  • Click Apply to close the Add your own columns dialog
  • Click Close to close the Preferences dialog.

Step 2: Create the template function
  • Open Preferences|Advanced|Template Functions
  • Copy the code below, paste it into the Program Code window.
    Code:
    def evaluate(self, formatter, kwargs, mi, locals, *args):
      from calibre.utils.date import format_date
      SEPARATOR = u' \u00B7 '   # Unicode representation of &middot;
      awards = {}
      for arg in args:
        strings = arg.split(':')
        awards[strings[0]] = strings[1]
      a_list = [awards[t] + format_date(kwargs.get('pubdate'), u' yyyy') for t in kwargs.get('tags') if t in awards]
      return SEPARATOR.join(a_list)
  • In the Function field, type award_date
  • Set Arg count to -1, representing a variable number of arguments
  • Click Create to add the award_date function
  • Click Apply, then close the Preferences dialog

Step 3: Add the template to the custom column
  • Double-click any book's cell in the Awards column to open the Edit template dialog window.
  • Paste the following code into the edit box:
    Code:
    program:award_date(
    'NYT 10 Best of the Year:NYT 10 Best of',
    'NYT #1 Fiction:NYT #1',
    'NYT #1 Nonfiction:NYT #1')
  • Click OK to close the Edit template window

Step 4: Create the catalog, adding Awards to the Description pages
  • Click Convert Books↓Create catalog of books in your calibre library
  • Switch to the E-book options tab
  • In Other options|Description note, select Awards
  • Catalogs created with these settings will show matching books awards in the Description notes section, opposite the cover

An even more advanced approach: per-tag processing
Spoiler:
It is also possible to process matching tags individually. In this approach, the mappings are assigned in the function.
Step 1: Create a custom column 'Awards'
  • Open Preferences|Interface|Add your own columns
  • Click Add custom column
  • Lookup name: awards
  • Column heading: Awards
  • Column type: Column built from other columns
  • Template: program:'' (we will add the template in Step 3)
  • Click OK to close the dialog
  • Click Apply to close the Add your own columns dialog
  • Click Close to close the Preferences dialog.

Step 2: Create the template function
  • Open Preferences|Advanced|Template Functions
  • Copy the code below, paste it into the Program Code window.
    Code:
    def evaluate(self, formatter, kwargs, mi, locals, val):
    	from calibre.utils.date import format_date
    	SEPARATOR = u' \u00B7 '   # Unicode representation of &middot;
    	list_1 = {'NYT 10 Best of the Year':'NYT Top 10 Best of'}
    	list_2 = {'NYT #1 Fiction':'NYT Best Seller',
    		      'NYT #1 Nonfiction':'NYT Best Seller'}
    	ans = []
    	for tag in kwargs.get('tags'):
    		if tag in list_1:
    			ans.append(list_1[tag] + format_date(kwargs.get('pubdate'),u' yyyy'))
    		elif tag in list_2:
    			ans.append(list_2[tag])
    	ans = sorted(ans)
    	return SEPARATOR.join(ans)
  • In the Function field, type award_date
  • Set Arg count to 1, representing a single argument
  • Click Create to add the award_date function
  • Click Apply, then close the Preferences dialog

Step 3: Add the template to the custom column
  • Double-click any book's cell in the Awards column to open the Edit template dialog window.
  • Paste the following code into the edit box:
    Code:
    program: award_date(None)
  • Click OK to close the Edit template window

Step 4: Create the catalog, adding Awards to the Description pages
  • Click Convert Books↓Create catalog of books in your calibre library
  • Switch to the E-book options tab
  • In Other options|Description note, select Awards
  • Catalogs created with these settings will show matching books awards in the Description notes section, opposite the cover

Thanks to @chaley for adding python functionality to the template feature making this possible, and his assistance in developing the sample code.

Learn more about calibre's template language feature
Discuss other uses of calibre's template language feature

G

N.B. The year added to the award name is actually the year of the book's publication, which is almost always the same year the book won the award. If you're more OCD than I am, and this bothers you, create a custom column dedicated to awards.

Last edited by GRiker; 01-18-2011 at 08:50 AM.
GRiker is offline