-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff2html.py
399 lines (325 loc) · 11.7 KB
/
diff2html.py
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#! /usr/bin/python
# coding=utf-8
import sys, re, htmlentitydefs, getopt, StringIO, codecs, datetime
# minimum line size, we add a zero-sized breakable space every
# LINESIZE characters
linesize = 20
tabsize = 8
show_CR = False
encoding = "utf-8"
lang = "en"
algorithm = 0
desc = "File comparison"
dtnow = datetime.datetime.now()
modified_date = "%s+01:00"%dtnow.isoformat()
DIFFON = "\x01"
DIFFOFF = "\x02"
buf = []
add_cpt, del_cpt = 0, 0
line1, line2 = 0, 0
hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0, 0, 0, 0
# Characters we're willing to word wrap on
WORDBREAK = " \t;.,/):-"
def sane(x):
r = ""
for i in x:
j = ord(i)
if i not in ['\t', '\n'] and (j < 32):
r = r + " "
else:
r = r + i
return r
def linediff(s, t):
'''
Original line diff algorithm of diff2html. It's character based.
'''
if len(s):
s = unicode(reduce(lambda x, y:x+y, [ sane(c) for c in s ]))
if len(t):
t = unicode(reduce(lambda x, y:x+y, [ sane(c) for c in t ]))
m, n = len(s), len(t)
d = [[(0, 0) for i in range(n+1)] for i in range(m+1)]
d[0][0] = (0, (0, 0))
for i in range(m+1)[1:]:
d[i][0] = (i,(i-1, 0))
for j in range(n+1)[1:]:
d[0][j] = (j,(0, j-1))
for i in range(m+1)[1:]:
for j in range(n+1)[1:]:
if s[i-1] == t[j-1]:
cost = 0
else:
cost = 1
d[i][j] = min((d[i-1][j][0] + 1, (i-1, j)),
(d[i][j-1][0] + 1, (i, j-1)),
(d[i-1][j-1][0] + cost, (i-1, j-1)))
l = []
coord = (m, n)
while coord != (0, 0):
l.insert(0, coord)
x, y = coord
coord = d[x][y][1]
l1 = []
l2 = []
for coord in l:
cx, cy = coord
child_val = d[cx][cy][0]
father_coord = d[cx][cy][1]
fx, fy = father_coord
father_val = d[fx][fy][0]
diff = (cx-fx, cy-fy)
if diff == (0, 1):
l1.append("")
l2.append(DIFFON + t[fy] + DIFFOFF)
elif diff == (1, 0):
l1.append(DIFFON + s[fx] + DIFFOFF)
l2.append("")
elif child_val-father_val == 1:
l1.append(DIFFON + s[fx] + DIFFOFF)
l2.append(DIFFON + t[fy] + DIFFOFF)
else:
l1.append(s[fx])
l2.append(t[fy])
r1, r2 = (reduce(lambda x, y:x+y, l1), reduce(lambda x, y:x+y, l2))
return r1, r2
def diff_changed(old, new):
'''
Returns the differences basend on characters between two strings
wrapped with DIFFON and DIFFOFF using `diff`.
'''
con = {'=': (lambda x: x),
'+': (lambda x: DIFFON + x + DIFFOFF),
'-': (lambda x: '')}
return "".join([(con[a])("".join(b)) for a, b in diff(old, new)])
def diff_changed_ts(old, new):
'''
Returns a tuple for a two sided comparison based on characters, see `diff_changed`.
'''
return (diff_changed(new, old), diff_changed(old, new))
def word_diff(old, new):
'''
Returns the difference between the old and new strings based on words. Punctuation is not part of the word.
Params:
old the old string
new the new string
Returns:
the output of `diff` on the two strings after splitting them
on whitespace (a list of change instructions; see the docstring
of `diff`)
'''
separator_pattern = '(\W+)';
return diff(re.split(separator_pattern, old, flags=re.UNICODE), re.split(separator_pattern, new, flags=re.UNICODE))
def diff_changed_words(old, new):
'''
Returns the difference between two strings based on words (see `word_diff`)
wrapped with DIFFON and DIFFOFF.
Returns:
the output of the diff expressed delimited with DIFFON and DIFFOFF.
'''
con = {'=': (lambda x: x),
'+': (lambda x: DIFFON + x + DIFFOFF),
'-': (lambda x: '')}
return "".join([(con[a])("".join(b)) for a, b in word_diff(old, new)])
def diff_changed_words_ts(old, new):
'''
Returns a tuple for a two sided comparison based on words, see `diff_changed_words`.
'''
return (diff_changed_words(new, old), diff_changed_words(old, new))
def convert(s, linesize=0, ponct=0):
i = 0
t = u""
for c in s:
# used by diffs
if c == DIFFON:
t += u'<span class="diffchanged2">'
elif c == DIFFOFF:
t += u"</span>"
# special html chars
elif htmlentitydefs.codepoint2name.has_key(ord(c)):
t += u"&%s;" % (htmlentitydefs.codepoint2name[ord(c)])
i += 1
# special highlighted chars
elif c == "\t" and ponct == 1:
n = tabsize-(i%tabsize)
if n == 0:
n = tabsize
t += (u'<span class="diffponct">»</span>'+' '*(n-1))
#elif c == " " and ponct == 1:
# t += u'<span class="diffponct">·</span>'
elif c == "\n" and ponct == 1:
if show_CR:
t += u'<span class="diffponct">\</span>'
else:
t += c
i += 1
if linesize and (WORDBREAK.count(c) == 1):
t += u'​'
i = 0
if linesize and i > linesize:
i = 0
t += u"​"
return t
def add_collapsable(s, output_file):
output_file.write(('<div class="diffmisc"><td colspan="4">%s</td>\n'%convert(s)).encode(encoding))
def add_newfile(output_file):
output_file.write('<tr class="diffhunk"><td colspan="2">Null</td>')
output_file.write('<td colspan="2"><font color="#880000" float="right">New</font></td></tr>\n')
def add_filename(output_file):
output_file.write("<tr><th class='difftablehead' colspan='2'>修改前</th>")
output_file.write("<th class='difftablehead' colspan='2'>修改后</th></tr>\n")
def add_hunk(output_file, show_hunk_infos):
if show_hunk_infos:
output_file.write('<tr class="diffhunk"><td colspan="2">Offset %d, %d lines modified</td>'%(hunk_off1, hunk_size1))
output_file.write('<td colspan="2">Offset %d, %d lines modified</td></tr>\n'%(hunk_off2, hunk_size2))
else:
# ⋮ - vertical ellipsis
output_file.write('<tr class="diffhunk"><td colspan="2">⋮</td><td colspan="2">⋮</td></tr>')
def add_line(s1, s2, output_file):
global line1
global line2
orig1 = s1
orig2 = s2
if s1 == None and s2 == None:
type_name = "unmodified"
elif s1 == None or s1 == "":
type_name = "added"
elif s2 == None or s1 == "":
type_name = "deleted"
elif s1 == s2:
type_name = "unmodified"
else:
type_name = "changed"
if algorithm == 1:
s1, s2 = diff_changed_words_ts(orig1, orig2)
elif algorithm == 2:
s1, s2 = diff_changed_ts(orig1, orig2)
else: # default
s1, s2 = linediff(orig1, orig2)
output_file.write(('<tr class="diff%s">' % type_name).encode(encoding))
if s1 != None and s1 != "":
output_file.write(('<td class="diffline">%d </td>' % line1).encode(encoding))
output_file.write('<td class="diffpresent">'.encode(encoding))
output_file.write(convert(s1, linesize=linesize, ponct=1).encode(encoding))
output_file.write('</td>')
else:
s1 = ""
output_file.write('<td colspan="2"> </td>')
if s2 != None and s2 != "":
output_file.write(('<td class="diffline">%d </td>'%line2).encode(encoding))
output_file.write('<td class="diffpresent">')
output_file.write(convert(s2, linesize=linesize, ponct=1).encode(encoding))
output_file.write('</td>')
else:
s2 = ""
output_file.write('<td colspan="2"></td>')
output_file.write('</tr>\n')
if s1 != "":
line1 += 1
if s2 != "":
line2 += 1
def empty_buffer(output_file):
global buf
global add_cpt
global del_cpt
if del_cpt == 0 or add_cpt == 0:
for l in buf:
add_line(l[0], l[1], output_file)
elif del_cpt != 0 and add_cpt != 0:
l0, l1 = [], []
for l in buf:
if l[0] != None:
l0.append(l[0])
if l[1] != None:
l1.append(l[1])
max_len = (len(l0) > len(l1)) and len(l0) or len(l1)
for i in range(max_len):
s0, s1 = "", ""
if i < len(l0):
s0 = l0[i]
if i < len(l1):
s1 = l1[i]
add_line(s0, s1, output_file)
add_cpt, del_cpt = 0, 0
buf = []
def parse_input(input_file, output_file, input_file_name, output_file_name,
show_hunk_infos):
global add_cpt, del_cpt
global line1, line2
global hunk_off1, hunk_size1, hunk_off2, hunk_size2
table_flag = False
changefilenum = 0
newfilenum = 0
while True:
l = input_file.readline()
# input_file content must have no blank line
if l == "":
break
m = re.match('^diff -ru\s+(\S+)\s+(\S+)', l)
n = re.match('^Only in\s+(\S+):\s(\S+)', l)
if m or n:
empty_buffer(output_file)
if m:
changefilenum += 1
filename = m.group(2)
if n:
newfilenum += 1
filename = '/'.join(n.groups())
if table_flag:
output_file.write(('</table></div>').encode(encoding))
else:
table_flag = True
add_collapsable(filename, output_file)
output_file.write(('<table id="difftable%d" class="diff_exp_table">'%changefilenum).encode(encoding))
if n:
empty_buffer(output_file)
add_filename(output_file)
add_newfile(output_file)
continue
m = re.match('^--- ([^\s]*)', l)
if m:
empty_buffer(output_file)
file1 = m.groups()[0]
while True:
l = input_file.readline()
m = re.match('^\+\+\+ ([^\s]*)', l)
if m:
file2 = m.groups()[0]
break
add_filename(output_file)
hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0, 0, 0, 0
continue
m = re.match("@@ -(\d+),?(\d*) \+(\d+),?(\d*)", l)
if m:
empty_buffer(output_file)
hunk_data = map(lambda x:x=="" and 1 or int(x), m.groups())
hunk_off1, hunk_size1, hunk_off2, hunk_size2 = hunk_data
line1, line2 = hunk_off1, hunk_off2
add_hunk(output_file, show_hunk_infos)
continue
if re.match("^\+", l):
add_cpt += 1
hunk_size2 -= 1
buf.append((None, l[1:]))
continue
if re.match("^\-", l):
del_cpt += 1
hunk_size1 -= 1
buf.append((l[1:], None))
continue
if re.match("^\ ", l) and hunk_size1 and hunk_size2:
empty_buffer(output_file)
hunk_size1 -= 1
hunk_size2 -= 1
buf.append((l[1:], l[1:]))
continue
empty_buffer(output_file)
output_file.write(('</table></div>').encode(encoding))
output_file.write(('<br /><div class="notemessage"><font color="#880000">NOTE:</font>').encode(encoding))
output_file.write(('<br />----%d File(s) Are Changed.\n'%changefilenum).encode(encoding))
output_file.write(('<br />----%d File(s) Are Added.</div>'%newfilenum).encode(encoding))
def parse_from_memory(txt, show_hunk_infos):
" Parses diff from memory and returns a string with html "
input_stream = StringIO.StringIO(txt)
output_stream = StringIO.StringIO()
parse_input(input_stream, output_stream, '', '', show_hunk_infos)
return output_stream.getvalue()