X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=diff2html.py;h=6e2751e4d7a585f73de28abd6de0fc2ba2ef96d3;hb=ad3d66724c62f87c720185bad6f6be71627339de;hp=36d191bc84de961fd18c586af2f4b7cd2372e155;hpb=68c39b65bc6c15f2df60564f0599bdfdb24d91c0;p=diff2html.git
diff --git a/diff2html.py b/diff2html.py
index 36d191b..6e2751e 100644
--- a/diff2html.py
+++ b/diff2html.py
@@ -38,14 +38,10 @@ import sys, re, htmlentitydefs, getopt
# LINESIZE characters
linesize = 20
tabsize = 8
-inputfile = sys.stdin
-outputfile = sys.stdout
-exclude_headers = False
show_CR = False
-show_hunk_infos = False
-html_hdr="""
+html_hdr = """
HTML Diff
@@ -67,56 +63,56 @@ html_hdr="""
"""
-html_footer="""
+html_footer = """
"""
-table_hdr="""
+table_hdr = """
"""
-table_footer="""
+table_footer = """
"""
-DIFFON="\x01"
-DIFFOFF="\x02"
+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
+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;.,/):"
+WORDBREAK = " \t;.,/):"
def sane(x):
- r=""
+ r = ""
for i in x:
j = ord(i)
if i not in ['\t', '\n'] and ((j < 32) or (j >= 127)):
- r=r+"."
+ r = r + "."
else:
- r=r+i
+ r = r + i
return r
def linediff(s, t):
if len(s):
- s=str(reduce(lambda x,y:x+y, [ sane(c) for c in s ]))
+ s = str(reduce(lambda x, y:x+y, [ sane(c) for c in s ]))
if len(t):
- t=str(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)]
- x=[[(0,0) for i in range(n+1)] for i in range(m+1)]
-
-
- d[0][0] = (0, (0,0))
+ t = str(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)]
+ x = [[(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))
+ d[i][0] = (i,(i-1, 0))
for j in range(n+1)[1:]:
- d[0][j] = (j,(0,j-1))
+ d[0][j] = (j,(0, j-1))
for i in range(m+1)[1:]:
for j in range(n+1)[1:]:
@@ -124,34 +120,34 @@ def linediff(s, t):
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):
+ 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
+ x, y = coord
coord = d[x][y][1]
l1 = []
l2 = []
for coord in l:
- cx,cy = coord
+ cx, cy = coord
child_val = d[cx][cy][0]
-
+
father_coord = d[cx][cy][1]
- fx,fy = father_coord
+ fx, fy = father_coord
father_val = d[fx][fy][0]
diff = (cx-fx, cy-fy)
- if diff == (0,1):
+ if diff == (0, 1):
l1.append("")
l2.append(DIFFON + t[fy] + DIFFOFF)
- elif diff == (1,0):
+ elif diff == (1, 0):
l1.append(DIFFON + s[fx] + DIFFOFF)
l2.append("")
elif child_val-father_val == 1:
@@ -161,120 +157,117 @@ def linediff(s, t):
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
+ r1, r2 = (reduce(lambda x, y:x+y, l1), reduce(lambda x, y:x+y, l2))
+ return r1, r2
def convert(s, linesize=0, ponct=0):
- i=0
- t=""
- l=[]
+ i = 0
+ t = ""
for c in s:
# used by diffs
- if c==DIFFON:
+ if c == DIFFON:
t += ''
- elif c==DIFFOFF:
+ elif c == DIFFOFF:
t += ""
# special html chars
elif htmlentitydefs.codepoint2name.has_key(ord(c)):
- t += "&%s;"%(htmlentitydefs.codepoint2name[ord(c)])
+ t += "&%s;" % (htmlentitydefs.codepoint2name[ord(c)])
i += 1
# special highlighted chars
- elif c=="\t" and ponct==1:
+ elif c == "\t" and ponct == 1:
n = tabsize-(i%tabsize)
- if n==0:
- n=tabsize
+ if n == 0:
+ n = tabsize
t += ('»'+' '*(n-1))
- elif c==" " and ponct==1:
+ elif c == " " and ponct == 1:
t += '·'
- elif c=="\n" and ponct==1:
+ elif c == "\n" and ponct == 1:
if show_CR:
t += '\'
else:
t += c
i += 1
- if linesize and (WORDBREAK.count(c)==1):
+ if linesize and (WORDBREAK.count(c) == 1):
t += ''
- i=0
- if linesize and i>linesize:
- i=0
+ i = 0
+ if linesize and i > linesize:
+ i = 0
t += ""
-
+
return t
-def add_comment(s):
- outputfile.write('%s |
\n'%convert(s))
+def add_comment(s, output_file):
+ output_file.write('%s |
\n'%convert(s))
-def add_filename(f1, f2):
- outputfile.write("%s | "%convert(f1, linesize=linesize))
- outputfile.write("%s |
\n"%convert(f2, linesize=linesize))
+def add_filename(f1, f2, output_file):
+ output_file.write("%s | "%convert(f1, linesize=linesize))
+ output_file.write("%s |
\n"%convert(f2, linesize=linesize))
-def add_hunk():
- global hunk_off1, hunk_size1, hunk_off2, hunk_size2
- global show_hunk_infos
+def add_hunk(output_file, show_hunk_infos):
if show_hunk_infos:
- outputfile.write('Offset %d, %d lines modified | '%(hunk_off1, hunk_size1))
- outputfile.write('Offset %d, %d lines modified |
\n'%(hunk_off2, hunk_size2))
+ output_file.write('Offset %d, %d lines modified | '%(hunk_off1, hunk_size1))
+ output_file.write('Offset %d, %d lines modified |
\n'%(hunk_off2, hunk_size2))
else:
# ⋮ - vertical ellipsis
- outputfile.write('⋮ | ⋮ |
');
+ output_file.write('⋮ | ⋮ |
')
-def add_line(s1, s2):
+def add_line(s1, s2, output_file):
global line1
global line2
- if s1==None and s2==None:
- type="unmodified"
- elif s1==None or s1=="":
- type="added"
- elif s2==None or s1=="":
- type="deleted"
- elif s1==s2:
- type="unmodified"
+ 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="changed"
- s1,s2 = linediff(s1, s2)
-
- outputfile.write(''%type)
- if s1!=None and s1!="":
- outputfile.write('%d | '%line1)
- outputfile.write('')
- outputfile.write(convert(s1, linesize=linesize, ponct=1))
- outputfile.write(' | ')
+ type_name = "changed"
+ s1, s2 = linediff(s1, s2)
+
+ output_file.write('
' % type_name)
+ if s1 != None and s1 != "":
+ output_file.write('%d | ' % line1)
+ output_file.write('')
+ output_file.write(convert(s1, linesize=linesize, ponct=1))
+ output_file.write(' | ')
else:
- s1=""
- outputfile.write(' | ')
-
- if s2!=None and s2!="":
- outputfile.write('%d | '%line2)
- outputfile.write('')
- outputfile.write(convert(s2, linesize=linesize, ponct=1))
- outputfile.write(' | ')
+ s1 = ""
+ output_file.write(' | ')
+
+ if s2 != None and s2 != "":
+ output_file.write('%d | '%line2)
+ output_file.write('')
+ output_file.write(convert(s2, linesize=linesize, ponct=1))
+ output_file.write(' | ')
else:
- s2=""
- outputfile.write(' | ')
+ s2 = ""
+ output_file.write(' | ')
- outputfile.write('
\n')
+ output_file.write('\n')
- if s1!="":
+ if s1 != "":
line1 += 1
- if s2!="":
+ if s2 != "":
line2 += 1
-def empty_buffer():
+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])
+ add_line(l[0], l[1], output_file)
elif del_cpt != 0 and add_cpt != 0:
l0, l1 = [], []
@@ -283,57 +276,58 @@ def empty_buffer():
l0.append(l[0])
if l[1] != None:
l1.append(l[1])
- max = (len(l0) > len(l1)) and len(l0) or len(l1)
- for i in range(max):
+ max_len = (len(l0) > len(l1)) and len(l0) or len(l1)
+ for i in range(max_len):
s0, s1 = "", ""
- if i