10 from cm.models import Text, TextVersion, Attachment, Comment |
10 from cm.models import Text, TextVersion, Attachment, Comment |
11 from cm.security import get_viewable_comments |
11 from cm.security import get_viewable_comments |
12 import mimetypes |
12 import mimetypes |
13 import simplejson |
13 import simplejson |
14 from cm.cm_settings import USE_ABI |
14 from cm.cm_settings import USE_ABI |
|
15 |
15 EXPORT2_INFOS = { |
16 EXPORT2_INFOS = { |
16 # key -> { mimetype, extension} |
17 # key -> { mimetype, extension} |
17 's5' : {}, |
18 's5' : {}, |
18 'pdf' : {'mimetype': 'application/pdf', 'extension':'pdf'}, |
19 'pdf' : {'mimetype': 'application/pdf', 'extension':'pdf'}, |
19 'markdown' : {'mimetype': 'text/plain', 'extension':'mkd'}, |
20 'markdown' : {'mimetype': 'text/plain', 'extension':'mkd'}, |
24 'html' :{'mimetype': 'text/html', 'extension':'html'}, |
25 'html' :{'mimetype': 'text/html', 'extension':'html'}, |
25 'epub' :{'mimetype': 'application/epub+zip', 'extension':'epub'}, |
26 'epub' :{'mimetype': 'application/epub+zip', 'extension':'epub'}, |
26 'raw' : {'mimetype': 'text/plain', 'extension':'txt'}, |
27 'raw' : {'mimetype': 'text/plain', 'extension':'txt'}, |
27 'xml' : {'mimetype': 'text/xml', 'extension':'xml'}, |
28 'xml' : {'mimetype': 'text/xml', 'extension':'xml'}, |
28 } |
29 } |
|
30 |
|
31 HTML_HEADER = u""" |
|
32 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
|
33 <html><head> |
|
34 <STYLE TYPE='text/css'> |
|
35 div.pagebreakhere { |
|
36 page-break-before: always ; |
|
37 } |
|
38 </STYLE> |
|
39 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head> |
|
40 <body>%s</body> |
|
41 </html> |
|
42 """ |
|
43 |
29 def content_export2(request, content, title, content_format, format, use_pandoc, download_response): |
44 def content_export2(request, content, title, content_format, format, use_pandoc, download_response): |
30 # TODO : formats must be imported from converters |
45 # TODO : formats must be imported from converters |
31 # import pdb;pdb.set_trace() |
46 # import pdb;pdb.set_trace() |
32 if format == 'raw' : |
47 if format == 'raw' : |
33 export_content = content |
48 export_content = content |
36 elif content_format == 'markdown' and format == 'markdown': |
51 elif content_format == 'markdown' and format == 'markdown': |
37 export_content = content |
52 export_content = content |
38 else: |
53 else: |
39 if use_pandoc : |
54 if use_pandoc : |
40 # markdown2pdf is buggy => convert to HTML and use abiword to export in PDF |
55 # markdown2pdf is buggy => convert to HTML and use abiword to export in PDF |
41 if format == 'pdf' and USE_ABI: |
56 if format in ('pdf', 'odt', 'docx', 'doc') and USE_ABI: |
42 html_content = pandoc_convert(content, content_format, 'html', full=True) |
57 html_content = pandoc_convert(content, content_format, 'html', full=True) |
43 from cm.converters.abi_converters import AbiFileConverter |
58 from cm.converters.abi_converters import AbiFileConverter |
44 converter = AbiFileConverter() |
59 converter = AbiFileConverter() |
45 full_content = converter.add_html_header(html_content) |
60 fix_content = converter.add_html_header(html_content) |
46 fix_content = do_tidy(full_content) |
|
47 export_content = converter.convert_from_html(fix_content, format) |
61 export_content = converter.convert_from_html(fix_content, format) |
48 else: |
62 else: |
49 export_content = pandoc_convert(content, content_format, format, full=True) |
63 export_content = pandoc_convert(content, content_format, format, full=True) |
50 else : |
64 else : |
51 fix_content = content |
65 fix_content = content |
52 if content_format == 'html': |
66 if content_format == 'html': |
53 if USE_ABI: |
67 if USE_ABI: |
54 from cm.converters.abi_converters import AbiFileConverter |
68 from cm.converters.abi_converters import AbiFileConverter |
55 converter = AbiFileConverter() |
69 converter = AbiFileConverter() |
56 full_content = converter.add_html_header(content) |
70 fix_content = converter.add_html_header(content) |
57 fix_content = do_tidy(full_content) |
|
58 else: |
71 else: |
59 from cm.converters.oo_converters import combine_css_body |
72 from cm.converters.oo_converters import combine_css_body |
60 fix_content = combine_css_body(content, '') |
73 fix_content = combine_css_body(content, '') |
61 if USE_ABI: |
74 if USE_ABI: |
62 from cm.converters.abi_converters import AbiFileConverter |
75 from cm.converters.abi_converters import AbiFileConverter |
83 if download_response: |
96 if download_response: |
84 return _response_download(export_content, title, export_infos['mimetype'], export_infos['extension']) ; |
97 return _response_download(export_content, title, export_infos['mimetype'], export_infos['extension']) ; |
85 else: |
98 else: |
86 return _response_write(export_content) |
99 return _response_write(export_content) |
87 |
100 |
88 def content_export_new(request, content, title, src_format, format, use_pandoc, download_response): |
|
89 # TODO : formats must be imported from converters |
|
90 if format == 'raw' : |
|
91 export_content = content |
|
92 elif src_format == format and format == 'html': |
|
93 export_content = HTML_HEADER % content |
|
94 else: |
|
95 if use_pandoc : |
|
96 export_content = pandoc_convert(content, src_format, format, full=True) |
|
97 else : |
|
98 fix_content = content |
|
99 if src_format == 'html': |
|
100 from cm.converters.oo_converters import combine_css_body |
|
101 fix_content = combine_css_body(content, '') |
|
102 from cm.converters.oo_converters import convert_html as oo_convert |
|
103 export_content = oo_convert(fix_content, format) |
|
104 |
|
105 export_infos = EXPORT_INFOS[format] |
|
106 format_download = export_infos[0] |
|
107 |
|
108 if download_response: |
|
109 return _response_download(export_content, export_infos[1], export_infos[2]) ; |
|
110 else: |
|
111 return _response_write(export_content) |
|
112 |
|
113 |
101 |
114 # read conversion chain |
|
115 # execute chain |
|
116 # ready to send response |
|
117 # # TODO : formats must be imported from converters |
|
118 # if format == 'raw' : |
|
119 # export_content = content |
|
120 # elif src_format == format and format == 'html': |
|
121 # export_content = HTML_HEADER % content |
|
122 # else: |
|
123 # if use_pandoc : |
|
124 # export_content = pandoc_convert(content, src_format, format, full=True) |
|
125 # else : |
|
126 # fix_content = content |
|
127 # if src_format == 'html': |
|
128 # fix_content = combine_css_body(content, '') |
|
129 # export_content = oo_convert(fix_content, format) |
|
130 # |
|
131 ## send response |
|
132 # export_infos = EXPORT_INFOS[format] |
|
133 # mimetype = export_infos['mimetype'] |
|
134 # extension = export_infos['extension'] |
|
135 # |
|
136 # if download: |
|
137 # return _response_download(export_content, mimetype, extension) |
|
138 # else : |
|
139 # return _response_write(export_content) |
|
140 # |
|
141 def _response_download(content, title, mimetype, extension): |
102 def _response_download(content, title, mimetype, extension): |
142 response = HttpResponse(mimetype=mimetype) |
103 response = HttpResponse(mimetype=mimetype) |
143 file_title = title + '.' + extension |
104 file_title = title + '.' + extension |
144 from email.header import Header |
105 from email.header import Header |
145 encoded_name = str(Header(file_title.encode('utf8'), charset='utf8', maxlinelen=500)) |
106 encoded_name = str(Header(file_title.encode('utf8'), charset='utf8', maxlinelen=500)) |
152 def _response_write(content): |
113 def _response_write(content): |
153 response = HttpResponse() |
114 response = HttpResponse() |
154 response.write(content) |
115 response.write(content) |
155 return response |
116 return response |
156 |
117 |
157 |
|
158 EXPORT_INFOS = { |
|
159 # key -> [ download?, mimetype, extension] |
|
160 's5' : [False , ], |
|
161 'pdf' : [True , 'application/pdf' , 'pdf'], |
|
162 'markdown' : [True , 'text/plain' , 'mkd'], |
|
163 'odt' : [True , 'application/vnd.oasis.opendocument.text', 'odt'], |
|
164 'doc' : [True , 'application/msword', 'odt'], |
|
165 'docx' : [True , 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'docx'], |
|
166 'latex' :[True , 'text/x-tex', 'tex'], |
|
167 'html' :[True , 'text/html', 'html'], |
|
168 'epub' :[True , 'application/epub+zip', 'epub'], |
|
169 # raw export |
|
170 'raw' : [True, 'text/plain', 'txt'] |
|
171 } |
|
172 HTML_HEADER = u""" |
|
173 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
|
174 <html><head> |
|
175 <STYLE TYPE='text/css'> |
|
176 div.pagebreakhere { |
|
177 page-break-before: always ; |
|
178 } |
|
179 </STYLE> |
|
180 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head> |
|
181 <body>%s</body> |
|
182 </html> |
|
183 """ |
|
184 EXPORT_FORMATS = EXPORT_INFOS.keys() |
|
185 |
|
186 def content_export(request, content, title, src_format, format, use_pandoc): |
|
187 # TODO : formats must be imported from converters |
|
188 if format == 'raw' : |
|
189 export_content = content |
|
190 elif src_format == format and format == 'html': |
|
191 export_content = HTML_HEADER % content |
|
192 else: |
|
193 if use_pandoc : |
|
194 export_content = pandoc_convert(content, src_format, format, full=True) |
|
195 else : |
|
196 fix_content = content |
|
197 if src_format == 'html': |
|
198 from cm.converters.oo_converters import combine_css_body |
|
199 fix_content = combine_css_body(content, '') |
|
200 from cm.converters.oo_converters import convert_html as oo_convert |
|
201 export_content = oo_convert(fix_content, format) |
|
202 |
|
203 export_infos = EXPORT_INFOS[format] |
|
204 format_download = export_infos[0] |
|
205 |
|
206 if format_download: |
|
207 format_mimetype = export_infos[1] |
|
208 format_extension = export_infos[2] |
|
209 |
|
210 response = HttpResponse(mimetype=format_mimetype) |
|
211 file_title = title + '.' + format_extension |
|
212 from email.header import Header |
|
213 encoded_name = str(Header(file_title.encode('utf8'), charset='utf8', maxlinelen=500)) |
|
214 response['Content-Disposition'] = u'attachment; filename=%s' % encoded_name |
|
215 response.write(export_content) |
|
216 return response |
|
217 else: |
|
218 response = HttpResponse() |
|
219 response.write(export_content) |
|
220 return response |
|
221 |
|
222 |
|
223 def text_export(request, key, format): |
|
224 # TODO : formats must be imported from converters |
|
225 format = format.lower() |
|
226 if format not in EXPORT_FORMATS: |
|
227 raise Exception("Unsupported format %s (supported formats %s)" % (format, ' '.join(EXPORT_FORMATS))) |
|
228 text = Text.objects.get(key=key) |
|
229 text_version = text.get_latest_version() |
|
230 |
|
231 return content_export(request, text_version.content, text_version.title, text_version.format, format) |
|
232 |
|
233 def text_feed(request, key): |
|
234 return "" |
|
235 |
|
236 def xml_export(request, text_version, whichcomments): |
118 def xml_export(request, text_version, whichcomments): |
237 # Text version infos |
119 # Text version infos |
238 template_dict = { 'title': text_version.title, 'date': text_version.modified, 'format': text_version.format, 'content': text_version.get_content(), 'tags': text_version.tags, } |
120 template_dict = { 'title': text_version.title, 'date': text_version.modified, 'format': text_version.format, 'content': text_version.content, 'tags': text_version.tags, } |
239 |
121 |
240 # Comments |
122 # Comments |
241 comments = [] # whichcomments=="none" |
123 comments = [] # whichcomments=="none" |
242 if whichcomments == "filtered" or whichcomments == "all": |
124 if whichcomments == "filtered" or whichcomments == "all": |
243 _comments = text_version.comment_set.all() |
125 _comments = text_version.comment_set.all() |