View Single Post
Old 10-02-2010, 06:13 AM   #1
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,734
Karma: 6690881
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Plugboard, template, and custom composite column recipes

Several people are doing interesting and useful things with templates, plugboards, and composite fields. This thread is both a request that they share what they have done and a place to put the information.

Suggestions/recipes are summarized below. Don't hesitate to post corrections and useful variants.

Kindle/Kobo
  • Metadata: Show series - series Index - Title as Title (Kindle/Kobo) (economix)
    Spoiler:
    I have been playing around with the Metadata plugboards to get the titles on a Kindle 3 to show series information. I used the following template:
    format: mobi
    device: kindle2
    template: {series}{series_index:0>2s| - | - }{title}
    destination: title

    This works very nicely for books that are part of a series e.g.:
    Quote:
    A Song of Ice & Fire - 01 - A Game of Thrones
    A Song of Ice & Fire - 02 - A Clash of Kings
    A Song of Ice & Fire - 03 - A Storm of Swords
    A Song of Ice & Fire - 04 - A Feast for Crows
  • Metadata: Show series [series index] - title as title (Kindle) (Scott Nielsen)
    Spoiler:
    format: mobi
    device: kindle2
    template: {series:|| }{series_index:0>2s|[|] - }{title}
    destination: title
  • Metadata: Show author name as Initial and Surname (Kindle) (inspired by brewjono)
    Spoiler:
    1) Verify author_sort values are Surname, Firstname order.
    2) Uncheck 'use author sort' in the mobi output plugin.
    3) Create a plugboard
    format: mobi
    device: kindle2
    template: {author_sort:re(\, (.).*?( &|$),\, \1\2)}
    destination: authors
  • Reading/read status custom column (Kindle) (pchrist7, w/ideas from beckywc):
    Spoiler:
    Extract reading/read status from kindle annotations and show them in a custom column. See this post for details and instructions..

Filename (Save Templates)
  • Filename: author sort/series/title - version (WinCE/Mobile (Mobipocket))(sweetpea)
    Spoiler:
    Using a version custom column in filenames
    {author_sort}/{series}/{title} - v{#version:0>2s}
  • Filename: Create file paths grouping authors into [A - D]-like categories (WinCE/Mobile (Mobipocket)) (Sweetpea)
    Spoiler:
    {author_sort:switch(^[A-D],[A - D],^[E-J],[E - J],^[K-O],[K - O],^[P-T],[P - T],^[U-Z],[U - Z],[Other])}
  • Filename: Create path based on series index (sweetpea, inspired by Arco Witter)
    Spoiler:
    If you have a lot of books in a series, and want them in a folder hierarchy like series/00-09/author/title, series/10-19/author/title. You want just author/title if there is no series.
    Create a composite column called (for example) #s_index.
    Column: #s_dex
    Template: {series_index:0>2s}

    Save template:
    Code:
     {series}{#s_dex:switch(0.,00-09,1.,10-19,2.,20-29,3.,30-39,4.,40-49,5.,50-59,6.,60-69,7.,70-79,8.,80-89,9.,90-99,)|/|/}{author}/{title}

General Metadata
  • Metadata: Put series into the title, using either initials or a shortened form. Strip leading articles from the series name (any) (inspired by Gary_M_Mugford)
    Spoiler:
    The solution requires creating three composite columns. The first column is used to remove the leading articles. The second is used to compute the 'shorten' form. The third is to compute the 'initials' form. Once you have these columns, the plugboard selects between them. You can hide any or all of the three columns on the library view.

    First column:
    Code:
    Name: #stripped_series. Template: {series:re(^(A|The|An)\s+,)||}
    Second column (the shortened form):
    Code:
    Name: #shortened. Template: {#stripped_series:shorten(4,-,4)}
    Third column (the initials form) (Modified/corrected by eschwartz 6/Jan/14):
    Code:
    Name: #initials. Template: {#stripped_series:re([\s]?([^\s])[^\s]+(\s|$),\1)}
    Plugboard expression:
    Code:
    Template:{#stripped_series:lookup(.\s,#initials,.,#shortened,series)}{series_index:0>2s| [|] }{title}
    Destination field: title
    This set of fields and plugboard produces:
    Series: The Lord of the Rings
    Series index: 2
    Title: The Two Towers
    Output: LotR [02] The Two Towers

    Series: Dahak
    Series index: 1
    Title: Mutineers Moon
    Output: Dahak [01] Mutineers Moon

    Series: Berserkers
    Series Index: 4
    Title: Berserker Throne
    Output: Bers-kers [04] Berserker Throne

    Series: Meg Langslow Mysteries
    Series Index: 3
    Title: Revenge of the Wrought-Iron Flamingos
    Output: MLM [03] Revenge of the Wrought-Iron Flamingos
  • Author pen names.
    Spoiler:
    Use User categories and templates to manage multiple pen names for authors. See this thread, in particular posts #7 and #15.

Useful stored templates
  • Get the lookup name of the current column in the books view:
    Spoiler:
    Stored template:
    Code:
    python:
    def evaluate(book, context):
    	from calibre.gui2.ui import get_gui
    	gui = get_gui()
    	cv = get_gui().current_view()
    	return cv.column_map[cv.currentIndex().column()]
    Preferences / Stored templates screen capture:
    Click image for larger version

Name:	Clipboard03.jpg
Views:	206
Size:	95.0 KB
ID:	199878
    Example:
    Code:
    current_column_name = current_column();
    column_value = field(current_column_name)
  • Check if a date is less than another:
    Spoiler:
    Stored template:
    Code:
    python:
    def evaluate(book, context):
    	args = context.arguments
    	if args is None or len(args) != 2:
    		return "date_less_than requires 2 arguments"
    	d1 = book.get(args[0])
    	d2 = book.get(args[1])
    	if d1 is None or d2 is None:
    		return '' # return no if either date isn't defined
    	return 'Yes' if d1 < d2 else ''
    Preferences / Stored templates screen capture:
    Click image for larger version

Name:	Clipboard05.jpg
Views:	180
Size:	174.0 KB
ID:	199879

    Example:
    Code:
    if date_less_than('#mydate1', '#mydate2') == 'Yes' then something fi

Template Searches
Common composite custom columns

All the recipes below require the creation of a custom column of type 'Column built from other columns', called a composite column. Creation instructions are under the spoiler.
Spoiler:
1. Go to preferences, Interface (the first row), Add your own columns.
2) Click the green plus sign.
3) Choose a lookup name. This will be the name you use for searching and in templates. It must be letters, all lowercase. Note that when you use the lookup name you must prefix it with a '#' character. Do not enter that character here.
4) Choose a column heading. This is what will appear at the top of the column.
5) Choose a column type. For the recipes below, that type must be 'Column built from other columns'.
6) Enter the template from the recipe into the template box.
7) Press OK, Apply, etc. Restart calibre.
  • Display the ISBN. Template: {isbn}
  • Display the formats for a book. Template: {formats}. EDIT: calibre 6.17 has a native Formats column. See Preferences / Add your own columns and check the box for Formats (lookup name 'formats')
  • Display Yes if the book has an EPUB format. Template:{formats:contains(EPUB, Yes,)}
  • Display the author sort. Template: {author_sort}
  • Display the title sort. Template: {title_sort}

Deprecated
  • Sony Metadata
    Spoiler:

    Metadata: Show author and title for title (Sony) (Sweetpea)
    Spoiler:
    format: device-db
    device: any device (or PRS505)
    template: {authors} - {title}
    destination: title

    Metadata: Show title [series [series index]] as title, limiting series to 8 letters (Sony) (chaley)
    Spoiler:
    Put 8 characters of the series plus the series index at the end of the title. The plugboard is:
    [CODE]format: device-db
    device: PRS505
    template: {title}{series:<.8s| [|}{series_index:| [|]]}
    destination: title

    Metadata: Show series - series index - title, with series as initials (Sony) (inspired by speakingtohe)
    Spoiler:
    The plugboard is:
    format: device-db
    device: PRS505
    template: {series:re(([^\s])[^\s]+(\s|$),\1)}{series_index:0>2s| - | - }{title}
    destination: title

    Metadata: Given a yes/no 'Read' custom column, have the collections display Read, Reading, Unread (Sony) (sweetpea)
    Spoiler:
    Assume that you have a custom yes/no column where you keep whether or not a book is read. Yes means the book is read, No means that the book is being read, and Unknown (empty) means that the book is to be read. This recipe will create a column containing Read/Reading/Unread that can be used to create a collection.

    Create a composite custom column, named as you will. In this example we assume that the yes/no column is named #read. The new column's template will be:
    template: {#read:switch(Yes,Read,No,Reading,Unread)}
    Then put the new composite column's name into the fields to be used for series list in the Sony's device customization.

Last edited by chaley; 03-15-2024 at 06:19 PM.
chaley is offline   Reply With Quote