X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=diff2html.py;h=6e2751e4d7a585f73de28abd6de0fc2ba2ef96d3;hb=ad3d66724c62f87c720185bad6f6be71627339de;hp=d616a5eb43ccdae53981c3fe4bb44ca939d256d4;hpb=b8d3cbfcf53806cc2045b818109feb6d009ca515;p=diff2html.git
diff --git a/diff2html.py b/diff2html.py
index d616a5e..6e2751e 100644
--- a/diff2html.py
+++ b/diff2html.py
@@ -38,11 +38,7 @@ 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 = """
@@ -168,7 +164,6 @@ def linediff(s, t):
def convert(s, linesize=0, ponct=0):
i = 0
t = ""
- l=[]
for c in s:
# used by diffs
if c == DIFFON:
@@ -206,25 +201,23 @@ def convert(s, linesize=0, ponct=0):
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
@@ -240,26 +233,26 @@ def add_line(s1, s2):
type_name = "changed"
s1, s2 = linediff(s1, s2)
- outputfile.write('' % type_name)
+ output_file.write('
' % type_name)
if s1 != None and s1 != "":
- outputfile.write('%d | ' % line1)
- outputfile.write('')
- outputfile.write(convert(s1, linesize=linesize, ponct=1))
- outputfile.write(' | ')
+ output_file.write('%d | ' % line1)
+ output_file.write('')
+ output_file.write(convert(s1, linesize=linesize, ponct=1))
+ output_file.write(' | ')
else:
s1 = ""
- outputfile.write(' | ')
+ output_file.write(' | ')
if s2 != None and s2 != "":
- outputfile.write('%d | '%line2)
- outputfile.write('')
- outputfile.write(convert(s2, linesize=linesize, ponct=1))
- outputfile.write(' | ')
+ output_file.write('%d | '%line2)
+ output_file.write('')
+ output_file.write(convert(s2, linesize=linesize, ponct=1))
+ output_file.write(' | ')
else:
s2 = ""
- outputfile.write(' | ')
+ output_file.write(' | ')
- outputfile.write('
\n')
+ output_file.write('\n')
if s1 != "":
line1 += 1
@@ -267,14 +260,14 @@ def add_line(s1, 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 = [], []
@@ -290,50 +283,51 @@ def empty_buffer():
s0 = l0[i]
if i < len(l1):
s1 = l1[i]
- add_line(s0, s1)
+ add_line(s0, s1, output_file)
add_cpt, del_cpt = 0, 0
buf = []
-def parse_input():
- global buf, add_cpt, del_cpt
+def parse_input(input_file, output_file,
+ exclude_headers, show_hunk_infos):
+ global add_cpt, del_cpt
global line1, line2
global hunk_off1, hunk_size1, hunk_off2, hunk_size2
if not exclude_headers:
- outputfile.write(html_hdr)
- outputfile.write(table_hdr)
+ output_file.write(html_hdr)
+ output_file.write(table_hdr)
while True:
- l = inputfile.readline()
+ l = input_file.readline()
if l == "":
break
m = re.match('^--- ([^\s]*)', l)
if m:
- empty_buffer()
+ empty_buffer(output_file)
file1 = m.groups()[0]
- l = inputfile.readline()
+ l = input_file.readline()
m = re.match('^\+\+\+ ([^\s]*)', l)
if m:
file2 = m.groups()[0]
- add_filename(file1, file2)
+ add_filename(file1, file2, 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()
+ 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()
+ add_hunk(output_file, show_hunk_infos)
continue
if hunk_size1 == 0 and hunk_size2 == 0:
- empty_buffer()
- add_comment(l)
+ empty_buffer(output_file)
+ add_comment(l, output_file)
continue
if re.match("^\+", l):
@@ -349,19 +343,19 @@ def parse_input():
continue
if re.match("^\ ", l) and hunk_size1 and hunk_size2:
- empty_buffer()
+ empty_buffer(output_file)
hunk_size1 -= 1
hunk_size2 -= 1
buf.append((l[1:], l[1:]))
continue
- empty_buffer()
- add_comment(l)
+ empty_buffer(output_file)
+ add_comment(l, output_file)
- empty_buffer()
- outputfile.write(table_footer)
+ empty_buffer(output_file)
+ output_file.write(table_footer)
if not exclude_headers:
- outputfile.write(html_footer)
+ output_file.write(html_footer)
def usage():
@@ -384,8 +378,13 @@ page on stdout.
def main():
global linesize, tabsize
- global inputfile, outputfile
- global exclude_headers, show_CR, show_hunk_infos
+ global show_CR
+
+ input_file = sys.stdin
+ output_file = sys.stdout
+
+ exclude_headers = False
+ show_hunk_infos = False
try:
opts, args = getopt.getopt(sys.argv[1:], "hi:o:xt:l:rk",
@@ -396,16 +395,15 @@ def main():
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
- output = None
verbose = False
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-i", "--input"):
- inputfile = open(a, "r")
+ input_file = open(a, "r")
elif o in ("-o", "--output"):
- outputfile = open(a, "w")
+ output_file = open(a, "w")
elif o in ("-x", "--exclude-html-headers"):
exclude_headers = True
elif o in ("-t", "--tabsize"):
@@ -418,7 +416,9 @@ def main():
show_hunk_infos = True
else:
assert False, "unhandled option"
- parse_input()
+ parse_input(input_file, output_file,
+ exclude_headers, show_hunk_infos)
+
if __name__ == "__main__":
main()