[pod] Allowed to call a pod template within another pod template via new POD function 'pod'.

This commit is contained in:
Gaetan Delannay 2013-04-22 10:57:51 +02:00
parent eaf7156b47
commit 4e5e5143cb
3 changed files with 53 additions and 4 deletions

View file

@ -133,6 +133,34 @@ class OdtImporter(DocImporter):
self.linkNs, self.importPath, self.textNs, self.textNs)
return self.res
class PodImporter(DocImporter):
'''This class allows to import the result of applying another POD template,
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.'''
self.context = context
def run(self):
# Define where to store the pod result in the temp folder
r = self.renderer
# Define where to store the ODT result.
op = os.path
resFolder = op.dirname(self.importPath)
resName = '%s.res.odt' % op.splitext(op.basename(self.importPath))[0]
resOdt = op.join(resFolder, resName)
# The POD template is in self.importPath
renderer = r.__class__(self.importPath, self.context, resOdt,
pythonWithUnoPath=r.pyPath,
ooPort=r.ooPort, forceOoCall=r.forceOoCall,
imageResolver=r.imageResolver)
renderer.stylesManager.stylesMapping = r.stylesManager.stylesMapping
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()
class PdfImporter(DocImporter):
'''This class allows to import the content of a PDF file into a pod
template. It calls gs to split the PDF into images and calls the

View file

@ -31,7 +31,7 @@ from appy.pod.converter import FILE_TYPES
from appy.pod.buffers import FileBuffer
from appy.pod.xhtml2odt import Xhtml2OdtConverter
from appy.pod.doc_importers import \
OdtImporter, ImageImporter, PdfImporter, ConvertImporter
OdtImporter, ImageImporter, PdfImporter, ConvertImporter, PodImporter
from appy.pod.styles_manager import StylesManager
# ------------------------------------------------------------------------------
@ -232,7 +232,8 @@ class Renderer:
evalContext = {'xhtml': self.renderXhtml,
'text': self.renderText,
'test': self.evalIfExpression,
'document': self.importDocument} # Default context
'document': self.importDocument,
'pod': self.importPod } # Default context
if hasattr(context, '__dict__'):
evalContext.update(context.__dict__)
elif isinstance(context, dict) or isinstance(context, UserDict):
@ -333,8 +334,28 @@ class Renderer:
imp = importer(content, at, format, self)
# Initialise image-specific parameters
if isImage: imp.setImageInfo(anchor, wrapInPara, size, sizeUnit, style)
res = imp.run()
return res
return imp.run()
def importPod(self, content=None, at=None, format='odt', context=None):
'''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.'''
# Is there a pod template defined?
if not content and not at:
raise PodError(DOC_NOT_SPECIFIED)
# If the POD template is specified as a Zope file, convert it into a
# Appy FileWrapper.
if content.__class__.__name__ == 'File':
content = FileWrapper(content)
imp = PodImporter(content, at, format, self)
self.forceOoCall = True
# Define the context to use: either the current context of the current
# POD renderer, or p_context if given.
if context:
imp.setContext(context)
else:
imp.setContext(self.contentParser.env.context)
return imp.run()
def prepareFolders(self):
# Check if I can write the result