def display_diff_html(self): # get the content object's HTML # get all of its changesets greater than this version number # aplly the patches ( do not save over ) # get difference HTML between the reverted object ( if reverted ) # return the content object HTML( original ) # return the difference HTML dmp = diff_match_patch() document_obj = self.content_object #get all changesets greater than self.revision ordered -revision changesets = document_obj.changes.filter(revision__gt = self.at_revision ).order_by('-revision') or None #collect the content diff & convert to patch current_html = None if changesets is not None: for change in changesets: if current_html is None: current_html = document_obj.get_html_content() patch = dmp.patch_fromText(change.content_diff) #apply patches to the current content object current_html = dmp.patch_apply(patch, current_html)[0] else: current_html = document_obj.get_html_content() diffs = dmp.diff_main(current_html, self.content, checklines = False) return dmp.diff_prettyHtml(diffs)
def make_difPatch(new_text, old_text): ''' returns a patch object between two strings as a string for storage in DB ''' _dmp = diff_match_patch() patch = _dmp.patch_make(new_text, old_text) return _dmp.patch_toText(patch)
def get_html_content(self): if self.html_patch is None: return self.content else: _DMP = diff_match_patch() patch = _DMP.patch_fromText(self.html_patch) return _DMP.patch_apply(patch, self.content)[0]
def _merge_code(view, edit, code, formatted): def ss(start, end): return view.substr(sublime.Region(start, end)) dmp = diff_match_patch() diffs = dmp.diff_main(code, formatted) dmp.diff_cleanupEfficiency(diffs) i = 0 dirty = False for k, s in diffs: l = len(s) if k == 0: # match l = len(s) if ss(i, i + l) != s: raise MergeException('mismatch', dirty) i += l else: dirty = True if k > 0: # insert view.insert(edit, i, s) i += l else: # delete if ss(i, i + l) != s: raise MergeException('mismatch', dirty) view.erase(edit, sublime.Region(i, i + l)) return dirty
def save(self): ''' Queued items are only intended to be moderated once, so we want to check to see if the moderate field is not NULL. If it is not NULL, we know it has been moderated already ''' # their should never be an 'new' instance of a queueditem # but better safe than sorry if self.instance.id is None: return False else: import pdb pdb.set_trace() # we have an unmoderated queue objec mod_decision = self.cleaned_data['moderate'] if mod_decision == 'approval': from diff_match_patch.diff_match_patch import diff_match_patch _dmp = diff_match_patch() current_doc = self.instance.content_object old_html = current_doc.get_html_content() current_doc.content = self.instance.content # save the HTML to the document current_doc.save() #make revision current_doc.make_new_revision(old_html, current_doc.name, self.instance.comment, self.instance.editor) # save plain text for search current_doc.make_indexable() # save the queue item item = super(QueueModerationForm, self).save() if logger is not None: from jaxerlog.models import LOG_ADDITION, LOG_CHANGE if self.instance.action =="new": logger.objects.log_action( user_id = item.editor.pk, content_type_id = item.content_object.get_ct_id(), object_id = item.content_object.pk, action_flag = LOG_ADDITION, change_message = "added a new document" ) else: logger.objects.log_action( user_id = item.content_object.editor.pk, content_type_id = item.content_object.get_ct_id(), object_id = item.content_object.pk, action_flag = LOG_CHANGE, change_message = 'edited the document' ) return item else: # if the decision was a denial # just save the changes to the queue object return super(QueueModerationForm, self).save()
def apply_patch_from_que(self, ptext, description, editor): #will be betting a patch_text object from the queue #convert to actual patch #apply patch to object #save object DMP = diff_match_patch() patch = DMP.patch_fromText(ptext) self.description = DMP.patch_apply(patch, self.content[0])
def _diff_text(self, t1, t2): diff = diff_match_patch() diff.Diff_Timeout = 1 a = diff.diff_linesToChars(t1, t2) diffs = diff.diff_main(a[0], a[1], False) diff.diff_charsToLines(diffs, a[2]) diff.diff_cleanupEfficiency(diffs) patch = diff.patch_make(diffs) return unquote(diff.patch_toText(patch).replace('%0A', ''))
def __get_raw_text_from_patches(self): # apply patches from rev 0 through current rev # until the raw text has been reconstructed dmp = diff_match_patch() raw_text = '' for rev in self.page.revs[0:self.rev_num+1]: patches = dmp.patch_fromText(rev.patch_text) raw_text = dmp.patch_apply(patches, raw_text)[0] return raw_text
def __set_patch_text_from_raw_text(self, raw_text): # diff raw text against the raw text of prior revision prior_raw_text = '' if self.rev_num > 0: prior_rev = self.page.revs[self.rev_num-1] prior_raw_text = prior_rev._Revision__get_raw_text_from_patches() dmp = diff_match_patch() patches = dmp.patch_make(prior_raw_text, raw_text) self.patch_text = dmp.patch_toText(patches)
def diff(self, documentcontent_id): earlier_content = DocumentContent.objects.get(id=documentcontent_id) # Basic diff_match_patch thing dmp = diff_match_patch() dmp.Diff_Timeout = 0 # dmp.Diff_EditCost = 10 # Higher value means more semantic cleanup. 4 is default which works for us right now. d = dmp.diff_main(earlier_content.text, self.text) # Calculate the diff dmp.diff_cleanupSemantic(d) result = dmp.diff_prettyHtml(d).replace('¶', '') result = re.sub( r'\r<br>', r'<br>', result ) # Because we're using <pre></pre> in the template, so the HTML creates two newlines. return result
def make_indexable(self): ''' Incoming content is assumed to be HTML We want to index plain text, not html If the current content is already plain text, we don't do anything else convert the html to plain text make a patch to convert the text back to html save the plain text, save the patch as text string save the object ''' from django.template.defaultfilters import striptags DMP = diff_match_patch() html = self.content plain_text = striptags(html) if not self.content == plain_text: patch = DMP.patch_make(plain_text, html) self.content = plain_text self.html_patch = DMP.patch_toText(patch)
''' Wiki Models ''' from django.db import models from django.contrib.admin.models import User from diff_match_patch import diff_match_patch from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic #from django.contrib import contenttypes from datetime import datetime from django.db.models import Manager from django_extensions.db.fields import AutoSlugField # Create your models here. #diff_match_patch instance DMP = diff_match_patch.diff_match_patch() class ChangeSet(models.Model): ''' The ChangeSet can be attached to any object and ment to be connected to a Class which subclases EditableItem as a generic inline relation import this in to your application's admin.py create a generic inline and attach it to anything which subclasses EditableItem as a generic inline and you have a "wiki-able" item ''' content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') editor = models.ForeignKey(User, limit_choices_to={'is_staff':True}) revision = models.PositiveIntegerField()
def cache_content(page: PageModel): content = '' for diffs in page.content: patches = diff_maker.patch_make(content, diffs.data) content, _ = diff_maker.patch_apply(patches, content) return content def validate_cache(key, content, cache_version, page_version=None): if not page_version: return False return page_version <= cache_version content_cache = CacheDict(cache_content, validate_cache) diff_maker = diff_match_patch() @tag('content') @params(category_id='The id of the category', page_id='The id of the page') class PageContent(Resource): @marshal_with({ 'type': 'string', 'format': 'markdown' }, code=200, content_type='text/markdown', apply=False) def get(self, category_id, page_id): """ ## Get the cached content of the page located at (category_id, page_id)
self.__protocol.create(self.__login, self.__key) self.__algorithm, self.__header_hash, self.__block_chain = self.__protocol.open(self.__login, self.__key) >>>>>>> Stashed changes if len(self.__block_chain) > 0: _, self.__prev_hash = self.__block_chain[-1] # Инициализация модуля diff_match_patch. <<<<<<< Updated upstream dmp = dmp_module() for record, _ in self.__block_chain: patch = self.__decoding_str(record) ======= dmp = dmp_module.diff_match_patch() for record, chain in self.__block_chain: patch = base64.b64decode(record).decode("utf-8") >>>>>>> Stashed changes self.__patches.append(dmp.patch_fromText(patch)) else: self.__prev_hash = self.__header_hash self.__init = True def valid(self): """ Проверка Grail на истинность.
RE_SYMBOL = re.compile(r"^([!-/:-@[-`{-~])+$") RE_COMMENT = re.compile(r"^(\s|\t)*(#|//)+(?!(if|else|include)).*($\n?)?") RE_PLUS = re.compile(r"\+") RE_MINUS = re.compile(r"-") """ File Extentions """ IMAGE = ["png", "gif", "jpg", "jpeg", "vg", "svgx"] BINARY_DOC = ["doc", "docx"] """ Get start time """ START = time.time() print time.strftime("START: %a, %d %b %Y %H:%M:%S", time.localtime()) """ Diff object from diff-match-patch """ DIFF_OBJ = diff_match_patch.diff_match_patch() FILE_LIST = [(pull_no, patch_no) for pull_no in range(MIN_PULL_NO, MAX_PULL_NO + 1) for patch_no in range(1, 10) if os.path.isfile(DIFF_DIR_PATH + str(pull_no) + "_" + str(patch_no) + EXTENSION)] FILE_NUM = len(FILE_LIST) with open(OUTPUT_PATH, "w") as output_diff: # Setting csv writer DIFF_WRITER = csv.writer(output_diff, lineterminator="\n") # Output column Name DIFF_WRITER.writerow( ("PullNo", "PatchNo", "Date", "CHANGED_CONTENTS", "SpaceOrTab", "NewLine", "UpperOrLower", "Symbol", "Renamed", "CHANGED_LINES", "If",
import re from diff_match_patch.diff_match_patch import diff_match_patch from .utils import get_loggers, freeze, DISALLOWED_CHARACTERS __all__ = [ 'get_diff_pattern', 'migrate_command', 'migrate_install_commands', 'get_matched_parts', 'name_by_common_prefix', 'group_keys_by_vv', 'get_common_values', ] logger, info, debug, warn, error = get_loggers(__name__) diff = diff_match_patch().diff_main def update_diff_pattern(pattern, fields, lhs, rhs): if fields and len(pattern[-1]) <= 3: delimiter = pattern.pop(-1) field = fields.pop(-1) fields.append(( field[0] + delimiter + lhs, field[1] + delimiter + rhs, )) else: pattern.append('%%(%d)s' % len(fields)) fields.append((lhs, rhs))