05-15-2014, 04:14 PM | #1 |
Connoisseur
Posts: 65
Karma: 400010
Join Date: Mar 2013
Location: London
Device: Nook STG, PW2
|
Send to device - Template help
I have a template that first creates two folders, one for books the other fanfiction.
Once inside the fanfiction folder, the fandom is used to create a folder in which fanfics will be placed. While in the books folder, the author name is to be used. Code:
{#fandom:'test($, 'Fanfiction', 'Books')'}/{#fandom:'test($, $, field('authors'))'}/{title} |
05-15-2014, 08:13 PM | #2 |
Ex-Helpdesk Junkie
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
|
Instead of
Code:
{#fandom:'test($, $, field('authors'))'} Code:
{#fandom:' ifempty( sublist($, 0, 1, ","), field("authors") ) '} EDIT: This code block takes up several lines, but only because I wanted to make it clear what is going on -- I like to indent and put each section on its own line. It may actually be MORE readable in calibre if it is all one line; this works too: Code:
{#fandom:'ifempty(sublist($, 0, 1, ","), field("authors"))'} Last edited by eschwartz; 05-15-2014 at 08:17 PM. |
Advert | |
|
05-20-2014, 02:57 PM | #3 | |
Connoisseur
Posts: 65
Karma: 400010
Join Date: Mar 2013
Location: London
Device: Nook STG, PW2
|
Quote:
In case someone else is interested in organising their folders by the name of their library; Code:
{title:'current_library_name()'}/{title:'contains(current_library_name(), 'Fanfiction Library',sublist(field('#fandom'), 0, 1, ","), field("authors"))'}/{title} |
|
05-20-2014, 03:45 PM | #4 |
Ex-Helpdesk Junkie
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
|
Glad I could help! I'd like to point out that in template program mode, which we used, the fieldname at the beginning is optional. But if available, you can represent it with "$".
It isn't Code:
{title:'current_library_name()'} Code:
{:'current_library_name()'} General Program Mode!!! Much cooler! And personally I think it's easier than applying functions to templates. For your final template, this is what it would look like. Code:
program: # if in fanfic library, use first available fandom # else use the author first_fandom = contains( current_library_name(), "^Fanfiction Library$", sublist( field( "#fandom"), 0, 1, "," ), field("authors") ); # add together each section of the template strcat( current_library_name(), "/", first_fandom, "/", field("title") ); Last edited by eschwartz; 05-20-2014 at 04:32 PM. Reason: with chaley's change |
05-20-2014, 04:18 PM | #5 |
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
@Snow Sciles and @eschwartz: you might want to use strcmp instead of contains. Contains takes an regexp and can match substrings, while strcmp does an exact match. For example, the contains in that template would match "Temp Fanfiction Library", which you might not want.
Alternatively, use anchors in the regexp pattern given to "contains", as in "^Fanfiction Library$". |
Advert | |
|
05-20-2014, 04:34 PM | #6 |
Ex-Helpdesk Junkie
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
|
thanks for the advice chaley!
I was only looking at the conversion to program mode. |
05-21-2014, 07:40 AM | #7 |
Connoisseur
Posts: 65
Karma: 400010
Join Date: Mar 2013
Location: London
Device: Nook STG, PW2
|
chaley, when using strcmp, is it correct to leave the "It" value blank?
Code:
strcmp( current_library_name(), 'Fanfiction Library', '', sublist(field('#fandom'), 0, 1, ','), field('authors') ) There are two things I'm going try. The first is to create a function that controls the author link in Book Details. Here is what I've mustered so far Code:
program: default = "https://en.wikipedia.org/w/index.php?search={author}"; ao3 = "http://archiveofourown.org/works/search?work_search[creator]={author}"; ffnet = "https://www.fanfiction.net/search.php?keywords={author}&ready=1&type=writer"; author_lookup = first_matching_cmp( field('publisher'), 'archiveofourown.org', ao3, 'fanfiction.net', ffnet, default ); I don't know what's wrong. I did expect to have to print author_lookup but it seems to work regardless, or perhaps first_match_cmp isn't a good function to use? The second thing that I'm going to try, is column colouring. I've used the rules method, but Calibre became sluggish. I read this a known consequence of too many rules, so I limited things to only colouring the Title rather than All. Before I try, I want to know if doing it in program mode would prevent that sluggishness? |
05-21-2014, 10:26 AM | #8 |
Ex-Helpdesk Junkie
Posts: 19,422
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
|
Not sur about the first question.
General program mode is faster at generating the templates than the other modes, since it is compiled to python. How much, I don't know. |
05-21-2014, 11:12 AM | #9 | |||
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Quote:
I think you want the switch function. Using that function you would write something like Code:
author_lookup = switch( field('publisher'), '^archiveofourown.org$', ao3, '^fanfiction.net$', ffnet, default ); Quote:
Something to keep in mind for the future: complicated templates can be replaced with hand-coded python functions, which are almost certainly faster, perhaps orders of magnitude. See the "Template functions" preference in the Advanced section. Personally, I wouldn't attempt a template function until I had a GPM template that did what I want. That way I am fighting only one battle, making the python work, instead of two, making it work while determining what it should do. |
|||
05-22-2014, 11:01 AM | #10 |
Connoisseur
Posts: 65
Karma: 400010
Join Date: Mar 2013
Location: London
Device: Nook STG, PW2
|
Thanks Chaley. I was eyeing the switch function, but thought perhaps I'm just doing something wrong with first_matching_cmp.
I found that I had to strcat the default. Code:
program: default = strcat("https://en.wikipedia.org/w/index.php?search=", field('author')); ao3 = "http://archiveofourown.org/works/search?work_search[creator]={author}"; ffnet = "https://www.fanfiction.net/search.php?keywords={author}&ready=1&type=writer"; author_lookup = switch( field('publisher'), '^archiveofourown.org$', ao3, '^fanfiction.net$', ffnet, default ); I'll continue to play around with GPM. Now that I've started, I'm trying to find things to do. |
05-26-2014, 04:57 PM | #11 |
Connoisseur
Posts: 65
Karma: 400010
Join Date: Mar 2013
Location: London
Device: Nook STG, PW2
|
I have found that the above program does not work. It works when I make a custom column, but when I put the code in the template box, on the book details settings page, everything defaults to the default.
I thought it worked, but found that the author links for books from ao3 and ff.net are saved in the "manage authors" settings. So I was mistaken. I don't know how that came to be, when I started this code all author links pointed to wikipedia. Which is the calibre default author link on the book details page. From what I can gather, the field('publisher') is not being accessed in the book details template box. This is the final code I tested in custom columns. But I want it to work in the book details template, is that possible? Code:
program: default = strcat("https://en.wikipedia.org/w/index.php?search=", field('author')); ao3 = strcat("http://archiveofourown.org/works/search?work_search[creator]=", field('author')); ffnet = strcat("https://www.fanfiction.net/search.php?keywords=", field('author'), "&ready=1&type=writer"); rkfic = strcat("http://www.rockfic.com/user_profile.php?user_id=", field('#authorid'), "&user=", field('author'), "&m=c"); author_lookup = switch( field('publisher'), 'archiveofourown.org', ao3, 'www.fanfiction.net', ffnet, 'rockfic', rkfic, default ); Last edited by Snow Sciles; 05-26-2014 at 05:17 PM. |
05-26-2014, 05:06 PM | #12 | |
Grand Sorcerer
Posts: 11,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Yes, "field" is a template function, and yes, it will run, but unfortunately the only fields that the "field" function can access are the two listed above. |
|
05-26-2014, 05:16 PM | #13 |
Connoisseur
Posts: 65
Karma: 400010
Join Date: Mar 2013
Location: London
Device: Nook STG, PW2
|
Oh! I never saw that. All this time I've been fussing with the code and trying to work out why it won't work! It would be good if the publisher field worked, because I think this is useful code to have.
Anyway, I've become more familiar with GPM, so I consider it a good exercise. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Issue with Send to Device template | BookJunkieLI | Library Management | 5 | 02-14-2014 01:16 AM |
Send to Device Template Structure Refuse to Work | soriya | Calibre | 5 | 02-02-2014 04:06 PM |
Template Help - Send to device | Snow Sciles | Library Management | 4 | 10-08-2013 07:54 AM |
Send to device template | rockeh | Calibre | 6 | 08-17-2010 05:28 PM |
template for "send to device" customization | sbin | Calibre | 8 | 01-07-2010 07:22 AM |