| author | gibus |
| Mon, 25 Jul 2011 15:35:50 +0200 | |
| changeset 367 | e4a0c2fe8df2 |
| parent 364 | 41dd28557b5d |
| child 433 | 056d92bffb23 |
| permissions | -rw-r--r-- |
| 0 | 1 |
from django import forms |
2 |
from django.core.urlresolvers import reverse |
|
3 |
from django.http import HttpResponse, HttpResponseRedirect, Http404 |
|
4 |
from django.shortcuts import render_to_response |
|
5 |
from django.template import RequestContext |
|
6 |
from django.utils.translation import ugettext as _, ugettext_lazy |
|
| 364 | 7 |
from cm.converters.pandoc_converters import pandoc_convert, do_tidy |
| 0 | 8 |
from cm.models import Text, TextVersion, Attachment, Comment |
9 |
import mimetypes |
|
10 |
import simplejson |
|
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
11 |
from cm.cm_settings import USE_ABI |
| 0 | 12 |
EXPORT2_INFOS = { |
13 |
# key -> { mimetype, extension} |
|
14 |
's5' : {}, |
|
15 |
'pdf' : {'mimetype': 'application/pdf', 'extension':'pdf'}, |
|
16 |
'markdown' : {'mimetype': 'text/plain', 'extension':'mkd'}, |
|
17 |
'odt' : {'mimetype': 'application/vnd.oasis.opendocument.text', 'extension':'odt'}, |
|
18 |
'latex' :{'mimetype': 'text/x-tex', 'extension':'tex'}, |
|
19 |
'html' :{'mimetype': 'text/html', 'extension':'html'}, |
|
20 |
# raw export |
|
21 |
'raw' : {'mimetype': 'text/plain', 'extension':'txt'} |
|
22 |
} |
|
23 |
def content_export2(request, content, title, content_format, format, use_pandoc, download_response): |
|
24 |
# TODO : formats must be imported from converters |
|
|
56
bd8a4ffc7dad
BUG FIX : pdf export PhA tests, TODO fix special html caracters in comments
reno
parents:
0
diff
changeset
|
25 |
# import pdb;pdb.set_trace() |
| 0 | 26 |
if format == 'raw' : |
27 |
export_content = content |
|
28 |
elif content_format == 'html' and format == 'html': |
|
29 |
export_content = HTML_HEADER % content |
|
30 |
elif content_format == 'markdown' and format == 'markdown': |
|
31 |
export_content = content |
|
32 |
else: |
|
33 |
if use_pandoc : |
|
|
367
e4a0c2fe8df2
markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents:
364
diff
changeset
|
34 |
# markdown2pdf is buggy => convert to HTML and use abiword to export in PDF |
|
e4a0c2fe8df2
markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents:
364
diff
changeset
|
35 |
if format == 'pdf' and USE_ABI: |
|
e4a0c2fe8df2
markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents:
364
diff
changeset
|
36 |
html_content = pandoc_convert(content, content_format, 'html', full=True) |
|
e4a0c2fe8df2
markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents:
364
diff
changeset
|
37 |
from cm.converters.abi_converters import AbiFileConverter |
|
e4a0c2fe8df2
markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents:
364
diff
changeset
|
38 |
converter = AbiFileConverter() |
|
e4a0c2fe8df2
markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents:
364
diff
changeset
|
39 |
full_content = converter.add_html_header(html_content) |
|
e4a0c2fe8df2
markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents:
364
diff
changeset
|
40 |
fix_content = do_tidy(full_content) |
|
e4a0c2fe8df2
markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents:
364
diff
changeset
|
41 |
export_content = converter.convert_from_html(fix_content, format) |
|
e4a0c2fe8df2
markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents:
364
diff
changeset
|
42 |
else: |
| 0 | 43 |
export_content = pandoc_convert(content, content_format, format, full=True) |
44 |
else : |
|
45 |
fix_content = content |
|
46 |
if content_format == 'html': |
|
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
47 |
if USE_ABI: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
48 |
from cm.converters.abi_converters import AbiFileConverter |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
49 |
converter = AbiFileConverter() |
| 364 | 50 |
full_content = converter.add_html_header(content) |
51 |
fix_content = do_tidy(full_content) |
|
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
52 |
else: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
53 |
from cm.converters.oo_converters import combine_css_body |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
54 |
fix_content = combine_css_body(content, '') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
55 |
if USE_ABI: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
56 |
from cm.converters.abi_converters import AbiFileConverter |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
57 |
converter = AbiFileConverter() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
58 |
export_content = converter.convert_from_html(fix_content, format) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
59 |
else: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
60 |
from cm.converters.oo_converters import convert_html as oo_convert |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
61 |
export_content = oo_convert(fix_content, format) |
| 0 | 62 |
|
63 |
export_infos = EXPORT2_INFOS[format] |
|
64 |
||
65 |
if download_response: |
|
66 |
return _response_download(export_content, title, export_infos['mimetype'], export_infos['extension']) ; |
|
67 |
else: |
|
68 |
return _response_write(export_content) |
|
69 |
||
70 |
def content_export_new(request, content, title, src_format, format, use_pandoc, download_response): |
|
71 |
# TODO : formats must be imported from converters |
|
72 |
if format == 'raw' : |
|
73 |
export_content = content |
|
74 |
elif src_format == format and format == 'html': |
|
75 |
export_content = HTML_HEADER % content |
|
76 |
else: |
|
77 |
if use_pandoc : |
|
78 |
export_content = pandoc_convert(content, src_format, format, full=True) |
|
79 |
else : |
|
80 |
fix_content = content |
|
81 |
if src_format == 'html': |
|
|
77
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
57
diff
changeset
|
82 |
from cm.converters.oo_converters import combine_css_body |
| 0 | 83 |
fix_content = combine_css_body(content, '') |
|
77
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
57
diff
changeset
|
84 |
from cm.converters.oo_converters import convert_html as oo_convert |
| 0 | 85 |
export_content = oo_convert(fix_content, format) |
86 |
||
87 |
export_infos = EXPORT_INFOS[format] |
|
88 |
format_download = export_infos[0] |
|
89 |
||
90 |
if download_response: |
|
91 |
return _response_download(export_content, export_infos[1], export_infos[2]) ; |
|
92 |
else: |
|
93 |
return _response_write(export_content) |
|
94 |
||
95 |
||
96 |
# read conversion chain |
|
97 |
# execute chain |
|
98 |
# ready to send response |
|
99 |
# # TODO : formats must be imported from converters |
|
100 |
# if format == 'raw' : |
|
101 |
# export_content = content |
|
102 |
# elif src_format == format and format == 'html': |
|
103 |
# export_content = HTML_HEADER % content |
|
104 |
# else: |
|
105 |
# if use_pandoc : |
|
106 |
# export_content = pandoc_convert(content, src_format, format, full=True) |
|
107 |
# else : |
|
108 |
# fix_content = content |
|
109 |
# if src_format == 'html': |
|
110 |
# fix_content = combine_css_body(content, '') |
|
111 |
# export_content = oo_convert(fix_content, format) |
|
112 |
# |
|
113 |
## send response |
|
114 |
# export_infos = EXPORT_INFOS[format] |
|
115 |
# mimetype = export_infos['mimetype'] |
|
116 |
# extension = export_infos['extension'] |
|
117 |
# |
|
118 |
# if download: |
|
119 |
# return _response_download(export_content, mimetype, extension) |
|
120 |
# else : |
|
121 |
# return _response_write(export_content) |
|
122 |
# |
|
123 |
def _response_download(content, title, mimetype, extension): |
|
124 |
response = HttpResponse(mimetype=mimetype) |
|
125 |
file_title = title + '.' + extension |
|
126 |
from email.header import Header |
|
127 |
encoded_name = str(Header(file_title.encode('utf8'), charset='utf8', maxlinelen=500)) |
|
|
52
9fa013909d9a
prevent header error if newline in header (pb not fully solved)
raph
parents:
0
diff
changeset
|
128 |
# TODO: find a way to include long (more than 76 chars) into header |
|
9fa013909d9a
prevent header error if newline in header (pb not fully solved)
raph
parents:
0
diff
changeset
|
129 |
encoded_name = encoded_name.replace('\n','') |
|
9fa013909d9a
prevent header error if newline in header (pb not fully solved)
raph
parents:
0
diff
changeset
|
130 |
response['Content-Disposition'] = 'attachment; filename=%s' % encoded_name |
| 0 | 131 |
response.write(content) |
132 |
return response |
|
133 |
||
134 |
def _response_write(content): |
|
135 |
response = HttpResponse() |
|
136 |
response.write(content) |
|
137 |
return response |
|
138 |
||
139 |
||
140 |
EXPORT_INFOS = { |
|
141 |
# key -> [ download?, mimetype, extension] |
|
142 |
's5' : [False , ], |
|
143 |
'pdf' : [True , 'application/pdf' , 'pdf'], |
|
144 |
'markdown' : [True , 'text/plain' , 'mkd'], |
|
145 |
'odt' : [True , 'application/vnd.oasis.opendocument.text', 'odt'], |
|
146 |
'latex' :[True , 'text/x-tex', 'tex'], |
|
147 |
'html' :[True , 'text/html', 'html'], |
|
148 |
# raw export |
|
149 |
'raw' : [True, 'text/plain', 'txt'] |
|
150 |
} |
|
151 |
HTML_HEADER = u""" |
|
152 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
|
153 |
<html><head> |
|
154 |
<STYLE TYPE='text/css'> |
|
155 |
div.pagebreakhere { |
|
156 |
page-break-before: always ; |
|
157 |
} |
|
158 |
</STYLE> |
|
159 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head> |
|
160 |
<body>%s</body> |
|
161 |
</html> |
|
162 |
""" |
|
163 |
EXPORT_FORMATS = EXPORT_INFOS.keys() |
|
164 |
||
165 |
def content_export(request, content, title, src_format, format, use_pandoc): |
|
166 |
# TODO : formats must be imported from converters |
|
167 |
if format == 'raw' : |
|
168 |
export_content = content |
|
169 |
elif src_format == format and format == 'html': |
|
170 |
export_content = HTML_HEADER % content |
|
171 |
else: |
|
172 |
if use_pandoc : |
|
173 |
export_content = pandoc_convert(content, src_format, format, full=True) |
|
174 |
else : |
|
175 |
fix_content = content |
|
176 |
if src_format == 'html': |
|
|
77
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
57
diff
changeset
|
177 |
from cm.converters.oo_converters import combine_css_body |
| 0 | 178 |
fix_content = combine_css_body(content, '') |
|
77
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
57
diff
changeset
|
179 |
from cm.converters.oo_converters import convert_html as oo_convert |
| 0 | 180 |
export_content = oo_convert(fix_content, format) |
181 |
||
182 |
export_infos = EXPORT_INFOS[format] |
|
183 |
format_download = export_infos[0] |
|
184 |
||
185 |
if format_download: |
|
186 |
format_mimetype = export_infos[1] |
|
187 |
format_extension = export_infos[2] |
|
188 |
||
189 |
response = HttpResponse(mimetype=format_mimetype) |
|
190 |
file_title = title + '.' + format_extension |
|
191 |
from email.header import Header |
|
192 |
encoded_name = str(Header(file_title.encode('utf8'), charset='utf8', maxlinelen=500)) |
|
193 |
response['Content-Disposition'] = u'attachment; filename=%s' % encoded_name |
|
194 |
response.write(export_content) |
|
195 |
return response |
|
196 |
else: |
|
197 |
response = HttpResponse() |
|
198 |
response.write(export_content) |
|
199 |
return response |
|
200 |
||
201 |
||
202 |
def text_export(request, key, format): |
|
203 |
# TODO : formats must be imported from converters |
|
204 |
format = format.lower() |
|
205 |
if format not in EXPORT_FORMATS: |
|
206 |
raise Exception("Unsupported format %s (supported formats %s)" % (format, ' '.join(EXPORT_FORMATS))) |
|
207 |
text = Text.objects.get(key=key) |
|
208 |
text_version = text.get_latest_version() |
|
209 |
||
210 |
return content_export(request, text_version.content, text_version.title, text_version.format, format) |
|
211 |
||
212 |
def text_feed(request, key): |
|
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
213 |
return "" |