def make_udiff(a, b): import urllib dm = diff_match_patch() diffs = dm.diff_main(a,b) patches = dm.patch_make(diffs) out = [] for patch in patches: if patch.length1 == 0: coords1 = str(patch.start1) + ",0" elif patch.length1 == 1: coords1 = str(patch.start1 + 1) else: coords1 = str(patch.start1 + 1) + "," + str(patch.length1) if patch.length2 == 0: coords2 = str(patch.start2) + ",0" elif patch.length2 == 1: coords2 = str(patch.start2 + 1) else: coords2 = str(patch.start2 + 1) + "," + str(patch.length2) text = ["@@ -", coords1, " +", coords2, " @@\n"] # Escape the body of the patch with %xx notation. for (op, data) in patch.diffs: data = data.encode("utf-8") txt = urllib.quote(data, "\"!~*'();/?:@&=+$,# ") #txt = (data.replace("&", "&").replace("<", "<").replace(">", ">").replace("\n", "<br>")) if op == diff_match_patch.DIFF_INSERT: text.append("<ins class=\"diff_add\">+%s</ins>" % txt) elif op == diff_match_patch.DIFF_DELETE: text.append("<del class=\"diff_sub\">-%s</del>" % txt) elif op == diff_match_patch.DIFF_EQUAL: text.append("<span>%s</span>" % txt) # High ascii will raise UnicodeDecodeError. Use Unicode instead. out.append("".join(text)) return "".join(out)
def make_diff(a, b): dm = diff_match_patch() diffs = dm.diff_main(a,b) dm.diff_cleanupSemantic(diffs) out = [] for (op, data) in diffs: text = (data.replace("&", "&").replace("<", "<").replace(">", ">").replace("\n", "<br>")) if op == diff_match_patch.DIFF_INSERT: out.append("<ins class=\"diff_add\">%s</ins>" % text) elif op == diff_match_patch.DIFF_DELETE: out.append("<del class=\"diff_sub\">%s</del>" % text) elif op == diff_match_patch.DIFF_EQUAL: out.append("<span>%s</span>" % text) return "".join(out)