Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions diff2HtmlCompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,12 @@ class CodeDiff(object):
resetCssFile = "./deps/reset.css"
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, encoding="utf-8"):
self.filename = name
self.fromfile = fromfile
if fromtxt == None:
try:
with io.open(fromfile) as f:
with io.open(fromfile, encoding=encoding) as f:
self.fromlines = f.readlines()
except Exception as e:
print("Problem reading file %s" % fromfile)
Expand All @@ -305,7 +305,7 @@ def __init__(self, fromfile, tofile, fromtxt=None, totxt=None, name=None):
self.tofile = tofile
if totxt == None:
try:
with io.open(tofile) as f:
with io.open(tofile, encoding=encoding) as f:
self.tolines = f.readlines()
except Exception as e:
print("Problem reading file %s" % tofile)
Expand Down Expand Up @@ -344,7 +344,7 @@ def expand_tabs(line):
def format(self, options):
self.diffs = self.getDiffDetails(self.fromfile, self.tofile)

if options.verbose:
if options['verbose']:
for diff in self.diffs:
print("%-6s %-80s %-80s" % (diff[2], diff[0], diff[1]))

Expand All @@ -358,13 +358,13 @@ def format(self, options):
self.diffs,
nobackground=False,
linenos=True,
style=options.syntax_css)
style=options['syntax_css'])

try:
self.lexer = guess_lexer_for_filename(self.filename, code)

except pygments.util.ClassNotFound:
if options.verbose:
if options['verbose']:
print("No Lexer Found! Using default...")

self.lexer = DefaultLexer()
Expand All @@ -376,20 +376,20 @@ def format(self, options):
answers = {
"html_title": self.filename,
"reset_css": self.resetCssFile,
"pygments_css": self.pygmentsCssFile % options.syntax_css,
"pygments_css": self.pygmentsCssFile % options['syntax_css'],
"diff_css": self.diffCssFile,
"page_title": self.filename,
"original_code": codeContents[0],
"modified_code": codeContents[1],
"jquery_js": self.jqueryJsFile,
"diff_js": self.diffJsFile,
"page_width": "page-80-width" if options.print_width else "page-full-width"
"page_width": "page-80-width" if options['print_width'] else "page-full-width"
}

self.htmlContents = HTML_TEMPLATE % answers

def write(self, path):
fh = io.open(path, 'w')
def write(self, path, encoding="utf-8"):
fh = io.open(path, 'w', encoding=encoding)
fh.write(self.htmlContents)
fh.close()

Expand Down Expand Up @@ -424,6 +424,7 @@ def show(outputpath):
raise ValueError("Syntax CSS (-c) must be one of %r." % PYGMENTS_STYLES)

outputpath = "index.html"
main(args.file1, args.file2, outputpath, args)
if args.show:
args = vars(args)
main(args['file1'], args['file2'], outputpath, args)
if args['show']:
show(outputpath)