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