![]() |
#1 |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 538
Karma: 1000000
Join Date: Dec 2011
Location: Rome, Italy
Device: Kindle PW5, Kindle PW4, Kindle 4 NT
|
Making references to other fields in template functions
Hello everybody, this is my first attempt at dealing with python and with calibre programming / customization so apologies if the terminology is not correct.
I'm messing with the templates calibre uses while exporting books to devices. Currently my template is the following: {series}/{author_sort}/{series_index:0>2s|| - }{title} - {authors} Correct me if I am wrong, with this template if the book isn't part of a series ({series} is null) calibre creates a directory named after the book author, and in that folder it puts the book. If the book _is_ part of a series calibre creates a folder with the name of the series, a subfolder named after the author and in there it puts the book. Correct? Now, what I would like to achieve is: if the book has NOT a series, create a top folder named after the author and place the book there. If it HAS a series, create a folder with that name, but do _NOT_ create a subfolder with the autor name! Given that if the {series} field is empty calibre automatically ignores it, I thought I could do something like: {series}/{author_sort:MAGIC_GOES_HERE}.... Where the magic should be: if field {series} is empty, put $ - the value of author_sort -, if {series} is not empty do not put anything. But how can I get this? In my understanding if I do something like: {author_sort:'ifempty(val,str)'} the functions ifempty refers to the value of author_sort, while I'd like to check {series}. Can I refer to the series field value in "val"? Is there a way to achieve that? Thank you!!! Rus Last edited by Ruskie_it; 12-21-2011 at 03:04 AM. |
![]() |
![]() |
![]() |
#2 |
US Navy, Retired
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 9,896
Karma: 13806776
Join Date: Feb 2009
Location: North Carolina
Device: Icarus Illumina XL HD, Kindle PaperWhite SE 11th Gen
|
Welcome to Mobileread, hopefully someone with more expertise than I will help you out with this question.
Moderator Notice
Moved out of Development sub-forum to the Library Management sub-forum. The description of the Library management sub-forum reads as follows: All about viewing, adding, and exporting books; managing metadata; using templates; and understanding the calibre directory structure. As a note to all folks there are sticky posts at the top of every calibre forum, these posts are an excellent source of information and at least the titles should be given a quick once over before posting to see if they might be of help. At the top of the Development forum is the following sticky post, PLEASE READ BEFORE POSTING - The purpose of this subforum, for some reason this post is often missed by folks posting in the development forum. |
![]() |
![]() |
Advert | |
|
![]() |
#3 | |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 538
Karma: 1000000
Join Date: Dec 2011
Location: Rome, Italy
Device: Kindle PW5, Kindle PW4, Kindle 4 NT
|
Hello dwthanny,
Thank you for moving it. However, one time that I really read the "read before posting"!!!! ![]() I did, if I got confused is because one of the things that section is for (the post says) is: Quote:
|
|
![]() |
![]() |
![]() |
#4 |
US Navy, Retired
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 9,896
Karma: 13806776
Join Date: Feb 2009
Location: North Carolina
Device: Icarus Illumina XL HD, Kindle PaperWhite SE 11th Gen
|
I'm glad to see you read the post ahead of time.
No need to apologize, I get just as confused at times. Since I don't have the coding skills I just do a little housekeeping in forums and try to keep the non development posts out of the development area. All I can do with regards to templates is point you to the calibre template language area of the manual. chaley did the heavy lifting in creating this feature and if he has time it is not unusual for him to help folks out. |
![]() |
![]() |
![]() |
#5 |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 538
Karma: 1000000
Join Date: Dec 2011
Location: Rome, Italy
Device: Kindle PW5, Kindle PW4, Kindle 4 NT
|
Thanks again.
![]() Since I had a little spare time today at work, I had another go at it. Here is how I figured out how it could be, I'm posting this here in case anyone is interested: {series}/{author_sort:'test(ifempty(field('series'),""),"", $)'}/{series_index:0>2s|| - }{title} - {authors} The heart of what I was trying to do is this: {author_sort:'test(ifempty(field('series'),""),"", $)'} Here's the logic (I did not find relational operators, otherwise it might have been simpler): So, if {series} is empty, calibre won't create {series} folder in the first instance. After that, if series is empty, the function ifempty(field('series'),"") will return an empty string. This string will be thrown at the test function it is called within: test(FUNCTION,"",$) If the value of the function is "" (that is if, and only if, the {series} field is empty, the function test will output the value of the author field, $; if the value is not empty, then test will return an empty value. Therefore, when {series} has a value the whole template will be: series_name/""/book_title (and calibre will convert it to series_name/book_title) When {series} has not a value the template will be: ""/author_name/book_title (and calibre, I expect, will convert it to author_name/book_title) If anyone has experience in coding, please correct or confirm... thank you!!! Rus Last edited by Ruskie_it; 12-21-2011 at 10:22 AM. Reason: wrong order of the test parameters |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 538
Karma: 1000000
Join Date: Dec 2011
Location: Rome, Italy
Device: Kindle PW5, Kindle PW4, Kindle 4 NT
|
Yep, I tested it on a test folder, it works! :P
|
![]() |
![]() |
![]() |
#7 | |
Well trained by Cats
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 31,046
Karma: 60358908
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
|
Quote:
There is coding (as in user templates, recipes) and there is coding ( the core of Calibre, that we download). Development is for the Big Coding |
|
![]() |
![]() |
![]() |
#8 | |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 538
Karma: 1000000
Join Date: Dec 2011
Location: Rome, Italy
Device: Kindle PW5, Kindle PW4, Kindle 4 NT
|
Quote:
But coding a functional programming script as above was fun and mindbending enough. ![]() |
|
![]() |
![]() |
![]() |
#9 | ||
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,440
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Glad you found template program mode!
Quote:
Quote:
First, the /""/ will be converted to //, which will convert to /. As such, there isn't much point in generating it, although it doesn't hurt anything. Second, the test function can be used to test for != '', which eliminates the need for the subfunction by directly testing if series is not empty. Combining the above with my understanding of your requirements, I think that this template satisfies your requirements. Code:
{series:'test($, $, field('author_sort'))'}/{series_index:0>2s|| - }{title} - {authors} |
||
![]() |
![]() |
![]() |
#10 | |||
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 538
Karma: 1000000
Join Date: Dec 2011
Location: Rome, Italy
Device: Kindle PW5, Kindle PW4, Kindle 4 NT
|
Quote:
![]() Quote:
I want my book organized in folders on the device. When they are part of a series, I want them under a folder with the name of the series. When they are not, they should go under the name of the author. Quote:
And I know I should have refined it at home and not in the office. ![]() Yes, I think that moving the test in the series field makes it simpler and more elegant. ![]() |
|||
![]() |
![]() |
![]() |
#11 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,440
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
I was silly. I shouldn't have used "test". Better is "ifempty" as it avoids passing the same parameter twice, making things clearer and having better performance.
Code:
{series:'ifempty($, field('author_sort'))'}/{series_index:0>2s|| - }{title} - {authors} |
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Does anyone use "general program mode" template functions? | chaley | Library Management | 0 | 08-16-2011 11:54 AM |
Removing class and id references | Artha | Sigil | 10 | 07-24-2011 11:17 AM |
Multiple references to the same footnote | crutledge | Sigil | 8 | 08-23-2010 04:33 AM |
multiple references | troymc | Sigil | 3 | 05-06-2010 03:20 PM |
Unutterably Silly Why all the Lovecraft references? | Mobile Mason | Lounge | 3 | 12-03-2009 12:53 PM |