1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
import sys
import difflib
import argparse
import StringIO
import pygments
from pygments.lexers import guess_lexer_for_filename
from pygments.lexer import RegexLexer
from pygments.formatters import HtmlFormatter
from pygments.token import *
class DefaultLexer(RegexLexer):
"""
Simply lex each line as a token.
"""
name = 'Default'
aliases = ['default']
filenames = ['*']
tokens = {
'root': [
(r'.*\n', Text),
]
}
class DiffHtmlFormatter(HtmlFormatter):
"""
Formats a single source file with pygments and adds diff highlights based on the
diff details given.
"""
isLeft = False
diffs = None
def __init__(self, isLeft, diffs, *args, **kwargs):
self.isLeft = isLeft
self.diffs = diffs
super(DiffHtmlFormatter, self).__init__(*args, **kwargs)
def wrap(self, source, outfile):
return self._wrap_code(source)
def getDiffLineNos(self):
retlinenos = []
for idx, ((left_no, left_line),(right_no, right_line),change) in enumerate(self.diffs):
no = None
if self.isLeft:
if change:
if isinstance(left_no, int) and isinstance(right_no, int):
no = '<span class="lineno_q lineno_leftchange">' + str(left_no) + "</span>"
elif isinstance(left_no, int) and not isinstance(right_no, int):
no = '<span class="lineno_q lineno_leftdel">' + str(left_no) + "</span>"
elif not isinstance(left_no, int) and isinstance(right_no, int):
no = '<span class="lineno_q lineno_leftadd"> </span>'
else:
no = '<span class="lineno_q">' + str(left_no) + "</span>"
else:
if change:
if isinstance(left_no, int) and isinstance(right_no, int):
no = '<span class="lineno_q lineno_rightchange">' + str(right_no) + "</span>"
elif isinstance(left_no, int) and not isinstance(right_no, int):
no = '<span class="lineno_q lineno_rightdel"> </span>'
elif not isinstance(left_no, int) and isinstance(right_no, int):
no = '<span class="lineno_q lineno_rightadd">' + str(right_no) + "</span>"
else:
no = '<span class="lineno_q">' + str(right_no) + "</span>"
retlinenos.append(no)
return retlinenos
def _wrap_code(self, source):
source = list(source)
yield 0, '<pre>'
for idx, ((left_no, left_line),(right_no, right_line),change) in enumerate(self.diffs):
#print idx, ((left_no, left_line),(right_no, right_line),change)
try:
if self.isLeft:
if change:
if isinstance(left_no, int) and isinstance(right_no, int) and left_no <= len(source):
i,t = source[left_no-1]
t = '<span class="left_diff_change">' + t + "</span>"
elif isinstance(left_no, int) and not isinstance(right_no, int) and left_no <= len(source):
i,t = source[left_no-1]
t = '<span class="left_diff_del">' + t + "</span>"
elif not isinstance(left_no, int) and isinstance(right_no, int):
i,t = 1, left_line
t = '<span class="left_diff_add">' + t + "</span>"
else:
raise
else:
if left_no <= len(source):
i,t = source[left_no-1]
else:
i = 1
t = left_line
else:
if change:
if isinstance(left_no, int) and isinstance(right_no, int) and right_no <= len(source):
i,t = source[right_no-1]
t = '<span class="right_diff_change">' + t + "</span>"
elif isinstance(left_no, int) and not isinstance(right_no, int):
i,t = 1, right_line
t = '<span class="right_diff_del">' + t + "</span>"
elif not isinstance(left_no, int) and isinstance(right_no, int) and right_no <= len(source):
i,t = source[right_no-1]
t = '<span class="right_diff_add">' + t + "</span>"
else:
raise
else:
if right_no <= len(source):
i,t = source[right_no-1]
else:
i = 1
t = right_line
yield i, t
except:
#print "WARNING! failed to enumerate diffs fully!"
pass # this is expected sometimes
yield 0, '\n</pre>'
def _wrap_tablelinenos(self, inner):
dummyoutfile = StringIO.StringIO()
lncount = 0
for t, line in inner:
if t:
lncount += 1
dummyoutfile.write(line)
fl = self.linenostart
mw = len(str(lncount + fl - 1))
sp = self.linenospecial
st = self.linenostep
la = self.lineanchors
aln = self.anchorlinenos
nocls = self.noclasses
lines = []
for i in self.getDiffLineNos():
lines.append('%s' % (i,))
ls = ''.join(lines)
# in case you wonder about the seemingly redundant <div> here: since the
# content in the other cell also is wrapped in a div, some browsers in
# some configurations seem to mess up the formatting...
if nocls:
yield 0, ('<table class="%stable">' % self.cssclass +
'<tr><td><div class="linenodiv" '
'style="background-color: #f0f0f0; padding-right: 10px">'
'<pre style="line-height: 125%">' +
ls + '</pre></div></td><td class="code">')
else:
yield 0, ('<table class="%stable">' % self.cssclass +
'<tr><td class="linenos"><div class="linenodiv"><pre>' +
ls + '</pre></div></td><td class="code">')
yield 0, dummyoutfile.getvalue()
yield 0, '</td></tr></table>'
class CodeDiff(object):
"""
Manages a pair of source files and generates a single html diff page comparing
the contents.
"""
pygmentsStyleOpt = "vs"
pygmentsCssFile="./deps/codeformats/%s.css" % pygmentsStyleOpt
diffCssFile="./deps/diff.css"
diffJsFile="./deps/diff.js"
resetCssFile="./deps/reset.css"
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:
self.fromlines = open(fromfile, 'U').readlines()
else:
self.fromlines = [n + "\n" for n in fromtxt.split("\n")]
self.leftcode = "".join(self.fromlines)
self.tofile = tofile
if totxt == None:
self.tolines = open(tofile, 'U').readlines()
else:
self.tolines = [n + "\n" for n in totxt.split("\n")]
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):
# hide real spaces
line = line.replace(' ','\0')
# expand tabs into spaces
line = line.expandtabs(tabSize)
# replace spaces from expanded tabs back into tab characters
# (we'll replace them with markup after we do differencing)
line = line.replace(' ','\t')
return line.replace('\0',' ').rstrip('\n')
self.fromlines = [expand_tabs(line) for line in self.fromlines]
self.tolines = [expand_tabs(line) for line in self.tolines]
# create diffs iterator which generates side by side from/to data
if context:
context_lines = numlines
else:
context_lines = None
diffs = difflib._mdiff(self.fromlines, self.tolines, context_lines, linejunk=None, charjunk=difflib.IS_CHARACTER_JUNK)
return list(diffs)
def format(self, verbose=False):
self.diffs = self.getDiffDetails(self.fromfile, self.tofile)
if verbose:
for diff in self.diffs:
print "%-6s %-80s %-80s" % ( diff[2], diff[0], diff[1] )
fields = ( (self.leftcode, True, self.fromfile) , (self.rightcode, False, self.tofile) )
codeContents = []
for (code, isLeft, filename) in fields:
inst = DiffHtmlFormatter(isLeft,
self.diffs,
nobackground=False,
linenos=True,
style=self.pygmentsStyleOpt)
try:
self.lexer = guess_lexer_for_filename(self.filename, code)
except pygments.util.ClassNotFound:
if verbose:
print "No Lexer Found! Using default..."
self.lexer = DefaultLexer()
formatted = pygments.highlight(code, self.lexer, inst)
codeContents.append(formatted)
diffTemplate = open("./templates/diff_template.html",'r').read()
answers = {
"html_title": self.filename,
"reset_css": self.resetCssFile,
"pygments_css": self.pygmentsCssFile,
"diff_css": self.diffCssFile,
"page_title": self.filename,
"original_code": codeContents[0],
"modified_code": codeContents[1],
"jquery_js": self.jqueryJsFile,
"diff_js": self.diffJsFile,
}
self.htmlContents = diffTemplate % answers
def write(self, path="index.html"):
fh = open(path,'w')
fh.write(self.htmlContents.encode('utf8'))
fh.close()
def main(fromfile, tofile, verbose=False):
codeDiff = CodeDiff(fromfile, tofile, name=tofile)
codeDiff.format(verbose)
codeDiff.write()
if __name__ == "__main__":
description = """Given two source files this application\
creates an html page which highlights the differences between the two. """
parser = argparse.ArgumentParser(description=description)
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('file2', help='source file to compare ("after" file).')
args = parser.parse_args()
main(args.file1, args.file2, args.v)
|