From e8c63f225f4d460af3182cc2e4da7d2a446764bb Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Mon, 27 May 2013 22:32:18 +0200 Subject: [PATCH] [pod] Within the context of a 'for' statement, loop objects now have additional boolean attributes named 'first' and 'last' that allow to know if the currently walked element is, respectively, the first of the last element of the whole list. Added params 'pageBreakBefore' and 'pageBreakAfter' to OdtImporter and PodImporter. --- pod/actions.py | 6 ++++++ pod/doc_importers.py | 33 ++++++++++++++++++++++++++------- pod/renderer.py | 20 +++++++++++++++----- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/pod/actions.py b/pod/actions.py index 2243e20..66c1b46 100644 --- a/pod/actions.py +++ b/pod/actions.py @@ -168,6 +168,10 @@ class ForAction(BufferAction): # the loop # * curLoop.nb gives the index (starting at 0) if the currently # walked element. + # * curLoop.first is True if the currently walkded element is the + # first one. + # * curLoop.last is True if the currently walkded element is the + # last one. # For example, if you have a "for" statement like this: # for elem in myListOfElements # Within the part of the ODT document impacted by this statement, you @@ -217,6 +221,8 @@ class ForAction(BufferAction): for item in self.exprResult: i += 1 loop.nb = i + loop.first = i == 0 + loop.last = i == (loop.length-1) context[self.iter] = item # Cell: add a new row if we are at the end of a row if isCell and (currentColIndex == nbOfColumns): diff --git a/pod/doc_importers.py b/pod/doc_importers.py index f0ca65e..d7aeace 100644 --- a/pod/doc_importers.py +++ b/pod/doc_importers.py @@ -86,8 +86,7 @@ class DocImporter: f = file(self.importPath, 'wb') f.write(fileContent) f.close() - # ImageImporter adds image-specific attrs, through - # ImageImporter.setImageInfo. + # Some importers add specific attrs, through method init. def getUuid(self): '''Creates a unique id for images/documents to be imported into an @@ -127,12 +126,27 @@ class OdtImporter(DocImporter): '''This class allows to import the content of another ODT document into a pod template.''' def getImportFolder(self): return '%s/docImports' % self.tempFolder + + def init(self, pageBreakBefore, pageBreakAfter): + '''OdtImporter-specific constructor.''' + self.pageBreakBefore = pageBreakBefore + self.pageBreakAfter = pageBreakAfter + def run(self): + # Define a "pageBreak" if needed. + if self.pageBreakBefore or self.pageBreakAfter: + pageBreak = '<%s:p %s:style-name="podPageBreak">' % \ + (self.textNs, self.textNs, self.textNs) + # Insert a page break before importing the doc if needed + if self.pageBreakBefore: self.res += pageBreak + # Import the external odt document self.res += '<%s:section %s:name="PodImportSection%f">' \ '<%s:section-source %s:href="%s" ' \ '%s:filter-name="writer8"/>' % ( self.textNs, self.textNs, time.time(), self.textNs, self.linkNs, self.importPath, self.textNs, self.textNs) + # Insert a page break after importing the doc if needed + if self.pageBreakAfter: self.res += pageBreak return self.res class PodImporter(DocImporter): @@ -140,9 +154,11 @@ class PodImporter(DocImporter): into the current POD result.''' def getImportFolder(self): return '%s/docImports' % self.tempFolder - def setContext(self, context): - '''Defines the context to user for the PodImporter.''' + def init(self, context, pageBreakBefore, pageBreakAfter): + '''PodImporter-specific constructor.''' self.context = context + self.pageBreakBefore = pageBreakBefore + self.pageBreakAfter = pageBreakAfter def run(self): # Define where to store the pod result in the temp folder @@ -161,7 +177,9 @@ class PodImporter(DocImporter): renderer.run() # The POD result is in "resOdt". Import it into the main POD result # using an OdtImporter. - return OdtImporter(None, resOdt, 'odt', self.renderer).run() + odtImporter = OdtImporter(None, resOdt, 'odt', self.renderer) + odtImporter.init(self.pageBreakBefore, self.pageBreakAfter) + return odtImporter.run() class PdfImporter(DocImporter): '''This class allows to import the content of a PDF file into a pod @@ -193,7 +211,7 @@ class PdfImporter(DocImporter): if os.path.exists(nextImage): # Use internally an Image importer for doing this job. imgImporter= ImageImporter(None, nextImage, 'jpg',self.renderer) - imgImporter.setImageInfo('paragraph', True, None, None, None) + imgImporter.init('paragraph', True, None, None, None) self.res += imgImporter.run() os.remove(nextImage) else: @@ -325,7 +343,8 @@ class ImageImporter(DocImporter): appyFile.dump(importPath) return importPath - def setImageInfo(self, anchor, wrapInPara, size, sizeUnit, style): + def init(self, anchor, wrapInPara, size, sizeUnit, style): + '''ImageImporter-specific constructor.''' # Initialise anchor if anchor not in self.anchorTypes: raise PodError(self.WRONG_ANCHOR % str(self.anchorTypes)) diff --git a/pod/renderer.py b/pod/renderer.py index 508c6fe..aef3654 100644 --- a/pod/renderer.py +++ b/pod/renderer.py @@ -278,7 +278,8 @@ class Renderer: convertibleFormats = FILE_TYPES.keys() def importDocument(self, content=None, at=None, format=None, anchor='as-char', wrapInPara=True, size=None, - sizeUnit='cm', style=None): + sizeUnit='cm', style=None, + pageBreakBefore=False, pageBreakAfter=False): '''If p_at is not None, it represents a path or url allowing to find the document. If p_at is None, the content of the document is supposed to be in binary format in p_content. The document @@ -297,6 +298,10 @@ class Renderer: * If p_style is given, it is the content of a "style" attribute, containing CSS attributes. If "width" and "heigth" attributes are found there, they will override p_size and p_sizeUnit. + + p_pageBreakBefore and p_pageBreakAfter are only relevant for import + of external odt documents, and allows to insert a page break + before/after the inserted document. ''' importer = None # Is there someting to import? @@ -318,9 +323,11 @@ class Renderer: if mimeTypesExts.has_key(format): format = mimeTypesExts[format] isImage = False + isOdt = False if format in self.ooFormats: importer = OdtImporter self.forceOoCall = True + isOdt = True elif (format in self.imageFormats) or not format: # If the format can't be guessed, we suppose it is an image. importer = ImageImporter @@ -333,10 +340,12 @@ class Renderer: raise PodError(DOC_WRONG_FORMAT % format) imp = importer(content, at, format, self) # Initialise image-specific parameters - if isImage: imp.setImageInfo(anchor, wrapInPara, size, sizeUnit, style) + if isImage: imp.init(anchor, wrapInPara, size, sizeUnit, style) + elif isOdt: imp.init(pageBreakBefore, pageBreakAfter) return imp.run() - def importPod(self, content=None, at=None, format='odt', context=None): + def importPod(self, content=None, at=None, format='odt', context=None, + pageBreakBefore=False, pageBreakAfter=False): '''Similar to m_importDocument, but allows to import the result of executing the POD template specified in p_content or p_at, and include it in the POD result.''' @@ -352,9 +361,10 @@ class Renderer: # Define the context to use: either the current context of the current # POD renderer, or p_context if given. if context: - imp.setContext(context) + ctx = context else: - imp.setContext(self.contentParser.env.context) + ctx = self.contentParser.env.context + imp.init(ctx, pageBreakBefore, pageBreakAfter) return imp.run() def prepareFolders(self):