Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre

Notices

Reply
 
Thread Tools Search this Thread
Old 01-25-2022, 05:26 PM   #1
rdyornot
Member
rdyornot began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Jul 2013
Device: none
Template Function SEPARATORS - valid characters

In template functions that require a separator, what characters are valid?

I'm trying to get the first word of a field using sublist()
{series:sublist(0₁1,❓)}

I'd like to use a space, but it won't accept that, nor if I escape it, or use \s, put it in quotes... so - how can it be done, is there a way to separate with a space, or should I be using a different function (sublist seemed the most obvious)?
rdyornot is offline   Reply With Quote
Old 01-25-2022, 05:30 PM   #2
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 8,758
Karma: 62032371
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Check out the template reference if you've not already.
ownedbycats is offline   Reply With Quote
Advert
Old 01-25-2022, 05:33 PM   #3
rdyornot
Member
rdyornot began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Jul 2013
Device: none
I've looked through it several times, read through it and tried various searches - it never specifies valid characters for separators. It also doesn't seem to have come up in the forums before.

By the way - thanks to Ownedbycats and Capink for answering my previous question, without them I couldn't ask this next one!
rdyornot is offline   Reply With Quote
Old 01-25-2022, 06:05 PM   #4
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,768
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by rdyornot View Post
I've looked through it several times, read through it and tried various searches - it never specifies valid characters for separators. It also doesn't seem to have come up in the forums before.
The syntax of Single Function Mode, which you are using, is extremely basic. There is no such thing as a string. Spaces are not ignored. Commas are sometimes ignored. As the documentation says:
Quote:
Important: If you have programming experience, please note that the syntax in Single Function Mode is not what you expect. Strings are not quoted and spaces are significant. All arguments are considered to be constants; there are no expressions.
You should use either Template Program Mode or General Program Mode. Both use a traditional syntax that separates strings from characters in the program. TPM's syntax is more constrained because it must live inside template snippets.

As for list separators, that is up to you. Any string can be used as a separator in the Template or General Program modes. Calibre uses both comma and ampersand, depending on the data. In some cases it uses a colon, for example in identifiers. You seem to want to split a space-separated phrase into words, which is possible in the more general modes but isn't something calibre does with its standard data types.

Last edited by chaley; 01-25-2022 at 06:14 PM. Reason: missing word
chaley is offline   Reply With Quote
Old 01-25-2022, 07:27 PM   #5
rdyornot
Member
rdyornot began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Jul 2013
Device: none
Quote:
Originally Posted by chaley View Post
The syntax of Single Function Mode, which you are using, is extremely basic. There is no such thing as a string. Spaces are not ignored. Commas are sometimes ignored. As the documentation says:

You should use either Template Program Mode or General Program Mode. Both use a traditional syntax that separates strings from characters in the program. TPM's syntax is more constrained because it must live inside template snippets.

As for list separators, that is up to you. Any string can be used as a separator in the Template or General Program modes. Calibre uses both comma and ampersand, depending on the data. In some cases it uses a colon, for example in identifiers. You seem to want to split a space-separated phrase into words, which is possible in the more general modes but isn't something calibre does with its standard data types.
Thank you - I was afraid of something like this. I'm trying to get the first one or two words from a book or series title - so i'm afraid as far as characters go anything but a space just won't work. If the function won't take a regex or escape character I'll just have to find some other work around.

I'm not much of a programmer, I'm mostly just making semi-educated guesses based on a couple of classes I took 20 years ago (C++ & tried to self teach Obc-C) so no experience with python. When I skimmed through the docs for Program modes I got the impression that it would probably take less time for me to just manually change the text of the books that are causing me problems than it would to learn to solve this programatically.

I thank everyone for their time & especially those who responded!
rdyornot is offline   Reply With Quote
Advert
Old 01-25-2022, 08:20 PM   #6
davidfor
Grand Sorcerer
davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.
 
Posts: 24,906
Karma: 47303748
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
Quote:
Originally Posted by rdyornot View Post
Thank you - I was afraid of something like this. I'm trying to get the first one or two words from a book or series title - so i'm afraid as far as characters go anything but a space just won't work. If the function won't take a regex or escape character I'll just have to find some other work around.
What you have is almost there. It is putting a space in place of the question mark:

Code:
{series:sublist(0,2, )}
That gets the first two words of the series. And to be clear, there is a space between the comma and the right parenthesis.

But, as @chaley mentioned, there are three modes. I don't like the above version. I would use General Program mode:
Code:
program:
sublist($series, 0, 2, " ")
I can read that and understand it, and there is no reliance on exact formatting of the template.

You could do this with regex:

Code:
program:
re($series, "^(.*? .*?\s).*", "\1")
Or maybe

Code:
program:
re($series, "^(.*?[\s:].*?[\s:]).*", "\1")
If you want to allow any white space or a colon as the separator. Which of the above is suitable depends on what it is you are trying to do.
davidfor is offline   Reply With Quote
Old 01-25-2022, 09:54 PM   #7
rdyornot
Member
rdyornot began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Jul 2013
Device: none
Quote:
Originally Posted by davidfor View Post
What you have is almost there. It is putting a space in place of the question mark:

Code:
{series:sublist(0,2, )}
I would have sworn up and down that this was the first thing I tried. Thank you - now I need to go think about what I'm doing with my life.
rdyornot is offline   Reply With Quote
Old 01-26-2022, 03:58 AM   #8
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,768
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by davidfor View Post
What you have is almost there. It is putting a space in place of the question mark:

Code:
{series:sublist(0,2, )}
That gets the first two words of the series. And to be clear, there is a space between the comma and the right parenthesis.
This snippet in TPM also works.
Code:
{series:'sublist($, 0, 2,' ')'}
In TPM spaces aren't significant except in strings. TPM might be easier to use if the complete template has several {} sections.

But like @davidfor, I prefer GPM. They are easier to read and can be faster.
chaley is offline   Reply With Quote
Reply

Tags
functions, syntax, template language, templates


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BuiltinAdd template language function DyckBook Editor 4 10-19-2021 04:21 PM
Bulk metadata Search/Replace: template function question meghane_e Library Management 3 01-24-2019 09:32 PM
Using built-in template functions in a custom template function ilovejedd Library Management 4 01-28-2018 12:20 PM
Problem with contains function in save template MicaOlaAdams Calibre 5 10-21-2016 10:25 AM
how to use re() function in Template Program Mode? msciwoj Library Management 3 07-07-2016 03:55 PM


All times are GMT -4. The time now is 07:21 PM.


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