fixed bytes to string issue and removed template file

This commit is contained in:
wagoodman 2016-12-23 17:42:07 -05:00
parent 4949162dc8
commit d6b00ca790
3 changed files with 335 additions and 328 deletions

View file

@ -2,6 +2,11 @@
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.
### Installation
```
pip install -r requirements.txt
```
### Usage ### Usage
``` ```
diff2HtmlCompare.py [-h] [-v] file1 file2 diff2HtmlCompare.py [-h] [-v] file1 file2

View file

@ -31,6 +31,86 @@ from pygments.lexer import RegexLexer
from pygments.formatters import HtmlFormatter from pygments.formatters import HtmlFormatter
from pygments.token import * from pygments.token import *
HTML_TEMPLATE = """
<!DOCTYPE html>
<html class="no-js">
<head>
<!--
html_title: browser tab title
reset_css: relative path to reset css file
pygments_css: relative path to pygments css file
diff_css: relative path to diff layout css file
page_title: title shown at the top of the page. This should be the filename of the files being diff'd
original_code: full html contents of original file
modified_code: full html contents of modified file
jquery_js: path to jquery.min.js
diff_js: path to diff.js
-->
<meta charset="utf-8">
<title>
%(html_title)s
</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<link rel="stylesheet" href="%(reset_css)s" type="text/css">
<link class="syntaxdef" rel="stylesheet" href="%(pygments_css)s" type="text/css">
<link rel="stylesheet" href="%(diff_css)s" type="text/css">
</head>
<body>
<div class="" id="topbar">
<div id="filetitle">
<!--&#10140;&nbsp;&nbsp;-->%(page_title)s
</div>
<div class="switches">
<div class="switch">
<input id="showoriginal" class="toggle toggle-yes-no menuoption" type="checkbox" checked>
<label for="showoriginal" data-on="&#10004; Original" data-off="Original"></label>
</div>
<div class="switch">
<input id="showmodified" class="toggle toggle-yes-no menuoption" type="checkbox" checked>
<label for="showmodified" data-on="&#10004; Modified" data-off="Modified"></label>
</div>
<div class="switch">
<input id="highlight" class="toggle toggle-yes-no menuoption" type="checkbox" checked>
<label for="highlight" data-on="&#10004; Highlight" data-off="Highlight"></label>
</div>
<div class="switch">
<input id="codeprintmargin" class="toggle toggle-yes-no menuoption" type="checkbox" checked>
<label for="codeprintmargin" data-on="&#10004; Margin" data-off="Margin"></label>
</div>
<div class="switch">
<input id="dosyntaxhighlight" class="toggle toggle-yes-no menuoption" type="checkbox" checked>
<label for="dosyntaxhighlight" data-on="&#10004; Syntax" data-off="Syntax"></label>
</div>
</div>
</div>
<div id="maincontainer" class="">
<div id="leftcode" class="left-inner-shadow codebox divider-outside-bottom">
<div class="codefiletab">
&#10092; Original
</div>
<div class="printmargin">
01234567890123456789012345678901234567890123456789012345678901234567890123456789
</div>
%(original_code)s
</div>
<div id="rightcode" class="left-inner-shadow codebox divider-outside-bottom">
<div class="codefiletab">
&#10093; Modified
</div>
<div class="printmargin">
01234567890123456789012345678901234567890123456789012345678901234567890123456789
</div>
%(modified_code)s
</div>
</div>
<script src="%(jquery_js)s" type="text/javascript"></script>
<script src="%(diff_js)s" type="text/javascript"></script>
</body>
</html>
"""
class DefaultLexer(RegexLexer): class DefaultLexer(RegexLexer):
""" """
@ -71,9 +151,11 @@ class DiffHtmlFormatter(HtmlFormatter):
if self.isLeft: if self.isLeft:
if change: if change:
if isinstance(left_no, int) and isinstance(right_no, int): if isinstance(left_no, int) and isinstance(right_no, int):
no = '<span class="lineno_q lineno_leftchange">' + str(left_no) + "</span>" no = '<span class="lineno_q lineno_leftchange">' + \
str(left_no) + "</span>"
elif isinstance(left_no, int) and not isinstance(right_no, int): elif isinstance(left_no, int) and not isinstance(right_no, int):
no = '<span class="lineno_q lineno_leftdel">' + str(left_no) + "</span>" no = '<span class="lineno_q lineno_leftdel">' + \
str(left_no) + "</span>"
elif not isinstance(left_no, int) and isinstance(right_no, int): elif not isinstance(left_no, int) and isinstance(right_no, int):
no = '<span class="lineno_q lineno_leftadd"> </span>' no = '<span class="lineno_q lineno_leftadd"> </span>'
else: else:
@ -81,11 +163,13 @@ class DiffHtmlFormatter(HtmlFormatter):
else: else:
if change: if change:
if isinstance(left_no, int) and isinstance(right_no, int): if isinstance(left_no, int) and isinstance(right_no, int):
no = '<span class="lineno_q lineno_rightchange">' + str(right_no) + "</span>" no = '<span class="lineno_q lineno_rightchange">' + \
str(right_no) + "</span>"
elif isinstance(left_no, int) and not isinstance(right_no, int): elif isinstance(left_no, int) and not isinstance(right_no, int):
no = '<span class="lineno_q lineno_rightdel"> </span>' no = '<span class="lineno_q lineno_rightdel"> </span>'
elif not isinstance(left_no, int) and isinstance(right_no, int): elif not isinstance(left_no, int) and isinstance(right_no, int):
no = '<span class="lineno_q lineno_rightadd">' + str(right_no) + "</span>" no = '<span class="lineno_q lineno_rightadd">' + \
str(right_no) + "</span>"
else: else:
no = '<span class="lineno_q">' + str(right_no) + "</span>" no = '<span class="lineno_q">' + str(right_no) + "</span>"
@ -145,7 +229,6 @@ class DiffHtmlFormatter(HtmlFormatter):
pass # this is expected sometimes pass # this is expected sometimes
yield 0, '\n</pre>' yield 0, '\n</pre>'
def _wrap_tablelinenos(self, inner): def _wrap_tablelinenos(self, inner):
dummyoutfile = io.StringIO() dummyoutfile = io.StringIO()
lncount = 0 lncount = 0
@ -185,7 +268,6 @@ class DiffHtmlFormatter(HtmlFormatter):
yield 0, '</td></tr></table>' yield 0, '</td></tr></table>'
class CodeDiff(object): class CodeDiff(object):
""" """
Manages a pair of source files and generates a single html diff page comparing Manages a pair of source files and generates a single html diff page comparing
@ -198,7 +280,6 @@ class CodeDiff(object):
resetCssFile = "./deps/reset.css" resetCssFile = "./deps/reset.css"
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
@ -228,7 +309,6 @@ class CodeDiff(object):
self.tolines = [n + "\n" for n in totxt.split("\n")] self.tolines = [n + "\n" for n in totxt.split("\n")]
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
@ -252,10 +332,10 @@ class CodeDiff(object):
else: else:
context_lines = None context_lines = None
diffs = difflib._mdiff(self.fromlines, self.tolines, context_lines, linejunk=None, charjunk=difflib.IS_CHARACTER_JUNK) diffs = difflib._mdiff(self.fromlines, self.tolines, context_lines,
linejunk=None, charjunk=difflib.IS_CHARACTER_JUNK)
return list(diffs) return list(diffs)
def format(self, verbose=False): def format(self, verbose=False):
self.diffs = self.getDiffDetails(self.fromfile, self.tofile) self.diffs = self.getDiffDetails(self.fromfile, self.tofile)
@ -263,7 +343,8 @@ class CodeDiff(object):
for diff in self.diffs: for diff in self.diffs:
print("%-6s %-80s %-80s" % (diff[2], diff[0], diff[1])) print("%-6s %-80s %-80s" % (diff[2], diff[0], diff[1]))
fields = ( (self.leftcode, True, self.fromfile) , (self.rightcode, False, self.tofile) ) fields = ((self.leftcode, True, self.fromfile),
(self.rightcode, False, self.tofile))
codeContents = [] codeContents = []
for (code, isLeft, filename) in fields: for (code, isLeft, filename) in fields:
@ -287,8 +368,6 @@ class CodeDiff(object):
codeContents.append(formatted) codeContents.append(formatted)
diffTemplate = open("./templates/diff_template.html",'r').read()
answers = { answers = {
"html_title": self.filename, "html_title": self.filename,
"reset_css": self.resetCssFile, "reset_css": self.resetCssFile,
@ -301,12 +380,11 @@ class CodeDiff(object):
"diff_js": self.diffJsFile, "diff_js": self.diffJsFile,
} }
self.htmlContents = diffTemplate % answers self.htmlContents = HTML_TEMPLATE % answers
def write(self, path="index.html"): def write(self, path="index.html"):
fh = open(path, 'w') fh = open(path, 'w')
fh.write(self.htmlContents.encode('utf8')) fh.write(self.htmlContents)
fh.close() fh.close()
@ -322,7 +400,8 @@ creates an html page which highlights the differences between the two. """
parser = argparse.ArgumentParser(description=description) parser = argparse.ArgumentParser(description=description)
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('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).') parser.add_argument('file2', help='source file to compare ("after" file).')
args = parser.parse_args() args = parser.parse_args()

View file

@ -1,77 +0,0 @@
<!DOCTYPE html>
<html class="no-js">
<head>
<!--
html_title: browser tab title
reset_css: relative path to reset css file
pygments_css: relative path to pygments css file
diff_css: relative path to diff layout css file
page_title: title shown at the top of the page. This should be the filename of the files being diff'd
original_code: full html contents of original file
modified_code: full html contents of modified file
jquery_js: path to jquery.min.js
diff_js: path to diff.js
-->
<meta charset="utf-8">
<title>
%(html_title)s
</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<link rel="stylesheet" href="%(reset_css)s" type="text/css">
<link class="syntaxdef" rel="stylesheet" href="%(pygments_css)s" type="text/css">
<link rel="stylesheet" href="%(diff_css)s" type="text/css">
</head>
<body>
<div class="" id="topbar">
<div id="filetitle">
<!--&#10140;&nbsp;&nbsp;-->%(page_title)s
</div>
<div class="switches">
<div class="switch">
<input id="showoriginal" class="toggle toggle-yes-no menuoption" type="checkbox" checked>
<label for="showoriginal" data-on="&#10004; Original" data-off="Original"></label>
</div>
<div class="switch">
<input id="showmodified" class="toggle toggle-yes-no menuoption" type="checkbox" checked>
<label for="showmodified" data-on="&#10004; Modified" data-off="Modified"></label>
</div>
<div class="switch">
<input id="highlight" class="toggle toggle-yes-no menuoption" type="checkbox" checked>
<label for="highlight" data-on="&#10004; Highlight" data-off="Highlight"></label>
</div>
<div class="switch">
<input id="codeprintmargin" class="toggle toggle-yes-no menuoption" type="checkbox" checked>
<label for="codeprintmargin" data-on="&#10004; Margin" data-off="Margin"></label>
</div>
<div class="switch">
<input id="dosyntaxhighlight" class="toggle toggle-yes-no menuoption" type="checkbox" checked>
<label for="dosyntaxhighlight" data-on="&#10004; Syntax" data-off="Syntax"></label>
</div>
</div>
</div>
<div id="maincontainer" class="">
<div id="leftcode" class="left-inner-shadow codebox divider-outside-bottom">
<div class="codefiletab">
&#10092; Original
</div>
<div class="printmargin">
01234567890123456789012345678901234567890123456789012345678901234567890123456789
</div>
%(original_code)s
</div>
<div id="rightcode" class="left-inner-shadow codebox divider-outside-bottom">
<div class="codefiletab">
&#10093; Modified
</div>
<div class="printmargin">
01234567890123456789012345678901234567890123456789012345678901234567890123456789
</div>
%(modified_code)s
</div>
</div>
<script src="%(jquery_js)s" type="text/javascript"></script>
<script src="%(diff_js)s" type="text/javascript"></script>
</body>
</html>