02-24-2011, 01:55 PM | #1 |
Member
Posts: 18
Karma: 10
Join Date: Feb 2011
Device: Nook
|
Send to device with conditional directories
I'd like to send books to my nook with a semi-dynamic format, if it appears on the goodreads shelf 'to read' it goes to a folder /To Read/Title - Author.file if not, then it goes to /Author_sort/Series/Title - authors.file
The Goodreads section is handled by the wonderful goodreads sync and a custom yes/no field called #gr_2read. I set my send to device filter to be what I think would accomplish this: Code:
{#gr_2read:test("To Read",{author_sort}/{series})}/{title} - {authors} Am I misunderstanding how the test function should work or what am I doing wrong? (EDIT: This was moved from Library Management to Devices, but it's a device independent question as far as I'm aware, I'm just asking how the filter is being applied and how to apply it the way I wish it to be.) Last edited by Doug-W; 02-24-2011 at 03:36 PM. |
02-25-2011, 11:51 AM | #2 | |
Member
Posts: 18
Karma: 10
Join Date: Feb 2011
Device: Nook
|
Quote:
However my plan is to create two composite fields: NookDir:{author_Sort}/{series} ToRead: To Read And then use the following send to device template: {#gr_2read:lookup(.,{#ToRead},{#NookDir})}/{title} - {authors} I didn't notice originally that test is only on raw strings and can't have templates. |
|
Advert | |
|
02-25-2011, 01:46 PM | #3 |
Grand Sorcerer
Posts: 12,043
Karma: 7548549
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
You should look at "template program mode" in http://calibre-ebook.com/user_manual/template_lang.html. That mode allows you to write more arbitrary programs. For example, your original program could be written as
Code:
{#gr_2read:'test($,"To Read",strcat(field("author_sort"),"/",field("series")))'}/{title} - {authors} Code:
{#gr_2read:'test($,"To Read",template("[[author_sort]]/[[series]]"))'}/{title} - {authors} Code:
program: strcat( test(field('#gr_2read'), "To Read", template("{author_sort}/{series}")), template('/{title} - {authors}') ) |
02-26-2011, 11:16 AM | #4 |
Member
Posts: 18
Karma: 10
Join Date: Feb 2011
Device: Nook
|
Thank you, that helped a lot.
I had gone over that page you linked a few dozen times while putting this together, the section on save/send templates is what gave me the idea to construct columns. One problem though is that it doesn't help when the docs are incorrect For example the largest problem I had was that the docs say: # test(text if not empty, text if empty) – return text if not empty if the field is not empty, otherwise return text if empty. Not test(expression_to_test, text if not empty, text if empty) which is how it is apparently used. I thought it took the expression to the left of the test, but now that I understand a bit better I see how the left expression fits in particularly with 'program' Final question just to be sure, does test take a regex of which a field value just happens to be a static test fed to the regex engine? If so that's kind of neat, I've been using regular expressions for 20 years, and never thought to use '.' for contains data, and '$' for always true. |
02-26-2011, 11:42 AM | #5 | ||
Grand Sorcerer
Posts: 12,043
Karma: 7548549
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Quote:
First, let's assume that we are using general program mode so we don't need to worry about which quote to use. The 'test' function checks if value is or is not empty. There is no regexp involved. Examples: Code:
test('', 'A', 'B') will return 'B' test('something', 'A', 'B') will return 'A' Code:
{series:test(A,B)} Code:
{series:'test($, "A", "B")'} Code:
switch('ABC', '.', 'A', 'B') will return 'A' switch('ABC', '^$', 'A', 'B') will return 'B' switch('ABC', 'AB', 'A', 'B') will return 'A' switch('ABC', '^.*$', 'A', 'B') will return 'A' |
||
Advert | |
|
02-26-2011, 01:41 PM | #6 | ||
Member
Posts: 18
Karma: 10
Join Date: Feb 2011
Device: Nook
|
Quote:
My confusion was thinking that $ wasn't a default variable but was rather the regex expression for end of line, which would always be true. Combined with this example: Quote:
|
||
02-26-2011, 02:36 PM | #7 | ||
Grand Sorcerer
Posts: 12,043
Karma: 7548549
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
All the functions listed under single-function mode can be used in program mode, noting that unlike the functions described below you must supply a first parameter providing the value the function is to act upon. The following functions are available in addition to those described in single-function mode. Remember from the example above that the single-function mode functions require an additional first parameter specifying the field to operate on. With the exception of the id parameter of assign, all parameters can be statements (sequences of expressions): Quote:
|
||
03-06-2011, 11:12 AM | #8 |
Member
Posts: 18
Karma: 10
Join Date: Feb 2011
Device: Nook
|
Sorry to bump a slightly older post, I appreciate all the help you'd given (and welcome anyone else to jump in,) but I just saw a problem with the templates specified in here, as I mentioned, #gr_2read is a yes/no custom field, and 'test' is returning True if the field has a value, not if the field has a value and the value is True. So, any book that I've synced to goodreads at all is showing up in To Read, even if it's marked as already read (Custom field #gr_read true, custom field #gr_2read false.) Is there an easy way to test for that?
|
03-06-2011, 11:35 AM | #9 |
Grand Sorcerer
Posts: 12,043
Karma: 7548549
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Try:
Code:
{#gr_2read:'switch($, "Yes", "To Read", template("[[author_sort]]/[[series]]"))'}/{title} - {authors} Note that the regexps used by switch are case sensitive, and that the string values of a bool column are Yes, No, and empty. IIRC, Yes and No are translatable, so if you are not running in English, check to see what the text says. Starting in 0.7.48 (or maybe 47) you can avoid translation by using raw_field, with will return 'True', 'False', and 'None'. Code:
program: strcat( switch(raw_field('#gr_2read'), 'True', 'To Read', template('[[author_sort]]/[[series]]')), template('/{title} - {authors}') ) |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Can't Send to device | Neelly | Devices | 3 | 01-20-2011 09:48 AM |
Problem with 'Send to Device' | Rolando | Calibre | 9 | 10-28-2010 02:14 PM |
0.6.20 Send to device - kindle | lisa.m | Calibre | 2 | 11-05-2009 12:26 PM |
Send to device button | p3aul | Calibre | 12 | 05-23-2009 03:34 AM |
LibPRS500 Send To Device | pilotbob | Calibre | 1 | 04-06-2008 02:53 PM |