| author | raph |
| Mon, 25 Jan 2010 09:23:32 +0100 | |
| changeset 118 | 75d94dd14511 |
| parent 78 | dda94db1149a |
| child 119 | 5e8dda1b7631 |
| 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 |
############################## |
|
| 118 | 38 |
# latex |
39 |
elif mime_type in ['application/x-latex','text/x-tex',]: |
|
40 |
converted_input = pandoc_convert(to_unicode(input), 'latex', format) |
|
41 |
||
42 |
############################## |
|
| 0 | 43 |
# anything looks like code: put them into markdown citation |
| 118 | 44 |
elif mime_type.startswith('text/x-') or mime_type in ['application/x-ruby',]: |
| 0 | 45 |
converted_input = markdown_from_code(input) |
46 |
||
47 |
############################## |
|
48 |
# html |
|
49 |
elif mime_type in ['text/html', 'application/xhtml+xml']: |
|
50 |
if format == 'html': |
|
51 |
converted_input = input |
|
52 |
else: |
|
53 |
converted_input = pandoc_convert(input, 'html', format) |
|
| 78 | 54 |
############################## |
55 |
# anything looks like text -> markdown |
|
56 |
elif mime_type in ['text/plain', |
|
57 |
'text/english', |
|
58 |
'text/enriched' |
|
59 |
]: |
|
60 |
converted_input = to_unicode(input) |
|
61 |
############################## |
|
62 |
# default case: assume it's text |
|
63 |
else: |
|
64 |
converted_input = to_unicode(input) |
|
65 |
||
| 0 | 66 |
|
67 |
return converted_input, attachs |
|
68 |
||
69 |
def fix_img_path(html, xhtml, imgs): |
|
70 |
""" |
|
71 |
imgs : name --> path |
|
72 |
""" |
|
73 |
finder_re = 'src[\s]*=[\s]*\"((?:(?!https?))[^\"]*)\"' |
|
74 |
len_res_html = len(re.findall(finder_re, html, re.IGNORECASE)) |
|
75 |
len_res_xhtml = len(re.findall(finder_re, xhtml, re.IGNORECASE)) |
|
76 |
res_html = re.finditer(finder_re, html, re.IGNORECASE) |
|
77 |
res_xhtml = re.finditer(finder_re, xhtml, re.IGNORECASE) |
|
78 |
result = [] |
|
79 |
last_index = 0 |
|
80 |
for match_xhtml in res_xhtml: |
|
81 |
img_path = '' |
|
82 |
try: |
|
83 |
match_html = res_html.next() |
|
84 |
if match_html: |
|
85 |
img_name = match_html.group(1) |
|
86 |
img_path = imgs[img_name] |
|
87 |
except StopIteration: |
|
88 |
# TODO : report pb |
|
89 |
pass |
|
90 |
offset = len(match_xhtml.group(0)) - len(match_xhtml.group(1)) |
|
91 |
result.append(xhtml[last_index:match_xhtml.start() + offset - 1]) |
|
92 |
result.append(img_path) |
|
93 |
last_index = match_xhtml.end() - 1 # -1 because trailing " |
|
94 |
result.append(xhtml[last_index:len(xhtml)]) |
|
95 |
return u''.join(result) |
|
96 |
||
|
77
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
50
diff
changeset
|
97 |
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
|
98 |
from oo_converters import convert |
| 0 | 99 |
html_input, images = convert(input, 'html') |
100 |
||
101 |
enc = chardet.detect(html_input)['encoding'] |
|
102 |
try_encodings = [enc, 'utf8', 'latin1'] |
|
103 |
res_content = None |
|
104 |
for encoding in try_encodings: |
|
105 |
try: |
|
106 |
res_content_html = unicode(html_input, encoding) |
|
107 |
break; |
|
108 |
except UnicodeDecodeError: |
|
109 |
pass |
|
110 |
if not res_content_html: |
|
111 |
raise Exception('UnicodeDecodeError: could not decode') |
|
112 |
return res_content_html, images |
|
113 |
||
|
77
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
50
diff
changeset
|
114 |
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
|
115 |
from oo_converters import convert |
| 0 | 116 |
html_input, images = convert(input, 'html') |
117 |
xhtml_input, _not_used_ = convert(input, 'xhtml') |
|
118 |
||
119 |
enc = chardet.detect(xhtml_input)['encoding'] |
|
120 |
try_encodings = [enc, 'utf8', 'latin1'] |
|
121 |
res_content = None |
|
122 |
for encoding in try_encodings: |
|
123 |
try: |
|
124 |
# TODO: fix path and manage images |
|
125 |
#res_content = fix_img_path(unicode(html_res_content,encoding), |
|
126 |
# unicode(xhtml_res_content,encoding), |
|
127 |
# iimg) |
|
128 |
res_content_html = unicode(html_input, encoding) |
|
129 |
res_content_xhtml = unicode(xhtml_input, encoding) |
|
130 |
break; |
|
131 |
except UnicodeDecodeError: |
|
132 |
pass |
|
133 |
if not res_content_html or not res_content_xhtml: |
|
134 |
raise Exception('UnicodeDecodeError: could not decode') |
|
135 |
return res_content_html, res_content_xhtml, images |
|
136 |
||
137 |
def markdown_from_code(code): |
|
138 |
CODE_INDICATOR = " " # 4 spaces |
|
139 |
return '\n'.join([CODE_INDICATOR + line for line in code.split('\n')]) |
|
140 |
||
141 |