1 --- diff2html.py.orig 2012-06-08 17:02:56.193781281 -0400
2 +++ diff2html.py 2012-06-10 17:19:50.959749390 -0400
4 # minimum line size, we add a zero-sized breakable space every
9 -outputfile = sys.stdout
10 -exclude_headers = 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">
19 <meta name="generator" content="diff2html.rb" />
20 <title>HTML Diff</title>
46 -add_cpt, del_cpt = 0,0
48 -hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0,0,0,0
50 +add_cpt, del_cpt = 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;.,/):"
64 if i not in ['\t', '\n'] and ((j < 32) or (j >= 127)):
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 ]))
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 @@
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)))
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)))
118 + while coord != (0, 0):
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)
143 l2.append(DIFFON + t[fy] + DIFFOFF)
144 - elif diff == (1,0):
145 + elif diff == (1, 0):
146 l1.append(DIFFON + s[fx] + DIFFOFF)
148 elif child_val-father_val == 1:
149 @@ -161,204 +157,204 @@
153 - r1,r2 = (reduce(lambda x,y:x+y, l1), reduce(lambda x,y:x+y, l2))
155 + r1, r2 = (reduce(lambda x, y:x+y, l1), reduce(lambda x, y:x+y, l2))
159 def convert(s, linesize=0, ponct=0):
169 t += '<span class="diffchanged2">'
175 elif htmlentitydefs.codepoint2name.has_key(ord(c)):
176 - t += "&%s;"%(htmlentitydefs.codepoint2name[ord(c)])
177 + t += "&%s;" % (htmlentitydefs.codepoint2name[ord(c)])
180 # special highlighted chars
181 - elif c=="\t" and ponct==1:
182 + elif c == "\t" and ponct == 1:
183 n = tabsize-(i%tabsize)
188 t += ('<span class="diffponct">»</span>'+' '*(n-1))
189 - elif c==" " and ponct==1:
190 + elif c == " " and ponct == 1:
191 t += '<span class="diffponct">·</span>'
192 - elif c=="\n" and ponct==1:
193 + elif c == "\n" and ponct == 1:
195 t += '<span class="diffponct">\</span>'
200 - if linesize and (WORDBREAK.count(c)==1):
201 + if linesize and (WORDBREAK.count(c) == 1):
204 - if linesize and i>linesize:
207 + if linesize and i > linesize:
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))
228 - global hunk_off1, hunk_size1, hunk_off2, hunk_size2
229 - global show_hunk_infos
230 +def add_hunk(output_file, 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))
237 # ⋮ - vertical ellipsis
238 - outputfile.write('<tr class="diffhunk"><td colspan="2">⋮</td><td colspan="2">⋮</td></tr>');
239 + output_file.write('<tr class="diffhunk"><td colspan="2">⋮</td><td colspan="2">⋮</td></tr>')
242 -def add_line(s1, s2):
243 +def add_line(s1, s2, output_file):
247 - if s1==None and s2==None:
249 - elif s1==None or s1=="":
251 - elif s2==None or s1=="":
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"
262 + type_name = "unmodified"
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>')
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>')
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>')
300 - outputfile.write('<td colspan="2"></td>')
302 + output_file.write('<td colspan="2"></td>')
304 - outputfile.write('</tr>\n')
305 + output_file.write('</tr>\n')
317 +def empty_buffer(output_file):
322 if del_cpt == 0 or add_cpt == 0:
324 - add_line(l[0], l[1])
326 + add_line(l[0], l[1], output_file)
328 elif del_cpt != 0 and add_cpt != 0:
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):
349 - add_cpt, del_cpt = 0,0
351 + add_line(s0, s1, output_file)
353 + add_cpt, del_cpt = 0, 0
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
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)
371 - l=inputfile.readline()
373 + l = inputfile.readline()
377 - m=re.match('^--- ([^\s]*)', l)
378 + m = re.match('^--- ([^\s]*)', l)
381 - file1=m.groups()[0]
382 - l=inputfile.readline()
383 - m=re.match('^\+\+\+ ([^\s]*)', l)
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]
391 + l = inputfile.readline()
392 + m = re.match('^\+\+\+ ([^\s]*)', l)
394 + file2 = m.groups()[0]
396 + add_filename(file1, file2, outputfile)
397 + hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0, 0, 0, 0
400 - m=re.match("@@ -(\d+),?(\d*) \+(\d+),?(\d*)", l)
401 + m = re.match("@@ -(\d+),?(\d*) \+(\d+),?(\d*)", l)
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
409 + add_hunk(outputfile, show_hunk_infos)
412 if hunk_size1 == 0 and hunk_size2 == 0:
415 + empty_buffer(outputfile)
416 + add_comment(l, outputfile)
419 if re.match("^\+", l):
422 - buffer.append((None, l[1:]))
423 + buf.append((None, l[1:]))
426 if re.match("^\-", l):
429 - buffer.append((l[1:], None))
430 + buf.append((l[1:], None))
433 if re.match("^\ ", l) and hunk_size1 and hunk_size2:
435 + empty_buffer(outputfile)
438 - buffer.append((l[1:], l[1:]))
439 + buf.append((l[1:], l[1:]))
444 + empty_buffer(outputfile)
445 + add_comment(l, outputfile)
448 + empty_buffer(outputfile)
449 outputfile.write(table_footer)
450 if not exclude_headers:
451 outputfile.write(html_footer)
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
464 global linesize, tabsize
465 - global inputfile, outputfile
466 - global exclude_headers, show_CR, show_hunk_infos
469 + inputfile = sys.stdin
470 + outputfile = sys.stdout
472 + exclude_headers = False
473 + show_hunk_infos = False
476 opts, args = getopt.getopt(sys.argv[1:], "hi:o:xt:l:rk",
478 print str(err) # will print something like "option -a not recognized"
484 if o in ("-h", "--help"):
486 show_hunk_infos = True
488 assert False, "unhandled option"
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):
501 + self.__lines = txt.splitlines(True)
502 + self.__lastIndex = len( self.__lines ) - 1
504 + def readline(self):
505 + " Imitates readline behavior "
506 + if self.__index > self.__lastIndex:
509 + return self.__lines[self.__index - 1]
511 + class OutputFileLikeDummy:
512 + " Imitates an output file "
513 + def __init__(self):
516 + def write(self, what):
517 + " Writes into an internal buffer "
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__":