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 09-18-2014, 10:30 PM   #571
crissman
Connoisseur
crissman could sell banana peel slippers to a Deveel.crissman could sell banana peel slippers to a Deveel.crissman could sell banana peel slippers to a Deveel.crissman could sell banana peel slippers to a Deveel.crissman could sell banana peel slippers to a Deveel.crissman could sell banana peel slippers to a Deveel.crissman could sell banana peel slippers to a Deveel.crissman could sell banana peel slippers to a Deveel.crissman could sell banana peel slippers to a Deveel.crissman could sell banana peel slippers to a Deveel.crissman could sell banana peel slippers to a Deveel.
 
Posts: 77
Karma: 3090
Join Date: Apr 2013
Location: California
Device: Kobo Aura One, Kobo Aura H20,Kindle PW1
Quote:
Originally Posted by eschwartz View Post
Ah, yes.

The brackets are reserved in template mode. That's why I like general program mode. Well, that and you can limit yourself to only using one custom column.

Use the general program mode version found here: http://manual.calibre-ebook.com/temp...l-program-mode
Simply fix the "stripped" variable.

PHP Code:
program:
#       compute the equivalent of the composite fields and store them in local variables
stripped re(field('series'), '^(A|The|An)\s+''');
shortened shorten(stripped4'-' ,4);
initials re(stripped'[\s]?([^\s])[^\s]+(\s|$)''\1');

#       Format the series index. Ends up as empty if there is no series index.
#       Note that leading and trailing spaces will be removed by the formatter,
#       so we cannot add them here. We will do that in the strcat below.
#       Also note that because we are in 'program' mode, we can freely use
#       curly brackets in strings, something we cannot do in template mode.
s_index template('{series_index:0>2.0f}');

#       print(stripped, shortened, initials, s_index);

#       Now concatenate all the bits together. The switch picks between
#       initials and shortened, depending on whether there is a space
#       in stripped. We then add the brackets around s_index if it is
#       not empty. Finally, add the title. As this is the last function in
#       the program, its value will be returned.
strcat(
    switch(
        
stripped,
        
'.\s',
        
initials,
        
'.',
        
shortened,
        
field('series')
    ),
    
test(
        
s_index,
        
strcat(' ['s_index'] '),
        
''
    
),
    
field('title')
); 
This worked like a charm, I apprehension I felt when I initially saw this was quite daunting, I did however manage it without a problem. I have absolutely no programming skills whatsoever being more technically inclined.

Also the regex was exactly what I was looking for which just proves I was correct in asking in the first place. I originally thought it was an un-modifiable field other then manually entering in something or downloading the data.

I am very grateful for all your help, a couple of days ago my laptop died and then by backup was corrupted, so I lost everything I had with the exception to what I had stored in the cloud and felt that this was the best time to recreate stuff that I have been wanting to solve for a little while.

So new Laptop and backup drive and calibre is at the point to where I can just sit back and enjoy it.

I do have one small thing that is eluding if you wouldn't mind once again helping me with.

In Metadata Plugboards I have template set up for:

Epub and Any Device
Source template: {title} - {series}{series_index:0>3s| [|]
Destination Field: title

When dealing with series it works perfectly, but when dealing with a book that is not part of a series I have a dash leftover

Example: Ken Follets Book: Edge of Eternity
becomes:
Edge of Eternity - The Century Trilogy [03]

Cottam, F. G. Book: The Lazarus Prophecy
becomes:
The Lazarus Prophecy -

How can I get it to drop the "-" at the end of the title on a book with no series? Yet keep it for series?

Thank you very much eschwartz!
crissman is offline   Reply With Quote
Old 09-18-2014, 10:43 PM   #572
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,421
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Use:
Code:
{title}{series:| - |}{series_index:0>3s| [|]}
Explanation (see http://manual.calibre-ebook.com/temp...ced-formatting for the gory details):

Everything after the ":" is a modifier. The "|stuff|stuff" are prefixes and suffixes for the parent template (in this case, series) -- as you can see, you already have one for series_index.

This allows for making stuff dependent on the parent template existing. So. I simply moved the space and dash to be dependent on the series field not being empty.

Last edited by eschwartz; 09-18-2014 at 10:45 PM.
eschwartz is offline   Reply With Quote
Advert
Old 09-18-2014, 10:45 PM   #573
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,421
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Quote:
Originally Posted by crissman View Post
This worked like a charm, I apprehension I felt when I initially saw this was quite daunting, I did however manage it without a problem. I have absolutely no programming skills whatsoever being more technically inclined.
Solutions here are copy-paste, because we know not everyone need have pogramming skills.

Quote:
Also the regex was exactly what I was looking for which just proves I was correct in asking in the first place. I originally thought it was an un-modifiable field other then manually entering in something or downloading the data.

I am very grateful for all your help, a couple of days ago my laptop died and then by backup was corrupted, so I lost everything I had with the exception to what I had stored in the cloud and felt that this was the best time to recreate stuff that I have been wanting to solve for a little while.

Thank you very much eschwartz!
You're welcome.
eschwartz is offline   Reply With Quote
Old 09-19-2014, 02:11 AM   #574
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,743
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by eschwartz View Post
Everything after the ":" is a modifier. The "|stuff|stuff" are prefixes and suffixes for the parent template (in this case, series) -- as you can see, you already have one for series_index.

This allows for making stuff dependent on the parent template existing. So. I simply moved the space and dash to be dependent on the series field not being empty.
FWIW: you can use the finish_formatting function instead of the template and test to format it and add the spaces and brackets. Something like
Code:
s_index = finish_formatting(field('series_index'), '.2f', ' [', ']'))
chaley is offline   Reply With Quote
Old 09-19-2014, 06:32 AM   #575
BetterRed
null operator (he/him)
BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.
 
Posts: 20,591
Karma: 26954694
Join Date: Mar 2012
Location: Sydney Australia
Device: none
If I want
Code:
if (tags:count(,) > 5)
     return "too many" 
else
     return ""
endif
Why cant I do this

Code:
{cmp(tags:count(','), 5, "", "", "too many")}
BR
BetterRed is offline   Reply With Quote
Advert
Old 09-19-2014, 06:36 AM   #576
DMee
Zealot
DMee began at the beginning.
 
Posts: 116
Karma: 10
Join Date: Jun 2011
Location: Stockport, UK
Device: ipad, iphone, kindle
Quote:
Originally Posted by eschwartz View Post
Ah, yes.

The brackets are reserved in template mode. That's why I like general program mode. Well, that and you can limit yourself to only using one custom column.

Use the general program mode version found here: http://manual.calibre-ebook.com/temp...l-program-mode
Simply fix the "stripped" variable.
Getting there :-)

I changed formulas to

stripped = re(field('series'), '^(A|The|An)\s+', '');
shortened = shorten(stripped, 8, '-' ,6);
initials = re(stripped, '[\s]?([^\s]{1,3})[^\s]+(\s|$)', '\1');

But summats wrong

Worlds Without End
becomes
WorWitEn

Missing "d"?
Also is it poss to have it as

Wor-Wit-End

Also, is it feasible to add in the code we rejigged when doing series with fractions? you had a prog to decide which formula to use to reformat the series link properly depending on size of highest number and presence of fractions :-)
DMee is offline   Reply With Quote
Old 09-19-2014, 07:17 AM   #577
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,743
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by BetterRed View Post
If I want
Code:
if (tags:count(,) > 5)
     return "too many" 
else
     return ""
endif
Why cant I do this

Code:
{cmp(tags:count(','), 5, "", "", "too many")}
BR
There are three different template program modes. The first, single function mode (SFM), has the approximate syntax
Code:
{ fieldKey : format/function | prefix | suffix }
You don't quote anything, and the value of the field is implicitly passed to the one function (if there is one).

The second is template program mode (TPM), and has the approximate syntax
Code:
{ field name :'possibly nested functions'}
Note the apostrophes. I left off the prefix and suffix. You can use arbitrary function nesting in TPM. There are no implicit parameters. TPM has problems if the function arguments contain { } characters, because these are processed by the outer macro processor before the template compiler is called.

The third is general program mode (GPM). This mode has a more "classic" program style. It is considered as one template, where the other two can be chains of templates. GPM has no restrictions on characters used. It is the most expressive of all the three.

Unfortunately your example uses nested functions but does not use TPM syntax. It also uses attribute-like referencing (tags:count) which doesn't exist in the language.

The equivalent in TPM is
Code:
{tags:'cmp(count($, ','), 5, "", "", "too many")'}
In GPM it could be (indentation fully optional):
Code:
program: 
	cmp(
		count(
			field('tags'), 
			','), 
		5, 
		"", 
		"", 
		"too many")
chaley is offline   Reply With Quote
Old 09-19-2014, 09:36 AM   #578
DMee
Zealot
DMee began at the beginning.
 
Posts: 116
Karma: 10
Join Date: Jun 2011
Location: Stockport, UK
Device: ipad, iphone, kindle
Whilst I am asking - how would I get a column of the length of the title - I have some that have ended up massively long and I want to quickly find them and edit them :-)
DMee is offline   Reply With Quote
Old 09-19-2014, 09:50 AM   #579
DMee
Zealot
DMee began at the beginning.
 
Posts: 116
Karma: 10
Join Date: Jun 2011
Location: Stockport, UK
Device: ipad, iphone, kindle
Quote:
Originally Posted by DMee View Post
Getting there :-)

I changed formulas to

stripped = re(field('series'), '^(A|The|An)\s+', '');
shortened = shorten(stripped, 8, '-' ,6);
initials = re(stripped, '[\s]?([^\s]{1,3})[^\s]+(\s|$)', '\1');

But summats wrong
Actually its not handling and words 3 or less properly - loses last letter??
DMee is offline   Reply With Quote
Old 09-19-2014, 10:38 AM   #580
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,743
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by DMee View Post
Whilst I am asking - how would I get a column of the length of the title - I have some that have ended up massively long and I want to quickly find them and edit them :-)
The template is
Code:
{title:strlen()}
Quote:
Originally Posted by DMee View Post
Actually its not handling and words 3 or less properly - loses last letter??
Try
Code:
initials = re(stripped, '[\s]?([^\s]{1,3})[^\s]*(\s|$)', '\1');
Quote:
Originally Posted by DMee View Post
Also is it poss to have it as

Wor-Wit-End
Does this do what you want?
Code:
initials = re(list_re(stripped, ' ', '[\s]?([^\s]{1,3})[^\s]*(\s|$)', '\1'), ' ', '-');
chaley is offline   Reply With Quote
Old 09-19-2014, 11:35 AM   #581
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,421
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Quote:
Originally Posted by chaley View Post
FWIW: you can use the finish_formatting function instead of the template and test to format it and add the spaces and brackets. Something like
Code:
s_index = finish_formatting(field('series_index'), '.2f', ' [', ']'))
Thanks.

I already know about it, though. I do use it when in general program mode.

EDIT: It came true sooner than I thought. Look at the recycled s_index below.

Last edited by eschwartz; 09-19-2014 at 11:54 AM.
eschwartz is offline   Reply With Quote
Old 09-19-2014, 11:52 AM   #582
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,421
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Quote:
Originally Posted by DMee View Post
Getting there :-)

I changed formulas to

stripped = re(field('series'), '^(A|The|An)\s+', '');
shortened = shorten(stripped, 8, '-' ,6);
initials = re(stripped, '[\s]?([^\s]{1,3})[^\s]+(\s|$)', '\1');

But summats wrong

Worlds Without End
becomes
WorWitEn

Missing "d"?
Well, chaley has already answered this.

The previous template assumed only the first letter would be captured, and matched the following letters outside the capture. But it required there to be letters outside, under the assumption that any word would have at least two letters. So at least one letter at the end would not be included.

hence chaley's fix.

Quote:
Also is it poss to have it as

Wor-Wit-End
@chaley, cool use of list_re() -- I am still trying to get my head around using them. Well, list_re_group() specifically. I'm sure they will eventually click into place though.

Thanks, and thank you for all these recent extensions to template programming! They will no doubt make things easier as soon as I get the hang of them.

Quote:
Also, is it feasible to add in the code we rejigged when doing series with fractions? you had a prog to decide which formula to use to reformat the series link properly depending on size of highest number and presence of fractions :-)
You mean this?

PHP Code:
program:

ser_num test(
    
field('#complex_series'),
    
finish_formatting(
        
field('series_index'),
        
'_>7.2f',
        
' [',
        
'] '
    
),
    
finish_formatting(
        
field('series_index'),
        
'_>4d',
        
' [',
        
'] '
    
),
);

strcat(
    
field('series'),
    
ser_num,
    
field('title')

Um, lessee...

Ah, it's easy. Just replace s_index.

PHP Code:
program:
#       compute the equivalent of the composite fields and store them in local variables
stripped re(field('series'), '^(A|The|An)\s+''');
shortened shorten(stripped4'-' ,4);
initials re(list_re(stripped' ''[\s]?([^\s]{1,3})[^\s]*(\s|$)''\1'), ' ''-');

#       Format the series index. Ends up as empty if there is no series index.
#       Note that leading and trailing spaces will be removed by the formatter,
#       so we cannot add them here. We will do that in the strcat below.
#       Also note that because we are in 'program' mode, we can freely use
#       curly brackets in strings, something we cannot do in template mode.
#
#       Using the new ser_num calculation from https://www.mobileread.com/forums/showpost.php?p=2916022&postcount=548
s_index test(
    
field('#complex_series'),
    
finish_formatting(
        
field('series_index'),
        
'_>7.2f',
        
' [',
        
'] '
    
),
    
finish_formatting(
        
field('series_index'),
        
'_>4d',
        
' [',
        
'] '
    
),
);

#       print(stripped, shortened, initials, s_index);

#       Now concatenate all the bits together. The switch picks between
#       initials and shortened, depending on whether there is a space
#       in stripped. We then add the brackets around s_index if it is
#       not empty. Finally, add the title. As this is the last function in
#       the program, its value will be returned.
strcat(
    switch(
        
stripped,
        
'.\s',
        
initials,
        
'.',
        
shortened,
        
field('series')
    ),
    
test(
        
s_index,
        
strcat(' ['s_index'] '),
        
''
    
),
    
field('title')
); 
EDIT: added chaley's fix to the new template.

Using the second fix, to get "Wor-Wit-End" style.

Last edited by eschwartz; 09-19-2014 at 12:07 PM. Reason: remembered to use chaley's fix.
eschwartz is offline   Reply With Quote
Old 09-19-2014, 01:16 PM   #583
DMee
Zealot
DMee began at the beginning.
 
Posts: 116
Karma: 10
Join Date: Jun 2011
Location: Stockport, UK
Device: ipad, iphone, kindle
Nearly there - somethings wrong with

#complex_series

If I cange it to

series_index

it works, but seems to return

__01.00

for even simple 1,2,3 series numbers

also shows as

[ [__01.00] ]

Can we replace _ with space as its in program mode?
DMee is offline   Reply With Quote
Old 09-19-2014, 01:30 PM   #584
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,421
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Quote:
Originally Posted by DMee View Post
Nearly there - somethings wrong with

#complex_series
What exactly is wrong with it? I cannot think why it wouldn't work, especially as it seemed to work before?

Quote:
If I cange it to

series_index

it works, but seems to return

__01.00

for even simple 1,2,3 series numbers
That is because #complex_series is supposed to indicate by having content that it should receive the complex formatting. If you test for series_index having content, it will return true whenever there is ANY series_index.

Quote:
also shows as

[ [__01.00] ]

Can we replace _ with space as its in program mode?
I thought it was supposed to be there on account of spaces will get stripped down to a max of one.
eschwartz is offline   Reply With Quote
Old 09-19-2014, 01:36 PM   #585
DMee
Zealot
DMee began at the beginning.
 
Posts: 116
Karma: 10
Join Date: Jun 2011
Location: Stockport, UK
Device: ipad, iphone, kindle
My bad re comples_series - I had forgotten we went for a manual Y/N re complex series

I was thinking of the code

https://www.mobileread.com/forums/sho...&postcount=552

To set the correct format.

I thought the code before the latest tweak took care of leading spaces later on - yours adds brackets and then original adds 2 more brackets later on :-)
DMee is offline   Reply With Quote
Reply

Tags
custom column, tag, tags


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
custom column i need a little help shinken Calibre 3 09-15-2010 03:41 AM
Using Custom Metadata in Save Template EJvdH Calibre 1 07-02-2010 06:06 AM
Accessories Decalgirl Kindle 2 custom skin template srmalloy Amazon Kindle 6 04-09-2010 09:55 PM
Donations for Custom Recipes ddavtian Calibre 5 01-23-2010 04:54 PM
Help understanding custom recipes andersent Calibre 0 12-17-2009 02:37 PM


All times are GMT -4. The time now is 06:12 AM.


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