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