[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.

This commit is contained in:
Gaetan Delannay 2013-05-27 22:32:18 +02:00
parent 540a9947d8
commit e8c63f225f
3 changed files with 47 additions and 12 deletions

View file

@ -168,6 +168,10 @@ class ForAction(BufferAction):
# the loop # the loop
# * curLoop.nb gives the index (starting at 0) if the currently # * curLoop.nb gives the index (starting at 0) if the currently
# walked element. # 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 example, if you have a "for" statement like this:
# for elem in myListOfElements # for elem in myListOfElements
# Within the part of the ODT document impacted by this statement, you # Within the part of the ODT document impacted by this statement, you
@ -217,6 +221,8 @@ class ForAction(BufferAction):
for item in self.exprResult: for item in self.exprResult:
i += 1 i += 1
loop.nb = i loop.nb = i
loop.first = i == 0
loop.last = i == (loop.length-1)
context[self.iter] = item context[self.iter] = item
# Cell: add a new row if we are at the end of a row # Cell: add a new row if we are at the end of a row
if isCell and (currentColIndex == nbOfColumns): if isCell and (currentColIndex == nbOfColumns):

View file

@ -86,8 +86,7 @@ class DocImporter:
f = file(self.importPath, 'wb') f = file(self.importPath, 'wb')
f.write(fileContent) f.write(fileContent)
f.close() f.close()
# ImageImporter adds image-specific attrs, through # Some importers add specific attrs, through method init.
# ImageImporter.setImageInfo.
def getUuid(self): def getUuid(self):
'''Creates a unique id for images/documents to be imported into an '''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 '''This class allows to import the content of another ODT document into a
pod template.''' pod template.'''
def getImportFolder(self): return '%s/docImports' % self.tempFolder 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): def run(self):
# Define a "pageBreak" if needed.
if self.pageBreakBefore or self.pageBreakAfter:
pageBreak = '<%s:p %s:style-name="podPageBreak"></%s:p>' % \
(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">' \ self.res += '<%s:section %s:name="PodImportSection%f">' \
'<%s:section-source %s:href="%s" ' \ '<%s:section-source %s:href="%s" ' \
'%s:filter-name="writer8"/></%s:section>' % ( '%s:filter-name="writer8"/></%s:section>' % (
self.textNs, self.textNs, time.time(), self.textNs, self.textNs, self.textNs, time.time(), self.textNs,
self.linkNs, self.importPath, self.textNs, 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 return self.res
class PodImporter(DocImporter): class PodImporter(DocImporter):
@ -140,9 +154,11 @@ class PodImporter(DocImporter):
into the current POD result.''' into the current POD result.'''
def getImportFolder(self): return '%s/docImports' % self.tempFolder def getImportFolder(self): return '%s/docImports' % self.tempFolder
def setContext(self, context): def init(self, context, pageBreakBefore, pageBreakAfter):
'''Defines the context to user for the PodImporter.''' '''PodImporter-specific constructor.'''
self.context = context self.context = context
self.pageBreakBefore = pageBreakBefore
self.pageBreakAfter = pageBreakAfter
def run(self): def run(self):
# Define where to store the pod result in the temp folder # Define where to store the pod result in the temp folder
@ -161,7 +177,9 @@ class PodImporter(DocImporter):
renderer.run() renderer.run()
# The POD result is in "resOdt". Import it into the main POD result # The POD result is in "resOdt". Import it into the main POD result
# using an OdtImporter. # 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): class PdfImporter(DocImporter):
'''This class allows to import the content of a PDF file into a pod '''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): if os.path.exists(nextImage):
# Use internally an Image importer for doing this job. # Use internally an Image importer for doing this job.
imgImporter= ImageImporter(None, nextImage, 'jpg',self.renderer) 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() self.res += imgImporter.run()
os.remove(nextImage) os.remove(nextImage)
else: else:
@ -325,7 +343,8 @@ class ImageImporter(DocImporter):
appyFile.dump(importPath) appyFile.dump(importPath)
return importPath return importPath
def setImageInfo(self, anchor, wrapInPara, size, sizeUnit, style): def init(self, anchor, wrapInPara, size, sizeUnit, style):
'''ImageImporter-specific constructor.'''
# Initialise anchor # Initialise anchor
if anchor not in self.anchorTypes: if anchor not in self.anchorTypes:
raise PodError(self.WRONG_ANCHOR % str(self.anchorTypes)) raise PodError(self.WRONG_ANCHOR % str(self.anchorTypes))

View file

@ -278,7 +278,8 @@ class Renderer:
convertibleFormats = FILE_TYPES.keys() convertibleFormats = FILE_TYPES.keys()
def importDocument(self, content=None, at=None, format=None, def importDocument(self, content=None, at=None, format=None,
anchor='as-char', wrapInPara=True, size=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 '''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 the document. If p_at is None, the content of the document is
supposed to be in binary format in p_content. The document 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, * If p_style is given, it is the content of a "style" attribute,
containing CSS attributes. If "width" and "heigth" attributes are containing CSS attributes. If "width" and "heigth" attributes are
found there, they will override p_size and p_sizeUnit. 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 importer = None
# Is there someting to import? # Is there someting to import?
@ -318,9 +323,11 @@ class Renderer:
if mimeTypesExts.has_key(format): if mimeTypesExts.has_key(format):
format = mimeTypesExts[format] format = mimeTypesExts[format]
isImage = False isImage = False
isOdt = False
if format in self.ooFormats: if format in self.ooFormats:
importer = OdtImporter importer = OdtImporter
self.forceOoCall = True self.forceOoCall = True
isOdt = True
elif (format in self.imageFormats) or not format: elif (format in self.imageFormats) or not format:
# If the format can't be guessed, we suppose it is an image. # If the format can't be guessed, we suppose it is an image.
importer = ImageImporter importer = ImageImporter
@ -333,10 +340,12 @@ class Renderer:
raise PodError(DOC_WRONG_FORMAT % format) raise PodError(DOC_WRONG_FORMAT % format)
imp = importer(content, at, format, self) imp = importer(content, at, format, self)
# Initialise image-specific parameters # 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() 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 '''Similar to m_importDocument, but allows to import the result of
executing the POD template specified in p_content or p_at, and executing the POD template specified in p_content or p_at, and
include it in the POD result.''' include it in the POD result.'''
@ -352,9 +361,10 @@ class Renderer:
# Define the context to use: either the current context of the current # Define the context to use: either the current context of the current
# POD renderer, or p_context if given. # POD renderer, or p_context if given.
if context: if context:
imp.setContext(context) ctx = context
else: else:
imp.setContext(self.contentParser.env.context) ctx = self.contentParser.env.context
imp.init(ctx, pageBreakBefore, pageBreakAfter)
return imp.run() return imp.run()
def prepareFolders(self): def prepareFolders(self):