Merge with 7b7ba5e472685e124441dcd8acef17ea9b8d80f2 preserve_html
authorraph
Tue, 20 Apr 2010 10:47:42 +0200
branchpreserve_html
changeset 258 a79a3c91d9b5
parent 254 94afd8e00c74 (diff)
parent 257 7b7ba5e47268 (current diff)
child 259 0371caf8bcc6
Merge with 7b7ba5e472685e124441dcd8acef17ea9b8d80f2
--- a/src/cm/converters/__init__.py	Tue Apr 20 10:47:04 2010 +0200
+++ b/src/cm/converters/__init__.py	Tue Apr 20 10:47:42 2010 +0200
@@ -2,6 +2,9 @@
 import chardet 
 from cm.utils.string_utils import to_unicode 
 import re
+import os
+from cm.converters.oo_converters import extract_css_body
+
 
 # TODO: move that in text_base: save images
 def convert_from_mimetype(file_name, mime_type, format):
@@ -19,8 +22,12 @@
                      'application/msword',
                      ]:
         
-        xhtml_input, attachs = convert_oo_to_html(input)
-        converted_input = pandoc_convert(xhtml_input, 'html', format)
+        html_input, xhtml_input, attachs = convert_oo_to_html_and_xhtml(input)
+        if format == 'html':
+                _not_used_css, converted_input = extract_css_body(xhtml_input)
+                #converted_input = xhtml_input
+        else:
+            converted_input = pandoc_convert(html_input, 'html', format)
         
     ##############################
     # latex
@@ -71,7 +78,7 @@
             match_html = res_html.next()
             if match_html:
                 img_name = match_html.group(1)
-                img_path = imgs[img_name]
+                img_path = os.path.split(img_name)[-1]
         except StopIteration:
             # TODO : report pb
             pass 
@@ -88,7 +95,6 @@
     
     enc = chardet.detect(html_input)['encoding']
     try_encodings = [enc, 'utf8', 'latin1']
-    res_content = None
     for encoding in try_encodings:
         try:
             res_content_html = unicode(html_input, encoding)
@@ -99,29 +105,33 @@
         raise Exception('UnicodeDecodeError: could not decode')
     return res_content_html, images
 
-def old_convert_oo_to_html(input): 
+def fix_html_img_path(html):
+    return html.replace('IMG SRC="../outdir/','IMG SRC="')
+    
+def convert_oo_to_html_and_xhtml(input): 
     from oo_converters import convert   
     html_input, images = convert(input, 'html')
     xhtml_input, _not_used_ = convert(input, 'xhtml')
-    
     enc = chardet.detect(xhtml_input)['encoding']
     try_encodings = [enc, 'utf8', 'latin1']
-    res_content = None
     for encoding in try_encodings:
         try:
-            # TODO: fix path and manage images
-            #res_content = fix_img_path(unicode(html_res_content,encoding),
-            #                           unicode(xhtml_res_content,encoding),
-            #                           iimg)
             res_content_html = unicode(html_input, encoding)
             res_content_xhtml = unicode(xhtml_input, encoding)
             break;
         except UnicodeDecodeError:
             pass
+
+    res_content_xhtml = fix_img_path(res_content_html, res_content_xhtml, images)
+    res_content_html = fix_html_img_path(res_content_html)
+    
     if not res_content_html or not res_content_xhtml:
         raise Exception('UnicodeDecodeError: could not decode')
-    return res_content_html, res_content_xhtml, images
+    return res_content_html, cleanup(res_content_xhtml), images
         
+def cleanup(string):
+    return string.replace(u'\xc2\xa0',u'')
+
 def markdown_from_code(code):
     CODE_INDICATOR = "    " # 4 spaces
     return '\n'.join([CODE_INDICATOR + line for line in code.split('\n')])
--- a/src/cm/converters/oo_converters.py	Tue Apr 20 10:47:04 2010 +0200
+++ b/src/cm/converters/oo_converters.py	Tue Apr 20 10:47:42 2010 +0200
@@ -223,35 +223,6 @@
 
 THE_INDIR = "indir"
 THE_INFILE = "infile"
-
-def fix_img_path(html,xhtml,imgs):
-    """
-    imgs : name --> path
-    """
-    finder_re = 'src[\s]*=[\s]*\"((?:(?!https?))[^\"]*)\"'
-    len_res_html = len(re.findall(finder_re,html,re.IGNORECASE))
-    len_res_xhtml = len(re.findall(finder_re,xhtml,re.IGNORECASE))
-    res_html = re.finditer(finder_re,html,re.IGNORECASE)
-    res_xhtml = re.finditer(finder_re,xhtml,re.IGNORECASE)
-    result = []
-    last_index = 0
-    for match_xhtml in res_xhtml:
-        img_path = '' 
-        try:
-            match_html = res_html.next()
-            if match_html:
-                img_name = match_html.group(1)
-                img_path = imgs[img_name]
-        except StopIteration:
-            # TODO : report pb
-            pass 
-        offset = len(match_xhtml.group(0)) - len(match_xhtml.group(1))
-        result.append(xhtml[last_index:match_xhtml.start() + offset - 1])
-        result.append(img_path)
-        last_index = match_xhtml.end() - 1 # -1 because trailing "
-    result.append(xhtml[last_index:len(xhtml)])
-    return u''.join(result)
-
   
 def extract_css_body(xhtml):
     dom = parseString(xhtml.encode('utf8'))
--- a/src/cm/converters/pandoc_converters.py	Tue Apr 20 10:47:04 2010 +0200
+++ b/src/cm/converters/pandoc_converters.py	Tue Apr 20 10:47:42 2010 +0200
@@ -13,7 +13,7 @@
 from cm.utils.string_utils import to_unicode
 
 PANDOC_BIN = "pandoc"
-PANDOC_OPTIONS = "--sanitize-html "
+PANDOC_OPTIONS = " -R "
 
 MARKDOWN2PDF_BIN = "markdown2pdf"
 
--- a/src/cm/models.py	Tue Apr 20 10:47:04 2010 +0200
+++ b/src/cm/models.py	Tue Apr 20 10:47:42 2010 +0200
@@ -189,7 +189,10 @@
     objects = TextVersionManager()
     
     def get_content(self, format='html'):
-        return pandoc_convert(self.content, self.format, format)
+        if format == self.format:
+            return self.content
+        else:
+            return pandoc_convert(self.content, self.format, format)
 #    def _get_comments(self, user = None, filter_reply = 0):        
 #        """
 #        get comments viewable by this user (user = None or user = AnonymousUser => everyone)