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 05-15-2014, 04:14 PM   #1
Snow Sciles
Connoisseur
Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.
 
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}
The problem is that a fanfic can have multiple fandoms, which are treated as one. So I want to select the first fandom, but have no idea how.
Snow Sciles is offline   Reply With Quote
Old 05-15-2014, 08:13 PM   #2
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
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'))'}
use

Code:
{#fandom:'
	ifempty(
		sublist($, 0, 1, ","),
		field("authors")
	)
'}
which should use sublist to return the zeroth through first items in #fandom, assuming they are separated by "," and ifempty, returning the authors field instead.

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.
eschwartz is offline   Reply With Quote
Advert
Old 05-20-2014, 02:57 PM   #3
Snow Sciles
Connoisseur
Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.
 
Posts: 65
Karma: 400010
Join Date: Mar 2013
Location: London
Device: Nook STG, PW2
Quote:
Originally Posted by eschwartz View Post

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"))'}
No worries about the block, it is clearer and for the first time, the template language looks familiar as it reminds me of php. Which I'm somewhat knowledgeable about. Thanks for your help. I re-jigged a few things and today used it to reorganise the books on my Nook.

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}
Again, thanks eschwartz.
Snow Sciles is offline   Reply With Quote
Old 05-20-2014, 03:45 PM   #4
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
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()'}
it is
Code:
{:'current_library_name()'}
You know what the next big step is, don't you... I mean, if you're going to insist on using the full capabilities and everything...

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
eschwartz is offline   Reply With Quote
Old 05-20-2014, 04:18 PM   #5
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,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$".
chaley is offline   Reply With Quote
Advert
Old 05-20-2014, 04:34 PM   #6
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
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.
eschwartz is offline   Reply With Quote
Old 05-21-2014, 07:40 AM   #7
Snow Sciles
Connoisseur
Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.
 
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')
)
eschwartz, your enthusiasm has convinced me to try program mode.

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
        );
This works on books that have ao3 or ffnet as publisher. But for those that do not, instead of seeing the default wikipedia link, I get "Could not convert string to float: No such variable publisher".

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?
Snow Sciles is offline   Reply With Quote
Old 05-21-2014, 10:26 AM   #8
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
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.
eschwartz is offline   Reply With Quote
Old 05-21-2014, 11:12 AM   #9
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,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Snow Sciles View Post
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')
)
No. Not-equal strings can compare as less than or greater than, so those two should probably return the same thing.
Quote:
eschwartz, your enthusiasm has convinced me to try program mode. 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
        );
This works on books that have ao3 or ffnet as publisher. But for those that do not, instead of seeing the default wikipedia link, I get "Could not convert string to float: No such variable publisher".

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 function first_matching_cmp is comparing numbers, not strings. That is why you are getting the error. Also, the function uses a "less than" comparison, which is probably not what you want.

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
        );
The regexp anchors are required because "switch", like "contains", uses patterns.
Quote:
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?
It will probably have no effect. Rules are converted to GPM templates. The exception is if your hand-built template is significantly less complex than the generated template.

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.
chaley is offline   Reply With Quote
Old 05-22-2014, 11:01 AM   #10
Snow Sciles
Connoisseur
Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.
 
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.
Snow Sciles is offline   Reply With Quote
Old 05-26-2014, 04:57 PM   #11
Snow Sciles
Connoisseur
Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.
 
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.
Snow Sciles is offline   Reply With Quote
Old 05-26-2014, 05:06 PM   #12
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,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Snow Sciles View Post
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.
If you let the mouse pointer hover over that template box you will see that the only two fields allowed are author and author_sort. So no, your template will not work in that box.

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.
chaley is offline   Reply With Quote
Old 05-26-2014, 05:16 PM   #13
Snow Sciles
Connoisseur
Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.Snow Sciles ought to be getting tired of karma fortunes by now.
 
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.
Snow Sciles is offline   Reply With Quote
Reply


Forum Jump

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


All times are GMT -4. The time now is 04:47 AM.


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