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