This commit is contained in:
wagoodman 2016-12-23 18:06:29 -05:00
parent 64d513dde6
commit 29d732b85f
2 changed files with 16 additions and 22 deletions

View file

@ -1,6 +1,6 @@
# diff2HtmlCompare
A python script that takes two files and compares the differences between them (side-by-side) in an HTML format.
A python script that takes two files and compares the differences between them (side-by-side) in an HTML format. Supports both python2 and python3.
### Installation
```
@ -9,7 +9,7 @@ pip install -r requirements.txt
### Usage
```
diff2HtmlCompare.py [-h] [-v] file1 file2
diff2HtmlCompare.py [-h] [-s] [-v] file1 file2
positional arguments:
file1 file to compare ("before" file).
@ -17,6 +17,7 @@ positional arguments:
optional arguments:
-h, --help show this help message and exit
-s, --show show html in a browser.
-v show verbose output.
```
### Example Output

View file

@ -20,12 +20,13 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import io
import os
import sys
import difflib
import argparse
import io
import pygments
import webbrowser
from pygments.lexers import guess_lexer_for_filename
from pygments.lexer import RegexLexer
from pygments.formatters import HtmlFormatter
@ -178,7 +179,6 @@ class DiffHtmlFormatter(HtmlFormatter):
return retlinenos
def _wrap_code(self, source):
source = list(source)
yield 0, '<pre>'
@ -281,7 +281,6 @@ class CodeDiff(object):
jqueryJsFile = "./deps/jquery.min.js"
def __init__(self, fromfile, tofile, fromtxt=None, totxt=None, name=None):
self.filename = name
self.fromfile = fromfile
if fromtxt == None:
@ -310,7 +309,6 @@ class CodeDiff(object):
self.rightcode = "".join(self.tolines)
def getDiffDetails(self, fromdesc='', todesc='', context=False, numlines=5, tabSize=8):
# change tabs to spaces before it gets more difficult after we insert
# markkup
def expand_tabs(line):
@ -382,26 +380,21 @@ class CodeDiff(object):
self.htmlContents = HTML_TEMPLATE % answers
def write(self, path="index.html"):
def write(self, path):
fh = open(path, 'w')
fh.write(self.htmlContents)
fh.close()
def main(fromfile, tofile, verbose=False):
codeDiff = CodeDiff(fromfile, tofile, name=tofile)
def main(file1, file2, outputpath, verbose=False):
codeDiff = CodeDiff(file1, file2, name=file2)
codeDiff.format(verbose)
codeDiff.write()
codeDiff.write(outputpath)
def show():
import os
import webbrowser
path = os.path.abspath('index.html')
def show(outputpath):
path = os.path.abspath(outputpath)
webbrowser.open('file://' + path)
if __name__ == "__main__":
description = """Given two source files this application\
creates an html page which highlights the differences between the two. """
@ -410,12 +403,12 @@ creates an html page which highlights the differences between the two. """
parser.add_argument('-s', '--show', action='store_true',
help='show html in a browser.')
parser.add_argument('-v', action='store_true', help='show verbose output.')
parser.add_argument(
'file1', help='source file to compare ("before" file).')
parser.add_argument('file1', help='source file to compare ("before" file).')
parser.add_argument('file2', help='source file to compare ("after" file).')
args = parser.parse_args()
main(args.file1, args.file2, args.v)
outputpath = "index.html"
main(args.file1, args.file2, outputpath, verbose=args.v)
if args.show:
show()
show(outputpath)