From 06039b300c3a62fb58c7833b74fa9abea20e1c9b Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Wed, 12 Jun 2013 10:30:20 +0200 Subject: [PATCH] [pod] Line breaks which are found within pod expression results are converted to odf line-breaks into the pod result. --- pod/actions.py | 4 ++-- pod/buffers.py | 17 ++++++++++------- shared/xml_parser.py | 12 ++++++++++-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/pod/actions.py b/pod/actions.py index 66c1b46..82fab5e 100644 --- a/pod/actions.py +++ b/pod/actions.py @@ -55,7 +55,7 @@ class BufferAction: self.fromExprResult = None # When an error is encountered, must we raise it or write it into the # buffer? - self.raiseErrors = self.buffer.caller() == 'px' + self.raiseErrors = self.buffer.caller == 'px' def getExceptionLine(self, e): '''Gets the line describing exception p_e, containing the pathname of @@ -66,7 +66,7 @@ class BufferAction: '''Write the encountered error into the buffer or raise an exception if self.raiseErrors is True.''' if self.raiseErrors: - if self.buffer.caller() == 'px': + if self.buffer.caller == 'px': # Add in the error message the line nb where the errors occurs # within the PX. locator = self.buffer.env.parser.locator diff --git a/pod/buffers.py b/pod/buffers.py index 31fcfd0..9e5f2ae 100644 --- a/pod/buffers.py +++ b/pod/buffers.py @@ -121,6 +121,8 @@ class Buffer: self.parent = parent self.subBuffers = {} # ~{i_bufferIndex: Buffer}~ self.env = env + # Are we computing for pod or for px ? + self.caller= (env.__class__.__name__=='PxEnvironment') and 'px' or 'pod' def addSubBuffer(self, subBuffer=None): if not subBuffer: @@ -182,7 +184,13 @@ class Buffer: def dumpContent(self, content): '''Dumps string p_content into the buffer.''' - self.write(escapeXml(content)) + if self.caller == 'pod': + # Take care of converting line breaks to odf line breaks. + content = escapeXml(content, format='odf', + nsText=self.env.namespaces[self.env.NS_TEXT]) + else: + content = escapeXml(content) + self.write(content) # ------------------------------------------------------------------------------ class FileBuffer(Buffer): @@ -635,7 +643,7 @@ class MemoryBuffer(Buffer): if escape: result.dumpContent(res) else: result.write(res) except Exception, e: - if self.caller() == 'pod': + if self.caller == 'pod': PodError.dump(result, EVAL_EXPR_ERROR % ( evalEntry.expr, e), dumpTb=False) else: # px @@ -654,9 +662,4 @@ class MemoryBuffer(Buffer): def clean(self): '''Cleans the buffer content.''' self.content = u'' - - def caller(self): - '''Returns "pod" if the caller is appy.pod, "px" if it is appy.px.''' - if self.env.__class__.__name__ == 'PxEnvironment': return 'px' - return 'pod' # ------------------------------------------------------------------------------ diff --git a/shared/xml_parser.py b/shared/xml_parser.py index 408ab3c..14f13a8 100644 --- a/shared/xml_parser.py +++ b/shared/xml_parser.py @@ -66,16 +66,24 @@ for k, v in htmlentitydefs.entitydefs.iteritems(): if not HTML_ENTITIES.has_key(k) and not XML_ENTITIES.has_key(k): HTML_ENTITIES[k] = '' -def escapeXml(s): +def escapeXml(s, format='xml', nsText='text'): '''Returns p_s, whose XML special chars have been replaced with escaped XML - entities.''' + entities. If p_format is "odf", line breaks are converted to ODF line + breaks. In this case, it is needed to give the name of the "text" + namespace (p_nsText) as defined in the ODF document where the line breaks + must be inserted.''' if isinstance(s, unicode): res = u'' else: res = '' + odf = format == 'odf' for c in s: if XML_SPECIAL_CHARS.has_key(c): res += XML_SPECIAL_CHARS[c] + elif odf and (c == '\n'): + res += '<%s:line-break/>' % nsText + elif odf and (c == '\r'): + pass else: res += c return res