View Single Post
Old 09-07-2014, 03:41 AM   #1
rebl
r.eads e.njoys b.ooks lol
rebl ought to be getting tired of karma fortunes by now.rebl ought to be getting tired of karma fortunes by now.rebl ought to be getting tired of karma fortunes by now.rebl ought to be getting tired of karma fortunes by now.rebl ought to be getting tired of karma fortunes by now.rebl ought to be getting tired of karma fortunes by now.rebl ought to be getting tired of karma fortunes by now.rebl ought to be getting tired of karma fortunes by now.rebl ought to be getting tired of karma fortunes by now.rebl ought to be getting tired of karma fortunes by now.rebl ought to be getting tired of karma fortunes by now.
 
rebl's Avatar
 
Posts: 76
Karma: 580748
Join Date: Mar 2010
Location: It's time to get this Book a Rest
Device: Kindle 4 NT
using re() and shorten() for custom comments column?

Hello all,

I'm trying to build a custom comments column to display in the library list, that would show text from the comments filed, stripping (removing) any html tags and shortening the string to a predefined length.
I've been looking all over for more information but all I found is not enough.
Especially, I don't understand a few things:
1. When building a custom column from other columns, according to the tooltip in the GUI, "Field template uses the same syntax as save tamplates"
but the first google search for calibre re() function takes me to the calibre documentation (template reference), where the re() function has three arguments: re(val, pattern, replacement)
In the end I've found the correct page: http://manual.calibre-ebook.com/template_lang.html where only two arguments are needed.
But what is the diference betwenn the two? Where is the 3-argument version applied if the custom colums and save templates use the 2-argument one?

2. I suppose there is no (simple) way to combine two functions in a custom field? I've read somewhere here on the forum about the simple mode being the default and about some programing or advanced mode(s) but I haven't figured out how to you use the other two.

If I use
Code:
{comments:shorten(50,...,0)}
I get a shortened string but also html tags.

But I would like to remove the HTML tags too, so i'd need a function like:
Code:
{comments:re((\<.+\>)?,):shorten(50,...,0)}
Please ignore for the moment how efficiently written is the re() function.
I know the above does not work, but how exactly could I appy two functions to the same input field (comments) for a custom column?

3. I have some strange problem with the re() function and I would really appreciate your help.
If I use
Code:
{comments:re((\<.+\>)?,)}
The various html tags (
Code:
<hr> <div> </div>
etc) are removed but in some case (when a comma is present) the resulting string is reversed around the comma and I can't figure out why. It's like switching \2 \1 when swapping author surname/name.
Also in some other cases the whole test is removed (like a greedy behavior) and the output is empty.
This behavior is not 100% reproducible and this puzzles me, because when I've done some minor metadata edit in the comment field, the output became the expected one. But other time(s) after a minor edit like adding a space in the html source of the comment the whole output disappeared.
I had a comment similar to this (for a Thomas Mann book in case you're wondering):
Code:
<hr> 
A portrait of the German bourgeois society, throughout several decades of the 19th century.
When it was in Romanian (it was like
Code:
<hr> Some text 1, some text 2.
) the function's result was:
Code:
some text 2., some text 1
...and I have this kind result for most if not all of the books.
But after replacing the comment with the English translation while keeping the tags intact, (just for doing some tests and for pasting here a more meaningful comment ) it suddenly started working and now the results is the expected text without html.
My question(s) are why the editing solved the problem since I've not altered the text pattern and why the re() does not work for the other books and instead it reversed the text fragments around the comma.

Another example of the strange behavior of this custom column:
comments field:
Code:
<p class="description">Premiile British SF Association şi John W. Campbell Memorial, 2013.</p>
initial output:
Code:
2013., Premiile British SF Association şi John W. Campbell Memorial
after editing the comment (I've just inserted an additional space between W. and C) the result was just EMPTY, no text at all!!!

4. Regarding the (\<.+\>)? part i wanted a "not greedy" matching of strings that start with < and end with > but matching the shortest possible string. I'm not sure it works though.

Thank you all for your time and for any help on this!

Last edited by rebl; 09-07-2014 at 04:35 AM. Reason: (\<.+\>)?
rebl is offline   Reply With Quote