|
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 |
|
7 from cm.converters.pandoc_converters import pandoc_convert |
|
8 from cm.converters.oo_converters import convert_html as oo_convert |
|
9 from cm.converters.oo_converters import combine_css_body |
|
10 from cm.models import Text, TextVersion, Attachment, Comment |
|
11 import mimetypes |
|
12 import simplejson |
|
13 EXPORT2_INFOS = { |
|
14 # key -> { mimetype, extension} |
|
15 's5' : {}, |
|
16 'pdf' : {'mimetype': 'application/pdf', 'extension':'pdf'}, |
|
17 'markdown' : {'mimetype': 'text/plain', 'extension':'mkd'}, |
|
18 'odt' : {'mimetype': 'application/vnd.oasis.opendocument.text', 'extension':'odt'}, |
|
19 'latex' :{'mimetype': 'text/x-tex', 'extension':'tex'}, |
|
20 'html' :{'mimetype': 'text/html', 'extension':'html'}, |
|
21 # raw export |
|
22 'raw' : {'mimetype': 'text/plain', 'extension':'txt'} |
|
23 } |
|
24 def content_export2(request, content, title, content_format, format, use_pandoc, download_response): |
|
25 # TODO : formats must be imported from converters |
|
26 #import pdb;pdb.set_trace() |
|
27 if format == 'raw' : |
|
28 export_content = content |
|
29 elif content_format == 'html' and format == 'html': |
|
30 export_content = HTML_HEADER % content |
|
31 elif content_format == 'markdown' and format == 'markdown': |
|
32 export_content = content |
|
33 else: |
|
34 if use_pandoc : |
|
35 export_content = pandoc_convert(content, content_format, format, full=True) |
|
36 else : |
|
37 fix_content = content |
|
38 if content_format == 'html': |
|
39 fix_content = combine_css_body(content, '') |
|
40 export_content = oo_convert(fix_content, format) |
|
41 |
|
42 export_infos = EXPORT2_INFOS[format] |
|
43 |
|
44 if download_response: |
|
45 return _response_download(export_content, title, export_infos['mimetype'], export_infos['extension']) ; |
|
46 else: |
|
47 return _response_write(export_content) |
|
48 |
|
49 def content_export_new(request, content, title, src_format, format, use_pandoc, download_response): |
|
50 # TODO : formats must be imported from converters |
|
51 if format == 'raw' : |
|
52 export_content = content |
|
53 elif src_format == format and format == 'html': |
|
54 export_content = HTML_HEADER % content |
|
55 else: |
|
56 if use_pandoc : |
|
57 export_content = pandoc_convert(content, src_format, format, full=True) |
|
58 else : |
|
59 fix_content = content |
|
60 if src_format == 'html': |
|
61 fix_content = combine_css_body(content, '') |
|
62 export_content = oo_convert(fix_content, format) |
|
63 |
|
64 export_infos = EXPORT_INFOS[format] |
|
65 format_download = export_infos[0] |
|
66 |
|
67 if download_response: |
|
68 return _response_download(export_content, export_infos[1], export_infos[2]) ; |
|
69 else: |
|
70 return _response_write(export_content) |
|
71 |
|
72 |
|
73 # read conversion chain |
|
74 # execute chain |
|
75 # ready to send response |
|
76 # # TODO : formats must be imported from converters |
|
77 # if format == 'raw' : |
|
78 # export_content = content |
|
79 # elif src_format == format and format == 'html': |
|
80 # export_content = HTML_HEADER % content |
|
81 # else: |
|
82 # if use_pandoc : |
|
83 # export_content = pandoc_convert(content, src_format, format, full=True) |
|
84 # else : |
|
85 # fix_content = content |
|
86 # if src_format == 'html': |
|
87 # fix_content = combine_css_body(content, '') |
|
88 # export_content = oo_convert(fix_content, format) |
|
89 # |
|
90 ## send response |
|
91 # export_infos = EXPORT_INFOS[format] |
|
92 # mimetype = export_infos['mimetype'] |
|
93 # extension = export_infos['extension'] |
|
94 # |
|
95 # if download: |
|
96 # return _response_download(export_content, mimetype, extension) |
|
97 # else : |
|
98 # return _response_write(export_content) |
|
99 # |
|
100 def _response_download(content, title, mimetype, extension): |
|
101 response = HttpResponse(mimetype=mimetype) |
|
102 file_title = title + '.' + extension |
|
103 from email.header import Header |
|
104 encoded_name = str(Header(file_title.encode('utf8'), charset='utf8', maxlinelen=500)) |
|
105 response['Content-Disposition'] = u'attachment; filename=%s' % encoded_name |
|
106 response.write(content) |
|
107 return response |
|
108 |
|
109 def _response_write(content): |
|
110 response = HttpResponse() |
|
111 response.write(content) |
|
112 return response |
|
113 |
|
114 |
|
115 EXPORT_INFOS = { |
|
116 # key -> [ download?, mimetype, extension] |
|
117 's5' : [False , ], |
|
118 'pdf' : [True , 'application/pdf' , 'pdf'], |
|
119 'markdown' : [True , 'text/plain' , 'mkd'], |
|
120 'odt' : [True , 'application/vnd.oasis.opendocument.text', 'odt'], |
|
121 'latex' :[True , 'text/x-tex', 'tex'], |
|
122 'html' :[True , 'text/html', 'html'], |
|
123 # raw export |
|
124 'raw' : [True, 'text/plain', 'txt'] |
|
125 } |
|
126 HTML_HEADER = u""" |
|
127 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
|
128 <html><head> |
|
129 <STYLE TYPE='text/css'> |
|
130 div.pagebreakhere { |
|
131 page-break-before: always ; |
|
132 } |
|
133 </STYLE> |
|
134 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head> |
|
135 <body>%s</body> |
|
136 </html> |
|
137 """ |
|
138 EXPORT_FORMATS = EXPORT_INFOS.keys() |
|
139 |
|
140 def content_export(request, content, title, src_format, format, use_pandoc): |
|
141 # TODO : formats must be imported from converters |
|
142 if format == 'raw' : |
|
143 export_content = content |
|
144 elif src_format == format and format == 'html': |
|
145 export_content = HTML_HEADER % content |
|
146 else: |
|
147 if use_pandoc : |
|
148 export_content = pandoc_convert(content, src_format, format, full=True) |
|
149 else : |
|
150 fix_content = content |
|
151 if src_format == 'html': |
|
152 fix_content = combine_css_body(content, '') |
|
153 export_content = oo_convert(fix_content, format) |
|
154 |
|
155 export_infos = EXPORT_INFOS[format] |
|
156 format_download = export_infos[0] |
|
157 |
|
158 if format_download: |
|
159 format_mimetype = export_infos[1] |
|
160 format_extension = export_infos[2] |
|
161 |
|
162 response = HttpResponse(mimetype=format_mimetype) |
|
163 file_title = title + '.' + format_extension |
|
164 from email.header import Header |
|
165 encoded_name = str(Header(file_title.encode('utf8'), charset='utf8', maxlinelen=500)) |
|
166 response['Content-Disposition'] = u'attachment; filename=%s' % encoded_name |
|
167 response.write(export_content) |
|
168 return response |
|
169 else: |
|
170 response = HttpResponse() |
|
171 response.write(export_content) |
|
172 return response |
|
173 |
|
174 |
|
175 def text_export(request, key, format): |
|
176 # TODO : formats must be imported from converters |
|
177 format = format.lower() |
|
178 if format not in EXPORT_FORMATS: |
|
179 raise Exception("Unsupported format %s (supported formats %s)" % (format, ' '.join(EXPORT_FORMATS))) |
|
180 text = Text.objects.get(key=key) |
|
181 text_version = text.get_latest_version() |
|
182 |
|
183 return content_export(request, text_version.content, text_version.title, text_version.format, format) |
|
184 |
|
185 def text_feed(request, key): |
|
186 return "" |