292 ret = HttpResponse() |
295 ret = HttpResponse() |
293 ret.status_code = 403 |
296 ret.status_code = 403 |
294 return ret |
297 return ret |
295 |
298 |
296 |
299 |
297 def from_html_links_to_abs_links(content): |
300 def from_html_links_to_inline_imgs(content, inline=True, full_path=True): |
298 """ |
301 """ |
299 Replaces (html) links to attachs with real file path on server |
302 Replaces (html) links to attachs with embeded inline images |
300 """ |
303 """ |
301 attach_re = r'/text/(?P<key>\w*)/attach/(?P<attach_key>\w*)/' |
304 content = re.sub("%s" %settings.SITE_URL, '', content) # changes absolute urls to relative urls |
302 attach_str = r'/text/%s/attach/%s/' |
305 attach_re = r'(?:/text/(?P<key>\w*))?/attach/(?P<attach_key>\w*)/' |
303 for match in re.findall(attach_re, content): |
306 attach_str_textversion = r'/text/%s/attach/%s/' |
304 link = attach_str %match |
307 attach_str = r'/attach/%s/' |
305 attach = Attachment.objects.get(key=match[1], text_version__text__key=match[0]) |
308 for match in re.findall(attach_re, content): |
|
309 if match[0]: |
|
310 link = attach_str_textversion %match |
|
311 else: |
|
312 link = attach_str %match[1] |
|
313 |
|
314 attach = Attachment.objects.get(key=match[1]) |
|
315 if inline: |
|
316 img_fmt = imghdr.what(attach.data.path) |
|
317 img = open(attach.data.path, 'rb') |
|
318 data = base64.b64encode(img.read()) |
|
319 img.close() |
|
320 content = content.replace(link, 'data:image/'+img_fmt+';base64,'+data) |
|
321 else: |
|
322 if full_path: |
306 content = content.replace(link, attach.data.path) |
323 content = content.replace(link, attach.data.path) |
307 return content |
324 else: |
308 |
325 img_fmt = imghdr.what(attach.data.path) |
309 #NOTE : some arguments like : withcolor = "yes" + format = "markdown" are incompatible |
326 content = content.replace(link, match[1]+'.'+img_fmt) |
310 #http://localhost:8000/text/text_key_1/export/pdf/1/all/1 |
327 return content |
|
328 |
311 def text_export(request, key, format, download, whichcomments, withcolor, adminkey=None): |
329 def text_export(request, key, format, download, whichcomments, withcolor, adminkey=None): |
312 text, admin = get_text_and_admin(key, adminkey) |
330 text, admin = get_text_and_admin(key, adminkey) |
313 text_version = text.get_latest_version() |
331 text_version = text.get_latest_version() |
314 |
332 |
315 if format == 'xml': |
333 if format == 'xml': |
338 if format in ('markdown', 'latex', 'epub') : |
356 if format in ('markdown', 'latex', 'epub') : |
339 use_pandoc = True |
357 use_pandoc = True |
340 else: |
358 else: |
341 use_pandoc = (original_format == 'markdown' or original_format == 'rst') |
359 use_pandoc = (original_format == 'markdown' or original_format == 'rst') |
342 |
360 |
343 # correct attach path => real path |
361 # attachments |
344 if format in ('pdf', 'odt', 'doc', 'docx') : |
362 # for html, inline images only when exporting |
345 original_content = from_html_links_to_abs_links(original_content) |
363 if format != 'html' or download_response : |
|
364 # for epub, file paths |
|
365 if format == 'epub': |
|
366 original_content = from_html_links_to_inline_imgs(original_content, False) |
|
367 # for latex, file name |
|
368 elif format == 'latex': |
|
369 original_content = from_html_links_to_inline_imgs(original_content, False, False) |
|
370 # for everything else, inline b64 encoded |
|
371 else: |
|
372 original_content = from_html_links_to_inline_imgs(original_content) |
346 |
373 |
347 if len(comments) == 0 : #want to bypass html conversion in this case |
374 if len(comments) == 0 : #want to bypass html conversion in this case |
348 # Prepends title |
375 # Prepends title |
349 if original_format == 'html': |
376 if original_format == 'html': |
350 original_content = "<h1>%s</h1>%s" %(text_version.title, original_content) |
377 original_content = "<h1>%s</h1>%s" %(text_version.title, original_content) |