Created
July 9, 2015 21:23
-
-
Save dufferzafar/9979b0bd71fe5fc6a6a2 to your computer and use it in GitHub Desktop.
Generates a compact word diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import difflib | |
tags = {'+': ('<ins>', '</ins>'), '-': ('<del>', '</del>'), ' ': (' ', '')} | |
left = "An edit to create a new revision that the diffs thing. I am now adding some more text so that I can test the diffs." | |
right = "The beginning has now been changed. An edit to create a new revision that the diffs thing. And make some changes here and there. Let's add a totally new paragraph.\n\nOkay, enough. This is entirely new paragraph. We really need to build a cool diff thingy." | |
# Generates a compact word diff | |
prev_start, prev_end = None, '' | |
prev_item = '' | |
diff = [] | |
differ = difflib.ndiff(left.split(), right.split()) | |
for item in differ: | |
tag = tags.get(item[0]) | |
if not tag: continue | |
if prev_start == tag[0]: | |
prev_item += item[1:] | |
else: | |
diff.append(prev_item + prev_end) | |
prev_start, prev_end = tag | |
prev_item = prev_start + item[1:] | |
diff.append(prev_item + prev_end) | |
return " ".join(diff) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Earlier I was using this. Yuck!