| author | gibus |
| Thu, 21 Jul 2011 09:42:33 +0200 | |
| changeset 360 | bfaab8740995 |
| parent 259 | 0371caf8bcc6 |
| child 416 | 5573b959131d |
| 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 |
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
6 |
from oo_converters import extract_css_body |
| 252 | 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 |
############################## |
|
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
21 |
# OO/MS-Word |
| 0 | 22 |
if mime_type in ['application/vnd.oasis.opendocument.text', |
23 |
'application/msword', |
|
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
24 |
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' |
| 0 | 25 |
]: |
26 |
||
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
27 |
from cm.cm_settings import USE_ABI |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
28 |
if USE_ABI: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
29 |
from abi_converters import AbiFileConverter |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
30 |
converter = AbiFileConverter() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
31 |
html_input, attachs = converter.convert_to_html(input) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
32 |
html_input = re.sub(r' awml:style="[^"]*"', '', html_input) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
33 |
converted_input = pandoc_convert(html_input, 'html', format) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
34 |
else: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
35 |
html_input, xhtml_input, attachs = convert_oo_to_html_and_xhtml(input) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
36 |
if format == 'html': |
| 252 | 37 |
_not_used_css, converted_input = extract_css_body(xhtml_input) |
38 |
#converted_input = xhtml_input |
|
|
259
0371caf8bcc6
always use pandoc but in raw mode for html->html convert
raph
parents:
253
diff
changeset
|
39 |
|
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
40 |
converted_input = pandoc_convert(html_input, 'html', format) |
| 0 | 41 |
|
42 |
############################## |
|
| 118 | 43 |
# latex |
44 |
elif mime_type in ['application/x-latex','text/x-tex',]: |
|
45 |
converted_input = pandoc_convert(to_unicode(input), 'latex', format) |
|
46 |
||
47 |
############################## |
|
| 0 | 48 |
# anything looks like code: put them into markdown citation |
| 118 | 49 |
elif mime_type.startswith('text/x-') or mime_type in ['application/x-ruby',]: |
| 0 | 50 |
converted_input = markdown_from_code(input) |
51 |
||
52 |
############################## |
|
53 |
# html |
|
54 |
elif mime_type in ['text/html', 'application/xhtml+xml']: |
|
55 |
if format == 'html': |
|
56 |
converted_input = input |
|
|
259
0371caf8bcc6
always use pandoc but in raw mode for html->html convert
raph
parents:
253
diff
changeset
|
57 |
|
|
0371caf8bcc6
always use pandoc but in raw mode for html->html convert
raph
parents:
253
diff
changeset
|
58 |
converted_input = pandoc_convert(input, 'html', format) |
| 78 | 59 |
############################## |
60 |
# anything looks like text -> markdown |
|
61 |
elif mime_type in ['text/plain', |
|
62 |
'text/english', |
|
63 |
'text/enriched' |
|
64 |
]: |
|
65 |
converted_input = to_unicode(input) |
|
66 |
############################## |
|
67 |
# default case: assume it's text |
|
68 |
else: |
|
69 |
converted_input = to_unicode(input) |
|
70 |
||
| 0 | 71 |
|
72 |
return converted_input, attachs |
|
73 |
||
74 |
def fix_img_path(html, xhtml, imgs): |
|
75 |
""" |
|
76 |
imgs : name --> path |
|
77 |
""" |
|
78 |
finder_re = 'src[\s]*=[\s]*\"((?:(?!https?))[^\"]*)\"' |
|
79 |
len_res_html = len(re.findall(finder_re, html, re.IGNORECASE)) |
|
80 |
len_res_xhtml = len(re.findall(finder_re, xhtml, re.IGNORECASE)) |
|
81 |
res_html = re.finditer(finder_re, html, re.IGNORECASE) |
|
82 |
res_xhtml = re.finditer(finder_re, xhtml, re.IGNORECASE) |
|
83 |
result = [] |
|
84 |
last_index = 0 |
|
85 |
for match_xhtml in res_xhtml: |
|
86 |
img_path = '' |
|
87 |
try: |
|
88 |
match_html = res_html.next() |
|
89 |
if match_html: |
|
90 |
img_name = match_html.group(1) |
|
| 253 | 91 |
img_path = os.path.split(img_name)[-1] |
| 0 | 92 |
except StopIteration: |
93 |
# TODO : report pb |
|
94 |
pass |
|
95 |
offset = len(match_xhtml.group(0)) - len(match_xhtml.group(1)) |
|
96 |
result.append(xhtml[last_index:match_xhtml.start() + offset - 1]) |
|
97 |
result.append(img_path) |
|
98 |
last_index = match_xhtml.end() - 1 # -1 because trailing " |
|
99 |
result.append(xhtml[last_index:len(xhtml)]) |
|
100 |
return u''.join(result) |
|
101 |
||
|
77
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
50
diff
changeset
|
102 |
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
|
103 |
from oo_converters import convert |
| 0 | 104 |
html_input, images = convert(input, 'html') |
105 |
||
106 |
enc = chardet.detect(html_input)['encoding'] |
|
107 |
try_encodings = [enc, 'utf8', 'latin1'] |
|
108 |
for encoding in try_encodings: |
|
109 |
try: |
|
110 |
res_content_html = unicode(html_input, encoding) |
|
111 |
break; |
|
112 |
except UnicodeDecodeError: |
|
113 |
pass |
|
114 |
if not res_content_html: |
|
115 |
raise Exception('UnicodeDecodeError: could not decode') |
|
116 |
return res_content_html, images |
|
117 |
||
| 253 | 118 |
def fix_html_img_path(html): |
119 |
return html.replace('IMG SRC="../outdir/','IMG SRC="') |
|
120 |
||
| 252 | 121 |
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
|
122 |
from oo_converters import convert |
| 0 | 123 |
html_input, images = convert(input, 'html') |
124 |
xhtml_input, _not_used_ = convert(input, 'xhtml') |
|
125 |
enc = chardet.detect(xhtml_input)['encoding'] |
|
126 |
try_encodings = [enc, 'utf8', 'latin1'] |
|
127 |
for encoding in try_encodings: |
|
128 |
try: |
|
129 |
res_content_html = unicode(html_input, encoding) |
|
130 |
res_content_xhtml = unicode(xhtml_input, encoding) |
|
131 |
break; |
|
132 |
except UnicodeDecodeError: |
|
133 |
pass |
|
| 253 | 134 |
|
135 |
res_content_xhtml = fix_img_path(res_content_html, res_content_xhtml, images) |
|
136 |
res_content_html = fix_html_img_path(res_content_html) |
|
137 |
||
| 0 | 138 |
if not res_content_html or not res_content_xhtml: |
139 |
raise Exception('UnicodeDecodeError: could not decode') |
|
| 252 | 140 |
return res_content_html, cleanup(res_content_xhtml), images |
| 0 | 141 |
|
| 252 | 142 |
def cleanup(string): |
143 |
return string.replace(u'\xc2\xa0',u'') |
|
144 |
||
| 0 | 145 |
def markdown_from_code(code): |
146 |
CODE_INDICATOR = " " # 4 spaces |
|
147 |
return '\n'.join([CODE_INDICATOR + line for line in code.split('\n')]) |
|
148 |
||
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
259
diff
changeset
|
149 |