| author | raph |
| Thu, 15 Apr 2010 17:21:59 +0200 | |
| branch | preserve_html |
| changeset 253 | a844469257b0 |
| parent 252 | 0f0a79f7f213 |
| child 259 | 0371caf8bcc6 |
| permissions | -rw-r--r-- |
| 0 | 1 |
from pandoc_converters import pandoc_convert |
2 |
import chardet |
|
| 149 | 3 |
from cm.utils.string_utils import to_unicode |
|
77
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
50
diff
changeset
|
4 |
import re |
| 253 | 5 |
import os |
| 252 | 6 |
from cm.converters.oo_converters import extract_css_body |
7 |
||
| 0 | 8 |
|
9 |
# TODO: move that in text_base: save images |
|
10 |
def convert_from_mimetype(file_name, mime_type, format): |
|
11 |
input = open(file_name, 'r').read() |
|
12 |
return _convert_from_mimetype(input, mime_type, format) |
|
13 |
||
14 |
||
15 |
def _convert_from_mimetype(input, mime_type, format): |
|
16 |
#input = to_unicode(input) |
|
17 |
||
18 |
attachs = [] |
|
19 |
attachs_dir = None |
|
20 |
############################## |
|
21 |
if mime_type in ['application/vnd.oasis.opendocument.text', |
|
22 |
'application/msword', |
|
23 |
]: |
|
24 |
||
| 252 | 25 |
html_input, xhtml_input, attachs = convert_oo_to_html_and_xhtml(input) |
26 |
if format == 'html': |
|
27 |
_not_used_css, converted_input = extract_css_body(xhtml_input) |
|
28 |
#converted_input = xhtml_input |
|
29 |
else: |
|
30 |
converted_input = pandoc_convert(html_input, 'html', format) |
|
| 0 | 31 |
|
32 |
############################## |
|
| 118 | 33 |
# latex |
34 |
elif mime_type in ['application/x-latex','text/x-tex',]: |
|
35 |
converted_input = pandoc_convert(to_unicode(input), 'latex', format) |
|
36 |
||
37 |
############################## |
|
| 0 | 38 |
# anything looks like code: put them into markdown citation |
| 118 | 39 |
elif mime_type.startswith('text/x-') or mime_type in ['application/x-ruby',]: |
| 0 | 40 |
converted_input = markdown_from_code(input) |
41 |
||
42 |
############################## |
|
43 |
# html |
|
44 |
elif mime_type in ['text/html', 'application/xhtml+xml']: |
|
45 |
if format == 'html': |
|
46 |
converted_input = input |
|
47 |
else: |
|
48 |
converted_input = pandoc_convert(input, 'html', format) |
|
| 78 | 49 |
############################## |
50 |
# anything looks like text -> markdown |
|
51 |
elif mime_type in ['text/plain', |
|
52 |
'text/english', |
|
53 |
'text/enriched' |
|
54 |
]: |
|
55 |
converted_input = to_unicode(input) |
|
56 |
############################## |
|
57 |
# default case: assume it's text |
|
58 |
else: |
|
59 |
converted_input = to_unicode(input) |
|
60 |
||
| 0 | 61 |
|
62 |
return converted_input, attachs |
|
63 |
||
64 |
def fix_img_path(html, xhtml, imgs): |
|
65 |
""" |
|
66 |
imgs : name --> path |
|
67 |
""" |
|
68 |
finder_re = 'src[\s]*=[\s]*\"((?:(?!https?))[^\"]*)\"' |
|
69 |
len_res_html = len(re.findall(finder_re, html, re.IGNORECASE)) |
|
70 |
len_res_xhtml = len(re.findall(finder_re, xhtml, re.IGNORECASE)) |
|
71 |
res_html = re.finditer(finder_re, html, re.IGNORECASE) |
|
72 |
res_xhtml = re.finditer(finder_re, xhtml, re.IGNORECASE) |
|
73 |
result = [] |
|
74 |
last_index = 0 |
|
75 |
for match_xhtml in res_xhtml: |
|
76 |
img_path = '' |
|
77 |
try: |
|
78 |
match_html = res_html.next() |
|
79 |
if match_html: |
|
80 |
img_name = match_html.group(1) |
|
| 253 | 81 |
img_path = os.path.split(img_name)[-1] |
| 0 | 82 |
except StopIteration: |
83 |
# TODO : report pb |
|
84 |
pass |
|
85 |
offset = len(match_xhtml.group(0)) - len(match_xhtml.group(1)) |
|
86 |
result.append(xhtml[last_index:match_xhtml.start() + offset - 1]) |
|
87 |
result.append(img_path) |
|
88 |
last_index = match_xhtml.end() - 1 # -1 because trailing " |
|
89 |
result.append(xhtml[last_index:len(xhtml)]) |
|
90 |
return u''.join(result) |
|
91 |
||
|
77
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
50
diff
changeset
|
92 |
def convert_oo_to_html(input): |
|
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
50
diff
changeset
|
93 |
from oo_converters import convert |
| 0 | 94 |
html_input, images = convert(input, 'html') |
95 |
||
96 |
enc = chardet.detect(html_input)['encoding'] |
|
97 |
try_encodings = [enc, 'utf8', 'latin1'] |
|
98 |
for encoding in try_encodings: |
|
99 |
try: |
|
100 |
res_content_html = unicode(html_input, encoding) |
|
101 |
break; |
|
102 |
except UnicodeDecodeError: |
|
103 |
pass |
|
104 |
if not res_content_html: |
|
105 |
raise Exception('UnicodeDecodeError: could not decode') |
|
106 |
return res_content_html, images |
|
107 |
||
| 253 | 108 |
def fix_html_img_path(html): |
109 |
return html.replace('IMG SRC="../outdir/','IMG SRC="') |
|
110 |
||
| 252 | 111 |
def convert_oo_to_html_and_xhtml(input): |
|
77
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
50
diff
changeset
|
112 |
from oo_converters import convert |
| 0 | 113 |
html_input, images = convert(input, 'html') |
114 |
xhtml_input, _not_used_ = convert(input, 'xhtml') |
|
115 |
enc = chardet.detect(xhtml_input)['encoding'] |
|
116 |
try_encodings = [enc, 'utf8', 'latin1'] |
|
117 |
for encoding in try_encodings: |
|
118 |
try: |
|
119 |
res_content_html = unicode(html_input, encoding) |
|
120 |
res_content_xhtml = unicode(xhtml_input, encoding) |
|
121 |
break; |
|
122 |
except UnicodeDecodeError: |
|
123 |
pass |
|
| 253 | 124 |
|
125 |
res_content_xhtml = fix_img_path(res_content_html, res_content_xhtml, images) |
|
126 |
res_content_html = fix_html_img_path(res_content_html) |
|
127 |
||
| 0 | 128 |
if not res_content_html or not res_content_xhtml: |
129 |
raise Exception('UnicodeDecodeError: could not decode') |
|
| 252 | 130 |
return res_content_html, cleanup(res_content_xhtml), images |
| 0 | 131 |
|
| 252 | 132 |
def cleanup(string): |
133 |
return string.replace(u'\xc2\xa0',u'') |
|
134 |
||
| 0 | 135 |
def markdown_from_code(code): |
136 |
CODE_INDICATOR = " " # 4 spaces |
|
137 |
return '\n'.join([CODE_INDICATOR + line for line in code.split('\n')]) |
|
138 |
||
139 |