diff --git a/README.md b/README.md index b37b0df..e088aca 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,12 @@ A python script that takes two files and compares the differences between them (side-by-side) in an HTML format. -###Usage +### Installation +``` +pip install -r requirements.txt +``` + +### Usage ``` diff2HtmlCompare.py [-h] [-v] file1 file2 diff --git a/diff2HtmlCompare.py b/diff2HtmlCompare.py index c9d4bf8..d11c9a7 100644 --- a/diff2HtmlCompare.py +++ b/diff2HtmlCompare.py @@ -1,17 +1,17 @@ # MIT License -# +# # Copyright (c) 2016 Alex Goodman -# +# # Permission is hereby granted, free of charge, to any person obtaining a copy of # this software and associated documentation files (the "Software"), to deal in # the Software without restriction, including without limitation the rights to # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies # of the Software, and to permit persons to whom the Software is furnished to do # so, subject to the following conditions: -# +# # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. -# +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -31,300 +31,379 @@ from pygments.lexer import RegexLexer from pygments.formatters import HtmlFormatter from pygments.token import * +HTML_TEMPLATE = """ + + +
+ + +'
-
- for idx, ((left_no, left_line),(right_no, right_line),change) in enumerate(self.diffs):
- #print idx, ((left_no, left_line),(right_no, right_line),change)
- try:
+ def getDiffLineNos(self):
+ retlinenos = []
+ for idx, ((left_no, left_line), (right_no, right_line), change) in enumerate(self.diffs):
+ no = None
if self.isLeft:
- if change:
- if isinstance(left_no, int) and isinstance(right_no, int) and left_no <= len(source):
- i,t = source[left_no-1]
- t = '' + t + ""
- elif isinstance(left_no, int) and not isinstance(right_no, int) and left_no <= len(source):
- i,t = source[left_no-1]
- t = '' + t + ""
- elif not isinstance(left_no, int) and isinstance(right_no, int):
- i,t = 1, left_line
- t = '' + t + ""
- else:
- raise
- else:
- if left_no <= len(source):
- i,t = source[left_no-1]
- else:
- i = 1
- t = left_line
+ if change:
+ if isinstance(left_no, int) and isinstance(right_no, int):
+ no = '' + \
+ str(left_no) + ""
+ elif isinstance(left_no, int) and not isinstance(right_no, int):
+ no = '' + \
+ str(left_no) + ""
+ elif not isinstance(left_no, int) and isinstance(right_no, int):
+ no = ' '
+ else:
+ no = '' + str(left_no) + ""
else:
- if change:
- if isinstance(left_no, int) and isinstance(right_no, int) and right_no <= len(source):
- i,t = source[right_no-1]
- t = '' + t + ""
- elif isinstance(left_no, int) and not isinstance(right_no, int):
- i,t = 1, right_line
- t = '' + t + ""
- elif not isinstance(left_no, int) and isinstance(right_no, int) and right_no <= len(source):
- i,t = source[right_no-1]
- t = '' + t + ""
- else:
- raise
- else:
- if right_no <= len(source):
- i,t = source[right_no-1]
- else:
- i = 1
- t = right_line
- yield i, t
- except:
- #print "WARNING! failed to enumerate diffs fully!"
- pass # this is expected sometimes
- yield 0, '\n'
+ if change:
+ if isinstance(left_no, int) and isinstance(right_no, int):
+ no = '' + \
+ str(right_no) + ""
+ elif isinstance(left_no, int) and not isinstance(right_no, int):
+ no = ' '
+ elif not isinstance(left_no, int) and isinstance(right_no, int):
+ no = '' + \
+ str(right_no) + ""
+ else:
+ no = '' + str(right_no) + ""
+ retlinenos.append(no)
- def _wrap_tablelinenos(self, inner):
- dummyoutfile = io.StringIO()
- lncount = 0
- for t, line in inner:
- if t:
- lncount += 1
- dummyoutfile.write(line)
+ return retlinenos
- fl = self.linenostart
- mw = len(str(lncount + fl - 1))
- sp = self.linenospecial
- st = self.linenostep
- la = self.lineanchors
- aln = self.anchorlinenos
- nocls = self.noclasses
+ def _wrap_code(self, source):
- lines = []
- for i in self.getDiffLineNos():
- lines.append('%s' % (i,))
+ source = list(source)
+ yield 0, '' - ls = ''.join(lines) + for idx, ((left_no, left_line), (right_no, right_line), change) in enumerate(self.diffs): + # print idx, ((left_no, left_line),(right_no, right_line),change) + try: + if self.isLeft: + if change: + if isinstance(left_no, int) and isinstance(right_no, int) and left_no <= len(source): + i, t = source[left_no - 1] + t = '' + t + "" + elif isinstance(left_no, int) and not isinstance(right_no, int) and left_no <= len(source): + i, t = source[left_no - 1] + t = '' + t + "" + elif not isinstance(left_no, int) and isinstance(right_no, int): + i, t = 1, left_line + t = '' + t + "" + else: + raise + else: + if left_no <= len(source): + i, t = source[left_no - 1] + else: + i = 1 + t = left_line + else: + if change: + if isinstance(left_no, int) and isinstance(right_no, int) and right_no <= len(source): + i, t = source[right_no - 1] + t = '' + t + "" + elif isinstance(left_no, int) and not isinstance(right_no, int): + i, t = 1, right_line + t = '' + t + "" + elif not isinstance(left_no, int) and isinstance(right_no, int) and right_no <= len(source): + i, t = source[right_no - 1] + t = '' + t + "" + else: + raise + else: + if right_no <= len(source): + i, t = source[right_no - 1] + else: + i = 1 + t = right_line + yield i, t + except: + # print "WARNING! failed to enumerate diffs fully!" + pass # this is expected sometimes + yield 0, '\n' - # in case you wonder about the seemingly redundant
'
- ' ' + - ls + ' | ')
- else:
- yield 0, ('
here: since the
+ # content in the other cell also is wrapped in a div, some browsers in
+ # some configurations seem to mess up the formatting...
+ if nocls:
+ yield 0, ('
|