Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 04-01-2022, 01:34 PM   #1
sylvandale
Junior Member
sylvandale began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Apr 2022
Device: none
Using IF THEN ELSE in a Calibre Save Template

Hi. I'm hoping for some help please in configuring my Calibre Save Template, and using IF THEN ELSE logic.

I have a custom 'Genre' hierarchical column to organise everything in my library. I got this idea from here many years ago I think, so I assume people will know the kind of thing. Examples of the structure are e.g.
fiction
fiction.crime
fiction.thriller
fiction.thriller.historical
travel
travel.usa
travel.usa.ca
etc.

For a long time I have happily used a nice template, also taken from this forum (thanks!), which breaks down the genre into folders and subfolders based on the "." separator. This is the argument:

"{#genre:'re_group($,"([^.]+)\.","[[$]]/")'}/{authors}/{series}/{title} - {authors}"

So e.g. John Grisham's Camino Winds, with Genre 'Fiction.Thriller' is sorted to 'Fiction/Thriller/John Grisham/Camino Winds - John Grisham'. [There is also an optional "Series" subfolder level, if the series field is populated.]

However, I don't pretend to understand this syntax properly. It just works and I'm very grateful.

What I have realised is that for all Fiction books I want to have the {authors} level subfolder, but for all other types of books I want a sort which does not use that subfolder. With Fiction, I tend to know what author I am looking for, but with other types that's not usually the case.

So, in simple terms, what I think I am after is something like:

IF
genre is fiction.*
THEN
{#genre:'re_group($,"([^.]+)\.","[[$]]/")'}/{authors}/{series}/{title} - {authors}
ELSE
{#genre:'re_group($,"([^.]+)\.","[[$]]/")'}/{series}/{title} - {authors}

The '*' above is meant to be the usual wildcard expression, as there may be no additional genre sub-categories or one sub-category or two or more.

Can anybody help put that into Calibre-speak please?

Many thanks
sylvandale is offline   Reply With Quote
Old 04-02-2022, 12:33 PM   #2
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,757
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
It isn't quite clear what you want but I think this is it. This template requires the latest version of calibre (5.40).
Code:
program:
# Isolate the first genre. If there is never more than one then use
#	g = re($#genre, '\.', '/');
	g = re(sublist($#genre, 0, 1, ','), '\.', '/');
	
# Test if the genre starts with 'fiction'.
	if "^fiction" in g then
		g & '/' & $authors & '/' & test($series, $series & '/', '') & $title & ' - ' & $authors

	else
		g & '/' & test($series, $series & '/', '') & $title & ' - ' & $authors
	fi
EDIT: Rereading your post it might be that you want to check for a prefix of "fiction.", not "fiction". If so then change the "if "^fiction" line to be
Code:
	if "^fiction/" in g then
NB: Use a slash because periods have already been converted to slashes, and the anchor '^' causes the check to be "starts with".

Last edited by chaley; 04-02-2022 at 04:05 PM. Reason: small change to program comments
chaley is offline   Reply With Quote
Advert
Old 04-02-2022, 04:03 PM   #3
sylvandale
Junior Member
sylvandale began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Apr 2022
Device: none
Absolutely brilliant, Works like a charm. Many, many thanks.

Last edited by sylvandale; 04-02-2022 at 04:03 PM. Reason: spelling error
sylvandale is offline   Reply With Quote
Reply

Tags
genre hierarchy, if then else, save template


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
save as template ajean4 Library Management 12 04-15-2021 04:32 PM
Book upload, Calibre Save Template sometimes parsed, sometimes not Irian Calibre 4 06-17-2020 12:22 PM
save to disk template help bilaly Library Management 2 10-19-2018 07:19 PM
Save Template Help Maverynthia Library Management 10 05-19-2014 12:37 PM
A little help with a save template...? Belfaborac Library Management 2 02-04-2011 09:47 PM


All times are GMT -4. The time now is 01:09 AM.


MobileRead.com is a privately owned, operated and funded community.