Back to index

plone3  3.1.7
TextDiff.py
Go to the documentation of this file.
00001 import difflib
00002 from Globals import InitializeClass
00003 from Products.CMFDiffTool.FieldDiff import FieldDiff
00004 
00005 class TextDiff(FieldDiff):
00006     """Text difference"""
00007 
00008     meta_type = "Lines Diff"
00009 
00010     def _parseField(self, value):
00011         """Parse a field value in preparation for diffing"""
00012         # Split the text into a list for diffs
00013         return value.split('\n')
00014 
00015     def unified_diff(self):
00016         """Return a unified diff"""
00017         a = [str(i) for i in self._parseField(self.oldValue)]
00018         b = [str(i) for i in self._parseField(self.newValue)]
00019         return '\n'.join(difflib.unified_diff(a, b, self.id1, self.id2))
00020 
00021     def html_diff(self, context=True, wrapcolumn=40):
00022         """Return an HTML table showing differences"""
00023         a = [str(i) for i in self._parseField(self.oldValue)]
00024         b = [str(i) for i in self._parseField(self.newValue)]
00025         vis_diff = difflib.HtmlDiff(wrapcolumn=wrapcolumn)
00026         return vis_diff.make_table(a, b, self.id1, self.id2, context=context)
00027 
00028 InitializeClass(TextDiff)