Skip to content

Instantly share code, notes, and snippets.

@sylvchev
Last active September 26, 2018 16:41
Show Gist options
  • Save sylvchev/c954b449385e73186d8ac3048558caa4 to your computer and use it in GitHub Desktop.
Save sylvchev/c954b449385e73186d8ac3048558caa4 to your computer and use it in GitHub Desktop.
Shell script to compare tex files in python with difflib, output HTML file to highlight the difference.
#!/usr/bin/env python
import sys
import re
import difflib as dl
if __name__ == '__main__':
if len(sys.argv) < 3 or len(sys.argv) > 5:
print ("usage: %s file1 file2 [output.html]" % sys.argv[0])
sys.exit(0)
filename1, filename2 = sys.argv[1], sys.argv[2]
if len(sys.argv) == 4: outname = sys.argv[3]
else: outname = "diff-output.html"
with open(filename1) as file1, open(filename2) as file2, open(outname, 'w') as outfile:
raw1, raw2 = file1.readlines(), file2.readlines()
text1, text2 = [], []
for l in raw1: # suppress TeX comment and blanks
if (re.match('^%', l) == None and
re.match('\s*\n', l) == None): text1.append(l)
for l in raw2:
if (re.match('^%', l) == None and
re.match('\s*\n', l) == None): text2.append(l)
for diff in dl.HtmlDiff().make_file(text1, text2,
filename1, filename2, context=False,
numlines=0):
outfile.write(diff)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment