def content_export2(request, content, title, content_format, format, use_pandoc, download_response): # TODO : formats must be imported from converters # import pdb;pdb.set_trace() if format == 'raw' : export_content = content elif content_format == 'html' and format == 'html': export_content = HTML_HEADER % content elif content_format == 'markdown' and format == 'markdown': export_content = content else: if use_pandoc : # markdown2pdf is buggy => convert to HTML and use abiword to export in PDF if format in ('pdf', 'odt', 'docx', 'doc') and USE_ABI: html_content = pandoc_convert(content, content_format, 'html', full=True) from cm.converters.abi_converters import AbiFileConverter converter = AbiFileConverter() fix_content = converter.add_html_header(html_content) export_content = converter.convert_from_html(fix_content, format) else: export_content = pandoc_convert(content, content_format, format, full=True) else : fix_content = content if content_format == 'html': if USE_ABI: from cm.converters.abi_converters import AbiFileConverter converter = AbiFileConverter() fix_content = converter.add_html_header(content) else: from cm.converters.oo_converters import combine_css_body fix_content = combine_css_body(content, '') if USE_ABI: from cm.converters.abi_converters import AbiFileConverter converter = AbiFileConverter() try: export_content = converter.convert_from_html(fix_content, format) except: if content_format == 'html': from cm.converters.oo_converters import combine_css_body fix_content = combine_css_body(content, '') from cm.converters.oo_converters import convert_html as oo_convert export_content = oo_convert(fix_content, format) else: from cm.converters.oo_converters import convert_html as oo_convert export_content = oo_convert(fix_content, format) export_infos = EXPORT2_INFOS[format] if download_response: return _response_download(export_content, title, export_infos['mimetype'], export_infos['extension']) ; else: return _response_write(export_content)
def test_rst(self): rst = unicode(file('src/cm/tests/data/text.rst').read()) # convert in all formats for to_format in OUTPUT_FORMATS: if to_format == 'pdf': # Skip this because it involves a whole LaTeX install continue res = pandoc_convert(rst, 'rst', to_format)
def test_rst(self): rst = unicode(file('src/cm/tests/data/text.rst').read()) # convert in all formats for to_format in OUTPUT_FORMATS: if to_format == 'pdf': # Skip this because it involves a whole LaTeX install continue res = pandoc_convert(rst,'rst',to_format)
def add_comment(request, key, version_key): # if edit_comment_id : # # if self.request.user.is_anonymous() : # accessing via an admin url ? # and comment.user == self.request.user user = None if request.user.is_anonymous() else request.user name, email, title, content, tags, category, reply_to_id, format, start_wrapper, end_wrapper, start_offset, end_offset = read_comment_args(request) errors = {} errors = validate_comment_args(name, email, title, content, tags) if start_wrapper == "" : errors['selection_place'] = selection_place_error_msg #TODO validate pandoc conversion content_html = pandoc_convert(content, format, "html", full=False) ret = {} if errors != {} : ret['errors'] = errors else : # INSERT # TODO check version still exist ... reply_to = None if reply_to_id : reply_to = Comment.objects.get(id=reply_to_id) text = Text.objects.get(key=key) text_version = TextVersion.objects.get(key=version_key) comment_state = 'approved' if text_version.mod_posteriori else 'pending' comment = Comment.objects.create(state=comment_state, text_version=text_version, user=user, name=name, email=email, title=title, content=content, content_html=content_html, tags = tags, category = category, start_wrapper = start_wrapper, end_wrapper = end_wrapper, start_offset = start_offset, end_offset = end_offset, reply_to=reply_to) ask_for_notification = True if user : workspace_notify_count = Notification.objects.filter(text=None,type='workspace',user=user, active=True).count() text_notify_count = Notification.objects.filter(text=text,type='text',user=user, active=True).count() if workspace_notify_count > 0 or text_notify_count > 0 : ask_for_notification = False if ask_for_notification : ask_for_notification = ( None == Notification.objects.get_notifications(text=None, type='own', email_or_user=(user if user else email))) ret['ask_for_notification'] = ask_for_notification ret['email'] = '' if user else email if text_version.mod_posteriori or has_perm(request, 'can_view_unapproved_comment', text=text) or has_perm(request, 'can_view_comment_own', text=text) : ret['comment'] = comment ret['msg'] = _(u"comment saved") else : ret['msg'] = _(u"comment saved, it is being held for moderation") if AUTO_CONTRIB_REGISTER: Notification.objects.set_notification(text=text, type='own', active=True, email_or_user=user or email) register_activity(request, "comment_created", text, comment) cache.clear() return ret
def edit_comment(request, key, comment_key): state = request.POST.get('state', None) change_state = state and state in comment_states errors = {} if not change_state: # moderation action change_scope = request.POST.get('change_scope', None) name, email, title, content, tags, url, reply_to_id, format, start_wrapper, end_wrapper, start_offset, end_offset = read_comment_args( request) errors = validate_comment_args(name, email, title, content, tags, url) if (change_scope) and start_wrapper == "": errors['selection_place'] = selection_place_error_msg content_html = pandoc_convert(content, format, "html", full=False) ret = {} if errors != {}: ret['errors'] = errors else: # INSERT # TODO check version is latest (if boolean #comment = Comment.objects.get(id=edit_comment_id) comment = Comment.objects.get(key=comment_key) if change_state: # moderation action comment.state = state else: comment.name = name comment.email = email comment.title = title comment.content = content comment.content_html = content_html comment.tags = tags comment.url = url if change_scope: comment.start_wrapper = start_wrapper comment.start_offset = start_offset comment.end_wrapper = end_wrapper comment.end_offset = end_offset comment.save() ret['comment'] = comment ret['msg'] = _(u'comment saved') return ret
def edit_comment(request, key, comment_key): state = request.POST.get('state', None) change_state = state and state in comment_states errors = {} if not change_state : # moderation action change_scope = request.POST.get('change_scope', None) name, email, title, content, tags, url, reply_to_id, format, start_wrapper, end_wrapper, start_offset, end_offset = read_comment_args(request) errors = validate_comment_args(name, email, title, content, tags, url) if (change_scope) and start_wrapper=="" : errors['selection_place'] = selection_place_error_msg content_html = pandoc_convert(content, format, "html", full=False) ret = {} if errors != {} : ret['errors'] = errors else : # INSERT # TODO check version is latest (if boolean #comment = Comment.objects.get(id=edit_comment_id) comment = Comment.objects.get(key=comment_key) if change_state : # moderation action comment.state = state else : comment.name = name comment.email = email comment.title = title comment.content = content comment.content_html = content_html comment.tags = tags comment.url = url if change_scope : comment.start_wrapper = start_wrapper comment.start_offset = start_offset comment.end_wrapper = end_wrapper comment.end_offset = end_offset comment.save() ret['comment'] = comment ret['msg'] = _(u'comment saved') return ret
def content_export2(request, content, title, content_format, format, use_pandoc, download_response): # TODO : formats must be imported from converters # import pdb;pdb.set_trace() if format == 'raw': export_content = content elif content_format == 'html' and format == 'html': export_content = HTML_HEADER % content elif content_format == 'markdown' and format == 'markdown': export_content = content else: if use_pandoc: # markdown2pdf is buggy => convert to HTML and use abiword to export in PDF if format in ('pdf', 'odt', 'docx', 'doc') and USE_ABI: html_content = pandoc_convert(content, content_format, 'html', full=True) from cm.converters.abi_converters import AbiFileConverter converter = AbiFileConverter() fix_content = converter.add_html_header(html_content) export_content = converter.convert_from_html( fix_content, format) else: export_content = pandoc_convert(content, content_format, format, full=True) else: fix_content = content if content_format == 'html': if USE_ABI: from cm.converters.abi_converters import AbiFileConverter converter = AbiFileConverter() fix_content = converter.add_html_header(content) else: from cm.converters.oo_converters import combine_css_body fix_content = combine_css_body(content, '') if USE_ABI: from cm.converters.abi_converters import AbiFileConverter converter = AbiFileConverter() try: export_content = converter.convert_from_html( fix_content, format) except: if content_format == 'html': from cm.converters.oo_converters import combine_css_body fix_content = combine_css_body(content, '') from cm.converters.oo_converters import convert_html as oo_convert export_content = oo_convert(fix_content, format) else: from cm.converters.oo_converters import convert_html as oo_convert export_content = oo_convert(fix_content, format) export_infos = EXPORT2_INFOS[format] if download_response: return _response_download(export_content, title, export_infos['mimetype'], export_infos['extension']) else: return _response_write(export_content)
def get_content(self, format='html'): return pandoc_convert(self.content, self.format, format)
def test_rst(self): rst = unicode(file('src/cm/tests/data/text.rst').read()) # convert in all formats for to_format in OUTPUT_FORMATS: res = pandoc_convert(rst,'rst',to_format)
def compute_new_comment_positions(old_content, old_format, new_content, new_format, commentList): # cf. TextVersion.get_content previousVersionContent = pandoc_convert(old_content, old_format, 'html') newVersionContent = pandoc_convert(new_content, new_format, 'html') _, previous_char_list, span_starts_previous = spannify( previousVersionContent, False) _, new_char_list, span_starts_new = spannify(newVersionContent, False) sm = SequenceMatcher(None, previous_char_list, new_char_list) opcodes = sm.get_opcodes() to_remove_comments_ids = set() # limit to real comments (not replies) and those that have scope commentList = [ c for c in commentList if not c.is_reply() and not c.is_scope_removed() ] for comment in commentList: try: comment.initial_start_offset = span_starts_previous[ comment.start_wrapper] + comment.start_offset comment.initial_end_offset = span_starts_previous[ comment.end_wrapper] + comment.end_offset except KeyError: logging.error( 'Key error (wrapper out of bounds of span_starts_previous)') continue comment.computed_start_offset = comment.initial_start_offset comment.computed_end_offset = comment.initial_end_offset # comment.computed_start_wrapper = None # comment.computed_end_wrapper = None comment.valid = True for tag, i1, i2, j1, j2 in opcodes: #print tag, i1, i2, j1, j2 for i in xrange(len(commentList)): if tag != 'equal': comment = commentList[i] if not comment.valid: continue if comment.initial_start_offset >= i2: # if offset delta = ((j2 - j1) - (i2 - i1)) comment.computed_start_offset += delta comment.computed_end_offset += delta elif comment.initial_end_offset > i1: comment.valid = False # id, initial_start, initial_end, computed_start, computed_end, valid = self.computationResults[i] for cc in commentList: if cc.valid: for id in xrange(len(span_starts_new.keys())): start = span_starts_new.get(id, 0) end = span_starts_new.get(id + 1, sys.maxint) # adjust start if cc.computed_start_offset >= start and cc.computed_start_offset < end: cc.start_wrapper = id cc.start_offset = cc.computed_start_offset - start # adjust end if cc.computed_end_offset >= start and cc.computed_end_offset < end: cc.end_wrapper = id cc.end_offset = cc.computed_end_offset - start # returns to_modify, to_remove return [c for c in commentList if c.valid], \ [c for c in commentList if not c.valid]
def compute_new_comment_positions(old_content, old_format, new_content, new_format, commentList): # cf. TextVersion.get_content previousVersionContent = pandoc_convert(old_content, old_format, 'html') newVersionContent = pandoc_convert(new_content, new_format, 'html') _, previous_char_list, span_starts_previous = spannify(previousVersionContent, False) _, new_char_list, span_starts_new = spannify(newVersionContent, False) sm = SequenceMatcher(None, previous_char_list, new_char_list) opcodes = sm.get_opcodes() to_remove_comments_ids = set() # limit to real comments (not replies) and those that have scope commentList = [c for c in commentList if not c.is_reply() and not c.is_scope_removed()] for comment in commentList: try: comment.initial_start_offset = span_starts_previous[comment.start_wrapper] + comment.start_offset comment.initial_end_offset = span_starts_previous[comment.end_wrapper] + comment.end_offset except KeyError: logging.error('Key error (wrapper out of bounds of span_starts_previous)') continue comment.computed_start_offset = comment.initial_start_offset comment.computed_end_offset = comment.initial_end_offset # comment.computed_start_wrapper = None # comment.computed_end_wrapper = None comment.valid = True for tag, i1, i2, j1, j2 in opcodes: #print tag, i1, i2, j1, j2 for i in xrange(len(commentList)) : if tag != 'equal' : comment = commentList[i] if not comment.valid: continue if comment.initial_start_offset >= i2 : # if offset delta = ((j2 - j1) - (i2 - i1)) comment.computed_start_offset += delta comment.computed_end_offset += delta elif comment.initial_end_offset > i1: comment.valid = False # id, initial_start, initial_end, computed_start, computed_end, valid = self.computationResults[i] for cc in commentList: if cc.valid: for id in xrange(len(span_starts_new.keys())): start = span_starts_new.get(id, 0) end = span_starts_new.get(id+1, sys.maxint) # adjust start if cc.computed_start_offset >= start and cc.computed_start_offset < end: cc.start_wrapper = id cc.start_offset = cc.computed_start_offset - start # adjust end if cc.computed_end_offset >= start and cc.computed_end_offset < end: cc.end_wrapper = id cc.end_offset = cc.computed_end_offset - start # returns to_modify, to_remove return [c for c in commentList if c.valid], \ [c for c in commentList if not c.valid]