More unit tests
[codimension.git] / thirdparty / diff2html / diff2html.py.patch
blobcf8efe7175d769051b1ca517945d7ba4585e9daa
1 --- diff2html.py.orig 2012-06-08 17:02:56.193781281 -0400
2 +++ diff2html.py 2012-06-10 17:19:50.959749390 -0400
3 @@ -37,15 +37,11 @@
4 # minimum line size, we add a zero-sized breakable space every
5 # LINESIZE characters
6 linesize = 20
7 -tabsize = 8
8 -inputfile = sys.stdin
9 -outputfile = sys.stdout
10 -exclude_headers = False
11 +tabsize = 4
12 show_CR = False
13 -show_hunk_infos = False
16 -html_hdr="""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
17 +html_hdr = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
18 <html><head>
19 <meta name="generator" content="diff2html.rb" />
20 <title>HTML Diff</title>
21 @@ -67,56 +63,56 @@
22 <body>
23 """
25 -html_footer="""
26 +html_footer = """
27 </body></html>
28 """
30 -table_hdr="""
31 +table_hdr = """
32 <table class="diff">
33 """
35 -table_footer="""
36 +table_footer = """
37 </table>
38 """
40 -DIFFON="\x01"
41 -DIFFOFF="\x02"
42 +DIFFON = "\x01"
43 +DIFFOFF = "\x02"
45 -buffer=[]
46 -add_cpt, del_cpt = 0,0
47 -line1, line2 = 0,0
48 -hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0,0,0,0
49 +buf = []
50 +add_cpt, del_cpt = 0, 0
51 +line1, line2 = 0, 0
52 +hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0, 0, 0, 0
55 # Characters we're willing to word wrap on
56 -WORDBREAK=" \t;.,/):"
57 +WORDBREAK = " \t;.,/):"
59 def sane(x):
60 - r=""
61 + r = ""
62 for i in x:
63 j = ord(i)
64 if i not in ['\t', '\n'] and ((j < 32) or (j >= 127)):
65 - r=r+"."
66 + r = r + "."
67 else:
68 - r=r+i
69 + r = r + i
70 return r
72 def linediff(s, t):
73 if len(s):
74 - s=str(reduce(lambda x,y:x+y, [ sane(c) for c in s ]))
75 + s = str(reduce(lambda x, y:x+y, [ sane(c) for c in s ]))
76 if len(t):
77 - t=str(reduce(lambda x,y:x+y, [ sane(c) for c in t ]))
79 - m,n = len(s), len(t)
80 - d=[[(0,0) for i in range(n+1)] for i in range(m+1)]
81 - x=[[(0,0) for i in range(n+1)] for i in range(m+1)]
82 + t = str(reduce(lambda x, y:x+y, [ sane(c) for c in t ]))
85 - d[0][0] = (0, (0,0))
86 + m, n = len(s), len(t)
87 + d = [[(0, 0) for i in range(n+1)] for i in range(m+1)]
88 + x = [[(0, 0) for i in range(n+1)] for i in range(m+1)]
91 + d[0][0] = (0, (0, 0))
92 for i in range(m+1)[1:]:
93 - d[i][0] = (i,(i-1,0))
94 + d[i][0] = (i,(i-1, 0))
95 for j in range(n+1)[1:]:
96 - d[0][j] = (j,(0,j-1))
97 + d[0][j] = (j,(0, j-1))
99 for i in range(m+1)[1:]:
100 for j in range(n+1)[1:]:
101 @@ -124,34 +120,34 @@
102 cost = 0
103 else:
104 cost = 1
105 - d[i][j] = min((d[i-1][j][0] + 1, (i-1,j)),
106 - (d[i][j-1][0] + 1, (i,j-1)),
107 - (d[i-1][j-1][0] + cost, (i-1,j-1)))
109 - l=[]
110 - coord = (m,n)
111 - while coord != (0,0):
112 + d[i][j] = min((d[i-1][j][0] + 1, (i-1, j)),
113 + (d[i][j-1][0] + 1, (i, j-1)),
114 + (d[i-1][j-1][0] + cost, (i-1, j-1)))
116 + l = []
117 + coord = (m, n)
118 + while coord != (0, 0):
119 l.insert(0, coord)
120 - x,y = coord
121 + x, y = coord
122 coord = d[x][y][1]
124 l1 = []
125 l2 = []
127 for coord in l:
128 - cx,cy = coord
129 + cx, cy = coord
130 child_val = d[cx][cy][0]
133 father_coord = d[cx][cy][1]
134 - fx,fy = father_coord
135 + fx, fy = father_coord
136 father_val = d[fx][fy][0]
138 diff = (cx-fx, cy-fy)
140 - if diff == (0,1):
141 + if diff == (0, 1):
142 l1.append("")
143 l2.append(DIFFON + t[fy] + DIFFOFF)
144 - elif diff == (1,0):
145 + elif diff == (1, 0):
146 l1.append(DIFFON + s[fx] + DIFFOFF)
147 l2.append("")
148 elif child_val-father_val == 1:
149 @@ -161,204 +157,204 @@
150 l1.append(s[fx])
151 l2.append(t[fy])
153 - r1,r2 = (reduce(lambda x,y:x+y, l1), reduce(lambda x,y:x+y, l2))
154 - return r1,r2
155 + r1, r2 = (reduce(lambda x, y:x+y, l1), reduce(lambda x, y:x+y, l2))
156 + return r1, r2
159 def convert(s, linesize=0, ponct=0):
160 - i=0
161 - t=""
162 - l=[]
163 + i = 0
164 + t = ""
165 for c in s:
166 # used by diffs
167 - if c==DIFFON:
168 + if c == DIFFON:
169 t += '<span class="diffchanged2">'
170 - elif c==DIFFOFF:
171 + elif c == DIFFOFF:
172 t += "</span>"
174 # special html chars
175 elif htmlentitydefs.codepoint2name.has_key(ord(c)):
176 - t += "&%s;"%(htmlentitydefs.codepoint2name[ord(c)])
177 + t += "&%s;" % (htmlentitydefs.codepoint2name[ord(c)])
178 i += 1
180 # special highlighted chars
181 - elif c=="\t" and ponct==1:
182 + elif c == "\t" and ponct == 1:
183 n = tabsize-(i%tabsize)
184 - if n==0:
185 - n=tabsize
186 + if n == 0:
187 + n = tabsize
188 t += ('<span class="diffponct">&raquo;</span>'+'&nbsp;'*(n-1))
189 - elif c==" " and ponct==1:
190 + elif c == " " and ponct == 1:
191 t += '<span class="diffponct">&middot;</span>'
192 - elif c=="\n" and ponct==1:
193 + elif c == "\n" and ponct == 1:
194 if show_CR:
195 t += '<span class="diffponct">\</span>'
196 else:
197 t += c
198 i += 1
200 - if linesize and (WORDBREAK.count(c)==1):
201 + if linesize and (WORDBREAK.count(c) == 1):
202 t += '&#8203;'
203 - i=0
204 - if linesize and i>linesize:
205 - i=0
206 + i = 0
207 + if linesize and i > linesize:
208 + i = 0
209 t += "&#8203;"
212 return t
215 -def add_comment(s):
216 - outputfile.write('<tr class="diffmisc"><td colspan="4">%s</td></tr>\n'%convert(s))
217 +def add_comment(s, output_file):
218 + output_file.write('<tr class="diffmisc"><td colspan="4">%s</td></tr>\n'%convert(s))
220 -def add_filename(f1, f2):
221 - outputfile.write("<tr><th colspan='2'>%s</th>"%convert(f1, linesize=linesize))
222 - outputfile.write("<th colspan='2'>%s</th></tr>\n"%convert(f2, linesize=linesize))
223 +def add_filename(f1, f2, output_file):
224 + output_file.write("<tr><th colspan='2'>%s</th>"%convert(f1, linesize=linesize))
225 + output_file.write("<th colspan='2'>%s</th></tr>\n"%convert(f2, linesize=linesize))
227 -def add_hunk():
228 - global hunk_off1, hunk_size1, hunk_off2, hunk_size2
229 - global show_hunk_infos
230 +def add_hunk(output_file, show_hunk_infos):
231 if show_hunk_infos:
232 - outputfile.write('<tr class="diffhunk"><td colspan="2">Offset %d, %d lines modified</td>'%(hunk_off1, hunk_size1))
233 - outputfile.write('<td colspan="2">Offset %d, %d lines modified</td></tr>\n'%(hunk_off2, hunk_size2))
234 + output_file.write('<tr class="diffhunk"><td colspan="2">Offset %d, %d lines modified</td>'%(hunk_off1, hunk_size1))
235 + output_file.write('<td colspan="2">Offset %d, %d lines modified</td></tr>\n'%(hunk_off2, hunk_size2))
236 else:
237 # &#8942; - vertical ellipsis
238 - outputfile.write('<tr class="diffhunk"><td colspan="2">&#8942;</td><td colspan="2">&#8942;</td></tr>');
239 + output_file.write('<tr class="diffhunk"><td colspan="2">&#8942;</td><td colspan="2">&#8942;</td></tr>')
242 -def add_line(s1, s2):
243 +def add_line(s1, s2, output_file):
244 global line1
245 global line2
247 - if s1==None and s2==None:
248 - type="unmodified"
249 - elif s1==None or s1=="":
250 - type="added"
251 - elif s2==None or s1=="":
252 - type="deleted"
253 - elif s1==s2:
254 - type="unmodified"
255 + if s1 == None and s2 == None:
256 + type_name = "unmodified"
257 + elif s1 == None or s1 == "":
258 + type_name = "added"
259 + elif s2 == None or s1 == "":
260 + type_name = "deleted"
261 + elif s1 == s2:
262 + type_name = "unmodified"
263 else:
264 - type="changed"
265 - s1,s2 = linediff(s1, s2)
266 + type_name = "changed"
267 + s1, s2 = linediff(s1, s2)
269 - outputfile.write('<tr class="diff%s">'%type)
270 - if s1!=None and s1!="":
271 - outputfile.write('<td class="diffline">%d </td>'%line1)
272 - outputfile.write('<td class="diffpresent">')
273 - outputfile.write(convert(s1, linesize=linesize, ponct=1))
274 - outputfile.write('</td>')
275 + output_file.write('<tr class="diff%s">' % type_name)
276 + if s1 != None and s1 != "":
277 + output_file.write('<td class="diffline">%d </td>' % line1)
278 + output_file.write('<td class="diffpresent">')
279 + output_file.write(convert(s1, linesize=linesize, ponct=1))
280 + output_file.write('</td>')
281 else:
282 - s1=""
283 - outputfile.write('<td colspan="2"> </td>')
285 - if s2!=None and s2!="":
286 - outputfile.write('<td class="diffline">%d </td>'%line2)
287 - outputfile.write('<td class="diffpresent">')
288 - outputfile.write(convert(s2, linesize=linesize, ponct=1))
289 - outputfile.write('</td>')
290 + s1 = ""
291 + output_file.write('<td colspan="2"> </td>')
293 + if s2 != None and s2 != "":
294 + output_file.write('<td class="diffline">%d </td>'%line2)
295 + output_file.write('<td class="diffpresent">')
296 + output_file.write(convert(s2, linesize=linesize, ponct=1))
297 + output_file.write('</td>')
298 else:
299 - s2=""
300 - outputfile.write('<td colspan="2"></td>')
301 + s2 = ""
302 + output_file.write('<td colspan="2"></td>')
304 - outputfile.write('</tr>\n')
305 + output_file.write('</tr>\n')
307 - if s1!="":
308 + if s1 != "":
309 line1 += 1
310 - if s2!="":
311 + if s2 != "":
312 line2 += 1
315 -def empty_buffer():
316 - global buffer
317 +def empty_buffer(output_file):
318 + global buf
319 global add_cpt
320 global del_cpt
322 if del_cpt == 0 or add_cpt == 0:
323 - for l in buffer:
324 - add_line(l[0], l[1])
325 + for l in buf:
326 + add_line(l[0], l[1], output_file)
328 elif del_cpt != 0 and add_cpt != 0:
329 l0, l1 = [], []
330 - for l in buffer:
331 + for l in buf:
332 if l[0] != None:
333 l0.append(l[0])
334 if l[1] != None:
335 l1.append(l[1])
336 - max = (len(l0) > len(l1)) and len(l0) or len(l1)
337 - for i in range(max):
338 + max_len = (len(l0) > len(l1)) and len(l0) or len(l1)
339 + for i in range(max_len):
340 s0, s1 = "", ""
341 - if i<len(l0):
342 + if i < len(l0):
343 s0 = l0[i]
344 - if i<len(l1):
345 + if i < len(l1):
346 s1 = l1[i]
347 - add_line(s0, s1)
349 - add_cpt, del_cpt = 0,0
350 - buffer = []
351 + add_line(s0, s1, output_file)
353 + add_cpt, del_cpt = 0, 0
354 + buf = []
357 -def parse_input():
358 - global buffer, add_cpt, del_cpt
359 +def parse_input(inputfile, outputfile,
360 + exclude_headers, show_hunk_infos):
361 + global add_cpt, del_cpt
362 global line1, line2
363 global hunk_off1, hunk_size1, hunk_off2, hunk_size2
365 if not exclude_headers:
366 outputfile.write(html_hdr)
367 outputfile.write(table_hdr)
370 while True:
371 - l=inputfile.readline()
372 - if l=="":
373 + l = inputfile.readline()
374 + if l == "":
375 break
377 - m=re.match('^--- ([^\s]*)', l)
378 + m = re.match('^--- ([^\s]*)', l)
379 if m:
380 - empty_buffer()
381 - file1=m.groups()[0]
382 - l=inputfile.readline()
383 - m=re.match('^\+\+\+ ([^\s]*)', l)
384 - if m:
385 - file2=m.groups()[0]
386 - add_filename(file1, file2)
387 - hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0,0,0,0
388 + empty_buffer(outputfile)
389 + file1 = m.groups()[0]
390 + while True:
391 + l = inputfile.readline()
392 + m = re.match('^\+\+\+ ([^\s]*)', l)
393 + if m:
394 + file2 = m.groups()[0]
395 + break
396 + add_filename(file1, file2, outputfile)
397 + hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0, 0, 0, 0
398 continue
400 - m=re.match("@@ -(\d+),?(\d*) \+(\d+),?(\d*)", l)
401 + m = re.match("@@ -(\d+),?(\d*) \+(\d+),?(\d*)", l)
402 if m:
403 - empty_buffer()
404 + empty_buffer(outputfile)
405 hunk_data = map(lambda x:x=="" and 1 or int(x), m.groups())
406 hunk_off1, hunk_size1, hunk_off2, hunk_size2 = hunk_data
407 line1, line2 = hunk_off1, hunk_off2
408 - add_hunk()
409 + add_hunk(outputfile, show_hunk_infos)
410 continue
412 if hunk_size1 == 0 and hunk_size2 == 0:
413 - empty_buffer()
414 - add_comment(l)
415 + empty_buffer(outputfile)
416 + add_comment(l, outputfile)
417 continue
419 if re.match("^\+", l):
420 add_cpt += 1
421 hunk_size2 -= 1
422 - buffer.append((None, l[1:]))
423 + buf.append((None, l[1:]))
424 continue
426 if re.match("^\-", l):
427 del_cpt += 1
428 hunk_size1 -= 1
429 - buffer.append((l[1:], None))
430 + buf.append((l[1:], None))
431 continue
433 if re.match("^\ ", l) and hunk_size1 and hunk_size2:
434 - empty_buffer()
435 + empty_buffer(outputfile)
436 hunk_size1 -= 1
437 hunk_size2 -= 1
438 - buffer.append((l[1:], l[1:]))
439 + buf.append((l[1:], l[1:]))
440 continue
442 - empty_buffer()
443 - add_comment(l)
444 + empty_buffer(outputfile)
445 + add_comment(l, outputfile)
447 - empty_buffer()
448 + empty_buffer(outputfile)
449 outputfile.write(table_footer)
450 if not exclude_headers:
451 outputfile.write(html_footer)
452 @@ -375,7 +371,7 @@
453 -i file set input file, else use stdin
454 -o file set output file, else use stdout
455 -x exclude html header and footer
456 - -t tabsize set tab size (default 8)
457 + -t tabsize set tab size (default 4)
458 -l linesize set maximum line size is there is no word break (default 20)
459 -r show \\r characters
460 -k show hunk infos
461 @@ -384,8 +380,13 @@
463 def main():
464 global linesize, tabsize
465 - global inputfile, outputfile
466 - global exclude_headers, show_CR, show_hunk_infos
467 + global show_CR
469 + inputfile = sys.stdin
470 + outputfile = sys.stdout
472 + exclude_headers = False
473 + show_hunk_infos = False
475 try:
476 opts, args = getopt.getopt(sys.argv[1:], "hi:o:xt:l:rk",
477 @@ -396,7 +397,6 @@
478 print str(err) # will print something like "option -a not recognized"
479 usage()
480 sys.exit(2)
481 - output = None
482 verbose = False
483 for o, a in opts:
484 if o in ("-h", "--help"):
485 @@ -418,7 +418,43 @@
486 show_hunk_infos = True
487 else:
488 assert False, "unhandled option"
489 - parse_input()
490 + parse_input(inputfile, outputfile,
491 + exclude_headers, show_hunk_infos)
494 +def parse_from_memory(txt,
495 + exclude_headers, show_hunk_infos):
496 + " Parses diff from memory and returns a string with html "
497 + class InputFileLikeDummy:
498 + " Imitates file input for a string "
499 + def __init__(self, txt):
500 + self.__index = 0
501 + self.__lines = txt.splitlines(True)
502 + self.__lastIndex = len( self.__lines ) - 1
503 + return
504 + def readline(self):
505 + " Imitates readline behavior "
506 + if self.__index > self.__lastIndex:
507 + return ""
508 + self.__index += 1
509 + return self.__lines[self.__index - 1]
511 + class OutputFileLikeDummy:
512 + " Imitates an output file "
513 + def __init__(self):
514 + self.buf = ""
515 + return
516 + def write(self, what):
517 + " Writes into an internal buffer "
518 + self.buf += what
519 + return
521 + inputStream = InputFileLikeDummy(txt)
522 + outputStream = OutputFileLikeDummy()
523 + parse_input(inputStream, outputStream,
524 + exclude_headers, show_hunk_infos)
525 + return outputStream.buf
527 if __name__ == "__main__":
528 main()