04-28-2011, 07:23 AM | #1 |
Calibre Plugins Developer
Posts: 4,636
Karma: 2162064
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
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? |
04-28-2011, 08:23 AM | #2 |
Grand Sorcerer
Posts: 11,731
Karma: 6690881
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Can't you treat each line as a separate template?
|
Advert | |
|
04-28-2011, 09:12 AM | #3 |
Calibre Plugins Developer
Posts: 4,636
Karma: 2162064
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
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?
|
04-28-2011, 09:29 AM | #4 |
Grand Sorcerer
Posts: 11,731
Karma: 6690881
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
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) Am I missing something? |
04-28-2011, 09:40 AM | #5 |
Calibre Plugins Developer
Posts: 4,636
Karma: 2162064
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
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... |
Advert | |
|
04-28-2011, 09:52 AM | #6 |
Grand Sorcerer
Posts: 11,731
Karma: 6690881
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
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 09:54 AM. |
04-28-2011, 10:08 AM | #7 |
Calibre Plugins Developer
Posts: 4,636
Karma: 2162064
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
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.
|
Thread Tools | Search this Thread |
|
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 07:09 PM |
HTML Conversion - Multiline Headers | prky | Calibre | 1 | 07-03-2010 09:24 AM |
Multiline Regex? | prky | Calibre | 25 | 05-01-2010 09:56 PM |
Multiline Regex Footer | hover | Calibre | 10 | 02-03-2010 04:23 AM |
Good text to java midlet, to read text in phones | sucahyo | Workshop | 1 | 02-18-2005 12:56 PM |