Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 04-28-2011, 08:23 AM   #1
kiwidude
calibre/Sigil Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,258
Karma: 1579358
Join Date: Oct 2010
Location: London, UK
Device: Kindle Paperwhite, iPad Pro
Using composite_formatter.safe_format with multiline text?

I'm moving an email conversation to here.

I am enhancing the Generate Cover plugin to support template driven expressions from a text input field. I thought it would be nice if I changed the input to be a multiline QTextEdit, thereby allowing the user to do multiline expressions like:

Genre: {tags}\n
Published: {pubdate}

However the problem I have is that composite_formatter.safe_format() strips newline characters out. Now if I substitute in something else like <br/> it will work, but then it will blow up if you try to use a multiline complex {} expression.

Any suggestions? I need something that template formatter won't strip out that I can split on afterwards, but that won't cause multi-line formulas to blow up...

Or do we just tell users not to do multi-line formulas?
kiwidude is offline   Reply With Quote
Old 04-28-2011, 09:23 AM   #2
chaley
Grumpy old git
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.
 
chaley's Avatar
 
Posts: 10,005
Karma: 3559369
Join Date: Jan 2010
Location: UK
Device: Reader
Can't you treat each line as a separate template?
chaley is offline   Reply With Quote
Advert
Old 04-28-2011, 10:12 AM   #3
kiwidude
calibre/Sigil Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,258
Karma: 1579358
Join Date: Oct 2010
Location: London, UK
Device: Kindle Paperwhite, iPad Pro
I don't think so? I can't split before I call the template formatter, because I might be splitting in a formula. I was trying to avoid complicating my UI (and all the code related to drawing) by having multiple UI controls. Or were you thinking of something else?
kiwidude is offline   Reply With Quote
Old 04-28-2011, 10:29 AM   #4
chaley
Grumpy old git
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.
 
chaley's Avatar
 
Posts: 10,005
Karma: 3559369
Join Date: Jan 2010
Location: UK
Device: Reader
I was assuming that the values between the { } characters never contain newlines. I don't see why they should.

If this is true, then the following should work.
Code:
lines = text.split('\n')
result = []
for l in lines:
    result.append(composite_formatter.safe_format(l, mi, 'XXX', mi))
val = '\n'.join(result)
This code gives you back the multiline text, with template fields inserted.

Am I missing something?
chaley is offline   Reply With Quote
Old 04-28-2011, 10:40 AM   #5
kiwidude
calibre/Sigil Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,258
Karma: 1579358
Join Date: Oct 2010
Location: London, UK
Device: Kindle Paperwhite, iPad Pro
Well, I was looking at your examples of what people "could" do in templates with functions and program mode. Your examples on the help page show multi-line templates inside the {}.

Now if you say those are "not likely" scenarios or something I needn't bother supporting then fair enough...
kiwidude is offline   Reply With Quote
Advert
Old 04-28-2011, 10:52 AM   #6
chaley
Grumpy old git
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.
 
chaley's Avatar
 
Posts: 10,005
Karma: 3559369
Join Date: Jan 2010
Location: UK
Device: Reader
General program mode templates do not go between {} characters. They must begin with the word 'program:' and the program goes until the end of the string. The concept of a multiline template goes away, because the entire string is one template. It makes little sense to me for someone to use a general program mode template in the situation you are describing. If they *really* need something of that complexity to generate a field value, then they can use a composite column or build a python template function.

Template program mode expressions can in theory be multiline, but in practice they never are. I think it is safe not to support them.

One thing that my suggestion does not cover is fields that contain explicit newlines, where these newlines are significant. Non-multiple text fields come to mind. These normally use HTML, so I am not convinced this is a problem. Is it?

BTW: I have no particular problem with adding a parameter to safe_formatter not to collapse white space. I just want to be sure it is needed before I do it. If I do this, then you could pass the entire string in one go. The downside is that there would be no white space processing, so templates like
"abc {series} def" would evaluate to "abc def" (two spaces) if there is no series. Perhaps this is OK.

Last edited by chaley; 04-28-2011 at 10:54 AM.
chaley is offline   Reply With Quote
Old 04-28-2011, 11:08 AM   #7
kiwidude
calibre/Sigil Developer
kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.kiwidude ought to be getting tired of karma fortunes by now.
 
Posts: 4,258
Karma: 1579358
Join Date: Oct 2010
Location: London, UK
Device: Kindle Paperwhite, iPad Pro
If you are happy multiple template expressions aren't frequently needed, that is good enough for me. I just wanted to make sure I wasn't putting an unreasonable restriction in. So don't bother about the parameter, thanks.
kiwidude is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make Amazon Kindle Text to Speech skip over some text xsaero00 Kindle Developer's Corner 3 06-18-2011 08:09 PM
HTML Conversion - Multiline Headers prky Calibre 1 07-03-2010 10:24 AM
Multiline Regex? prky Calibre 25 05-01-2010 10:56 PM
Multiline Regex Footer hover Calibre 10 02-03-2010 05:23 AM
Good text to java midlet, to read text in phones sucahyo Workshop 1 02-18-2005 01:56 PM


All times are GMT -4. The time now is 02:19 PM.


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