View Single Post
Old 10-11-2012, 03:00 AM   #321
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,733
Karma: 6690881
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by TicklishOwl View Post
I'm trying to set up a composite custom column (built from Calibre's tags column), but I'm having trouble with the template code.

...
Example: A book tagged 'classics, france, historical, middle-grade' would add 'Historical Fiction, Middle-Grade' to #koboshelf.

It's easy to get a single tag to work:
Code:
{tags:contains(historical, Historical Fiction,)}
Unfortunately, I can't get multiple tags to work. Clearly I do not have a gift for this language. I'd be most grateful for some guidance.
Use a general program mode template like the following
Code:
program:
	t = field('tags');
	l = '';
	l = list_union(l, in_list(t, ',', 'space', 'Space Opera', ''), ',');
	l = list_union(l, in_list(t, ',', 'science', 'Science Fiction', ''), ',');
	l = list_union(l, in_list(t, ',', 'alternate', 'Alternate History', ''), ',');
	l = list_union(l, in_list(t, ',', 'a', 'b', ''), ',');
This template constructs a list of tags returned by the in_list functions.

Note 1: in_list takes a regular expression for the match pattern. If there are multiple tags that result in the same shelf, you can write it like this
Code:
list_union(l, in_list(t, ',', 'tag1|tag2', 'Space Opera', ''), ',');
Note 2: if you want exact matching on the tags, use something like
Code:
list_union(l, in_list(t, ',', '^space$', 'Space Opera', ''), ',');
or in the case of a multiple match
Code:
list_union(l, in_list(t, ',', '^(tag1|tag2)$', 'Space Opera', ''), ',');
Note 3: this template is at the limit of what the template system is intended to do. If you have a large library, it will probably be slow. What "large" means depends on the computer running calibre. If you have python skills, or if you know someone with those skills, a custom calibre template function will have much better performance.
chaley is offline   Reply With Quote