Commit f1e5737f authored by Jean-Paul Smets's avatar Jean-Paul Smets

This patch make thes inclusion of subcontent much more generic. It may break existing templates.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@17339 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent ec2edfa3
...@@ -115,6 +115,7 @@ class OOoTemplate(ZopePageTemplate): ...@@ -115,6 +115,7 @@ class OOoTemplate(ZopePageTemplate):
_OLE_directory_prefix = 'Obj' _OLE_directory_prefix = 'Obj'
# every OOo document have a content-type starting like this # every OOo document have a content-type starting like this
_OOo_content_type_root = 'application/vnd.sun.xml.' _OOo_content_type_root = 'application/vnd.sun.xml.'
_ODF_content_type_root = 'application/vnd.oasis.opendocument.'
# Declarative Security # Declarative Security
security = ClassSecurityInfo() security = ClassSecurityInfo()
...@@ -224,53 +225,53 @@ class OOoTemplate(ZopePageTemplate): ...@@ -224,53 +225,53 @@ class OOoTemplate(ZopePageTemplate):
return ret return ret
def replaceIncludes(match): def replaceIncludes(match):
options_dict = dict( style="fr1", x="0cm", y="0cm" ) tag_string = match.group(1)
options_dict.update( dict(arguments_re.findall( match.group(1) )) )
document = self._resolvePath( options_dict['path'].encode() ) # Build a dictionary with tag parameters
#document_text = document.read() options_dict = dict(arguments_re.findall(match.group(1)))
# Find the page template based on the path and remove path from dict
document = self._resolvePath(options_dict['path'].encode())
document_text = ZopePageTemplate.pt_render(document) # extra_context is missing document_text = ZopePageTemplate.pt_render(document) # extra_context is missing
del options_dict['path']
if 'type' not in options_dict: # Find the type of the embedded document
options_dict['type'] = document.content_type document_type = document.content_type
else: # type passed in short form as an attribute
options_dict['type'] = self._OOo_content_type_root + options_dict['type']
w, h, x, y = getLengthInfos( options_dict , ('width', 'height', 'x', 'y') )
# Set defaults
if w is None:
w = 10.0
if h is None:
h = 10.0
if x is None:
x = 0.0
if y is None:
y = 0.0
# Prepare a subdirectory to store embedded objects
actual_idx = self.document_counter.next() actual_idx = self.document_counter.next()
dir_name = '%s%d'%(self._OLE_directory_prefix,actual_idx) dir_name = '%s%d'%(self._OLE_directory_prefix,actual_idx)
if sub_document: # sub-document means sub-directory if sub_document: # sub-document means sub-directory
dir_name = sub_document + '/' + dir_name dir_name = sub_document + '/' + dir_name
try: # Get the stylesheet of the embedded openoffice document
ooo_stylesheet = getattr(here, document.ooo_stylesheet) ooo_stylesheet = document.ooo_stylesheet
# If style is dynamic, call it if ooo_stylesheet:
ooo_stylesheet = getattr(here, ooo_stylesheet)
# If ooo_stylesheet is dynamic, call it
try: try:
ooo_stylesheet = ooo_stylesheet() ooo_stylesheet = ooo_stylesheet()
except AttributeError: except AttributeError:
pass pass
temp_builder = OOoBuilder(ooo_stylesheet) temp_builder = OOoBuilder(ooo_stylesheet)
stylesheet = temp_builder.extract('styles.xml') stylesheet = temp_builder.extract('styles.xml')
except AttributeError: else:
stylesheet = None stylesheet = None
# Start recursion if necessary
sub_attached_files_dict = {} sub_attached_files_dict = {}
if 'office:include' in document_text: # small optimisation to avoid recursion if possible if 'office:include' in document_text: # small optimisation to avoid recursion if possible
(document_text, sub_attached_files_dict ) = self.renderIncludes(document_text, dir_name) (document_text, sub_attached_files_dict ) = self.renderIncludes(document_text, dir_name)
# View* = writer # Build settings document if necessary
# Visible* = calc settings_text = None
settings_text = """<?xml version="1.0" encoding="UTF-8"?> if 0:
w = 10
h = 10
# View* = writer
# Visible* = calc
settings_text = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE office:document-settings PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "office.dtd"> <!DOCTYPE office:document-settings PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "office.dtd">
<office:document-settings xmlns:office="http://openoffice.org/2000/office" <office:document-settings xmlns:office="http://openoffice.org/2000/office"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xlink="http://www.w3.org/1999/xlink"
...@@ -287,36 +288,36 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0"> ...@@ -287,36 +288,36 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
<config:config-item config:name="VisibleAreaHeight" config:type="int">%(h)d</config:config-item> <config:config-item config:name="VisibleAreaHeight" config:type="int">%(h)d</config:config-item>
</config:config-item-set> </config:config-item-set>
</office:settings> </office:settings>
</office:document-settings>"""%dict( w=int(w*1000) , h=int(h*1000) ) # convert from 10^-2 (centimeters) to 10^-5 </office:document-settings>""" % dict( w=int(w*1000) , h=int(h*1000) ) # convert from 10^-2 (centimeters) to 10^-5
attached_files_dict[dir_name] = dict(document = document_text,
doc_type = options_dict['type'], stylesheet = stylesheet ) # Attach content, style and settings if any
attached_files_dict[dir_name+'/settings.xml'] = dict( document = settings_text, attached_files_dict[dir_name] = dict(document=document_text,
doc_type = 'text/xml' ) doc_type=document_type,
attached_files_dict.update(sub_attached_files_dict ) stylesheet=stylesheet)
if settings_text:
# add a paragraph with the OLE document in it attached_files_dict[dir_name + '/settings.xml'] = dict(document=settings_text,
# The dir_name is relative here, extract the last path component doc_type='text/xml')
replacement = """ attached_files_dict.update(sub_attached_files_dict)
<draw:object draw:style-name="%s" draw:name="ERP5IncludedObject%d"
text:anchor-type="paragraph" svg:x="%.3fcm" svg:y="%.3fcm" # Build the new tag
svg:width="%.3fcm" svg:height="%.3fcm" xlink:href="#./%s parameter_list = []
" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/> for k, v in options_dict.items():
"""%(options_dict['style'], actual_idx, x, y, w, h, dir_name.split('/')[-1]) parameter_list.append('%s="%s"' % (k, v))
if not self.content_type.endswith('draw'): new_tag = '<draw:object draw:name="ERP5IncludedObject%d" xlink:href="./%s" %s/>' %\
replacement = '<text:p text:style-name="Standard">'+replacement+'</text:p>' (actual_idx, dir_name.split('/')[-1], ' '.join(parameter_list))
return replacement return new_tag
def replaceIncludesImg(match): def replaceIncludesImg(match):
options_dict = dict( x='0cm', y='0cm', style="fr1" ) options_dict = dict(x='0cm', y='0cm', style="fr1")
options_dict.update( dict(arguments_re.findall( match.group(1) )) ) options_dict.update(dict(arguments_re.findall(match.group(1))))
picture = self._resolvePath( options_dict['path'].encode() ) picture = self._resolvePath(options_dict['path'].encode())
# "standard" filetype == Image or File , for ERP objects the # "standard" filetype == Image or File , for ERP objects the
# manipulations are different # manipulations are different
is_standard_filetype = True is_standard_filetype = True
if getattr(picture, 'data', None) is None \ if getattr(picture, 'data', None) is None \
or callable(picture.content_type): or callable(picture.content_type):
is_standard_filetype = False is_standard_filetype = False
if is_standard_filetype: if is_standard_filetype:
...@@ -334,7 +335,7 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0"> ...@@ -334,7 +335,7 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
if '/' not in options_dict['type']: if '/' not in options_dict['type']:
options_dict['type'] = 'image/' + options_dict['type'] options_dict['type'] = 'image/' + options_dict['type']
w, h, maxwidth, maxheight = getLengthInfos( options_dict, ('width','height','maxwidth','maxheight') ) w, h, maxwidth, maxheight = getLengthInfos(options_dict, ('width', 'height', 'maxwidth', 'maxheight'))
try: # try image properties try: # try image properties
aspect_ratio = float(picture.width) / float(picture.height) aspect_ratio = float(picture.width) / float(picture.height)
...@@ -359,22 +360,22 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0"> ...@@ -359,22 +360,22 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
w = h * aspect_ratio w = h * aspect_ratio
actual_idx = self.document_counter.next() actual_idx = self.document_counter.next()
pic_name = 'Pictures/picture%d.%s'%(actual_idx, options_dict['type'].split('/')[-1]) pic_name = 'Pictures/picture%d.%s' % (actual_idx, options_dict['type'].split('/')[-1])
if sub_document: # sub-document means sub-directory if sub_document: # sub-document means sub-directory
pic_name = sub_document+'/'+pic_name pic_name = sub_document + '/' + pic_name
attached_files_dict[pic_name] = dict( attached_files_dict[pic_name] = dict(
document = picture_data, document=picture_data,
doc_type = options_dict['type'] doc_type=options_dict['type']
) )
# XXX: Pictures directory not managed (seems facultative) # XXX: Pictures directory not managed (seems facultative)
# <manifest:file-entry manifest:media-type="" manifest:full-path="ObjBFE4F50D/Pictures/"/> # <manifest:file-entry manifest:media-type="" manifest:full-path="ObjBFE4F50D/Pictures/"/>
is_legacy = ('oasis.opendocument' not in self.content_type) is_legacy = ('oasis.opendocument' not in self.content_type)
replacement = """<draw:image draw:style-name="%s" draw:name="ERP5Image%d" replacement = """<draw:image draw:style-name="%s" draw:name="ERP5Image%d"
text:anchor-type="paragraph" svg:x="%s" svg:y="%s" text:anchor-type="paragraph" svg:x="%s" svg:y="%s"
svg:width="%.3fcm" svg:height="%.3fcm" xlink:href="%sPictures/%s" svg:width="%.3fcm" svg:height="%.3fcm" xlink:href="%sPictures/%s"
xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/> xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/>
""" % (options_dict['style'], actual_idx, """ % (options_dict['style'], actual_idx,
options_dict['x'], options_dict['y'], options_dict['x'], options_dict['y'],
w, h, w, h,
...@@ -432,20 +433,22 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0"> ...@@ -432,20 +433,22 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
# Add the associated files # Add the associated files
for dir_name, document_dict in attachments_dict.iteritems(): for dir_name, document_dict in attachments_dict.iteritems():
# Special case : the document is an OOo one # Special case : the document is an OOo one
if document_dict['doc_type'].startswith(self._OOo_content_type_root): if document_dict['doc_type'].startswith(self._OOo_content_type_root) or \
ooo_builder.addFileEntry(full_path = dir_name, document_dict['doc_type'].startswith(self._ODF_content_type_root):
media_type = document_dict['doc_type'] ) ooo_builder.addFileEntry(full_path=dir_name,
ooo_builder.addFileEntry(full_path = dir_name+'/content.xml', media_type=document_dict['doc_type'])
media_type = 'text/xml',content = document_dict['document'] ) ooo_builder.addFileEntry(full_path=dir_name + '/content.xml',
media_type='text/xml', content=document_dict['document'])
styles_text = default_styles_text styles_text = default_styles_text
if document_dict.has_key('stylesheet') and document_dict['stylesheet']: if document_dict.has_key('stylesheet') and document_dict['stylesheet']:
styles_text = document_dict['stylesheet'] styles_text = document_dict['stylesheet']
if styles_text: if styles_text:
ooo_builder.addFileEntry(full_path = dir_name+'/styles.xml', ooo_builder.addFileEntry(full_path=dir_name + '/styles.xml',
media_type = 'text/xml',content = styles_text ) media_type='text/xml', content=styles_text)
else: # Generic case else: # Generic case
ooo_builder.addFileEntry(full_path=dir_name, ooo_builder.addFileEntry(full_path=dir_name,
media_type=document_dict['doc_type'], content = document_dict['document'] ) media_type=document_dict['doc_type'],
content=document_dict['document'])
# Debug mode # Debug mode
if request.get('debug',0): if request.get('debug',0):
......
...@@ -14,16 +14,21 @@ OOo Template ...@@ -14,16 +14,21 @@ OOo Template
- <office:include> - <office:include>
Allow you to include another document in the current template (as an OLE attachment) Allow you to include another document in the current template (as an OLE attachment)
You must specify at least the path (can be either a single name or a path name using "/") You must specify at least the path (can be either a single name or a path name using "/").
and the type of the document (generally calc or writer), the default is zope's content-type. The type of document must be specified in the embedded document itself as
The size is always specified in centimeters (with attached "cm" suffix or not). a MIME type. Size parameters are defined, as in any ODF file,
You can specify the style (defined in the sylesheet) with the "style" option. within the <draw:frame> tag which encloses the <office:include> tag.
You can also pass "x" and "y" attributes for positioning, it's mainly useful for draw documents.
TODO: make sure it is useful or useless to pass x, y params (as before)
Example: Example:
<office:include type="calc" width="10.100cm" height="16.000cm" path="agenda" /> <draw:frame draw:style-name='gr1' svg:height='18.686cm' svg:width='27.367cm' draw:layer='layout'
<office:include width="15" height="20" path="/reports/my_report" /> svg:x='0cm' svg:y='0cm'
<office:include path="foo" /> tal:attributes="svg:height height | string:18.686cm;
svg:width width | string:27.367cm">
<office:include path="ERP5Site_viewOwnerBarChart" xlink:type='simple'
xlink:actuate='onLoad' xlink:show='embed'/>
</draw:frame>
- <office:include_img> - <office:include_img>
Not unlike <office:include>, allows you to include a picture document, refer to Not unlike <office:include>, allows you to include a picture document, refer to
...@@ -36,7 +41,7 @@ OOo Template ...@@ -36,7 +41,7 @@ OOo Template
or if a constraint is applied). or if a constraint is applied).
Example: Example:
<office:include x="5cm" y="1cm" path="foo" /> <office:include_img x="5cm" y="1cm" path="foo" />
Tips: Tips:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment