From 500c6f122776edf1226ad01a2cd75f175d1ff086 Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Tue, 16 Sep 2008 17:21:45 +0000
Subject: [PATCH] * Accept single quotes for attributes in <office:include> and
 <office:include_img>. * Use mimetypes.guess_extension to get the extension
 from the content_type. * Recognize BMP files in Image documents.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@23644 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/Image.py |  9 ++++++++-
 product/ERP5OOo/OOoTemplate.py | 14 ++++++--------
 2 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/product/ERP5/Document/Image.py b/product/ERP5/Document/Image.py
index 761958a4e8..f261353861 100644
--- a/product/ERP5/Document/Image.py
+++ b/product/ERP5/Document/Image.py
@@ -31,6 +31,7 @@
 
 import os
 import string
+import struct
 import sys
 import time
 import subprocess
@@ -118,10 +119,16 @@ class Image(File, OFSImage):
       TODO:
       - use image magick or PIL
     """
+    self.size = len(self.data)
     content_type, width, height = getImageInfo(self.data)
+    if not content_type:
+      if self.size >= 30 and self.data[:2] == 'BM':
+        header = struct.unpack('<III', self.data[14:26])
+        if header[0] >= 12:
+          content_type = 'image/x-bmp'
+          width, height = header[1:]
     self.height = height
     self.width = width
-    self.size = len(self.data)
     self._setContentType(content_type)
 
   
diff --git a/product/ERP5OOo/OOoTemplate.py b/product/ERP5OOo/OOoTemplate.py
index 1055d86bbd..c2cb306300 100644
--- a/product/ERP5OOo/OOoTemplate.py
+++ b/product/ERP5OOo/OOoTemplate.py
@@ -27,6 +27,7 @@
 ##############################################################################
 
 from types import StringType
+from mimetypes import guess_extension
 from zLOG import LOG
 from zLOG import PROBLEM
 from OFS.Image import File
@@ -242,7 +243,7 @@ class OOoTemplate(ZopePageTemplate):
 
   def renderIncludes(self, here, text, sub_document=None):
     attached_files_dict = {}
-    arguments_re = re.compile('(\S+?)\s*=\s*"(.*?)"\s*',re.DOTALL)
+    arguments_re = re.compile('''(\S+?)\s*=\s*('|")(.*?)\\2\s*''',re.DOTALL)
     def getLengthInfos( opts_dict, opts_names ):
       ret = []
       for opt_name in opts_names:
@@ -257,9 +258,8 @@ class OOoTemplate(ZopePageTemplate):
       return ret
 
     def replaceIncludes(match):
-      tag_string = match.group(1)
       # Build a dictionary with tag parameters
-      options_dict = dict(arguments_re.findall(tag_string))
+      options_dict = dict((x[0], x[2]) for x in 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
@@ -339,7 +339,7 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
 
     def replaceIncludesImg(match):
       options_dict = { 'text:anchor-type': 'paragraph' }
-      options_dict.update(arguments_re.findall(match.group(1)))
+      options_dict.update((x[0], x[2]) for x in arguments_re.findall(match.group(1)))
       for old_name, name, default in (('x', 'svg:x', '0cm'),
                                       ('y', 'svg:y', '0cm'),
                                       ('style', 'draw:style-name', 'fr1')):
@@ -373,9 +373,6 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
         if not is_standard_filetype:
           picture_type = picture_type()
 
-      if '/' not in picture_type:
-        picture_type = 'image/' + picture_type
-
       w, h, maxwidth, maxheight = getLengthInfos(options_dict,
                                   ('width', 'height', 'maxwidth', 'maxheight'))
 
@@ -407,7 +404,8 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
         w = h * aspect_ratio
 
       actual_idx = self.document_counter.next()
-      pic_name = 'Pictures/picture%d.%s' % (actual_idx, picture_type.split('/')[-1])
+      pic_name = 'Pictures/picture%d%s' \
+                 % (actual_idx, guess_extension(picture_type) or '')
 
       # XXX: Pictures directory not managed (seems facultative)
       #  <manifest:file-entry manifest:media-type="" manifest:full-path="ObjBFE4F50D/Pictures/"/>
-- 
2.30.9