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