Skip to content
Snippets Groups Projects
Commit a45319b1 authored by Todd Dembrey's avatar Todd Dembrey
Browse files

make the diff nicer

Tidy up the output to have less divs
parent ecefebef
No related branches found
No related tags found
No related merge requests found
...@@ -285,18 +285,37 @@ class RevisionCompareView(TemplateView): ...@@ -285,18 +285,37 @@ class RevisionCompareView(TemplateView):
diff = SequenceMatcher(None, answer_a, answer_b) diff = SequenceMatcher(None, answer_a, answer_b)
output = [] output = []
added = ''
deleted = ''
for opcode, a0, a1, b0, b1 in diff.get_opcodes(): for opcode, a0, a1, b0, b1 in diff.get_opcodes():
if opcode == 'equal': if opcode == 'equal':
output.append(diff.a[a0:a1]) if a1 - a0 > 2 or not (added or deleted):
if added:
output.append(self.added(added))
added = ''
if deleted:
output.append(self.deleted(deleted))
deleted = ''
output.append(diff.a[a0:a1])
else:
added += diff.a[a0:a1]
deleted += diff.a[a0:a1]
elif opcode == 'insert': elif opcode == 'insert':
output.append(self.added(diff.b[b0:b1])) added += diff.b[b0:b1]
elif opcode == 'delete': elif opcode == 'delete':
output.append(self.deleted(diff.a[a0:a1])) deleted += diff.a[a0:a1]
elif opcode == 'replace': elif opcode == 'replace':
output.append(self.deleted(diff.a[a0:a1])) deleted += diff.a[a0:a1]
output.append(self.added(diff.b[b0:b1])) added += diff.b[b0:b1]
else:
raise RuntimeError("unexpected opcode") if added == deleted:
output.append(added)
else:
if added:
output.append(self.deleted(deleted))
if deleted:
output.append(self.added(added))
return mark_safe(''.join(output)) return mark_safe(''.join(output))
def compare(self, from_data, to_data): def compare(self, from_data, to_data):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment