View Single Post
Old 01-20-2012, 11:22 AM   #13
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: 12,488
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
(After writing this reply I found that HarryT had answered the question far more succinctly, but having gone to the trouble to write this, I posted it.)

First comment: the template language is not python, and is only loosely related to python. It does use some python things such as formatting (see below), but that was for my convenience. It might help to think of the template language as serving a similar purpose in calibre as formula functions serve in Excel; they permit customization without having to change the underlying program.

Regarding:
Quote:
Originally Posted by BeccaPrice View Post
I blindly copied a plugboard rule I saw somewhere here to my copy of Calibre - it's supposed to put the series name and number after the title.
...
mobi:any device = ([{title} {series:|(| }{series_index:0>2s||)}] -> title)
and
Quote:
Originally Posted by BeccaPrice View Post
using my rule, as an example of what I don't understand (please remember that I am not a programmer, and this stuff is very new to me)

([{title} {series:|(| }{series_index:0>2s||)}] -> title)

The whole rule is included in opening and closing parens (but why isn't the second rule I cited enclosed in parens?)
First thing to remember: a plugboard is not the same thing as a template. A plugboard *uses* templates.

Your first example describes a plugboard containing one template that changes the title field. Such descriptions have a fixed format. The template plus changed field are between parentheses. The changed field is separated from the template by the characters "->". The template is contained within "[]" characters. In other words, the syntax is:
Code:
format:device = ([TEMPLATE] -> CHANGED FIELD)
where (...) is repeated for each changed field in the plugboard.

Your example says that when a "mobi" format is sent to "any device", then the template
Code:
{title} {series:|(| }{series_index:0>2s||)}
is evaluated and the result is placed into the title metadata inside the book.
Quote:
what does the 0>2s mean? I assume that it's saying that the series index has to be greater than 0, but I don't understand the 2s part.
This is telling the template language processor how to format the series index. The initial 0 means add zeros to the front if needed. The > means align the result to the right side (important for what comes next). The 2 means that the field should be 2 long, and the 's' means it is a string. The combination of all of this is "make series_index a value 2-long, left filled with zero characters as needed. Example: 1 -> 01. 5 -> 05. 10 -> 10.

See http://docs.python.org/library/string.html#formatspec for more than you ever wanted to know about formatting.
chaley is offline   Reply With Quote