X-Git-Url: http://git.droids-corp.org/?p=diff2html.git;a=blobdiff_plain;f=diff2html.py;h=14094de32ce11d54b9f7c2daf7515a0e68cce2ed;hp=59d445dbf9cdf9dfd655a6bacf881feafd56c7bf;hb=d5d43cb0e52cacb457e5c96329e18bf54a2e0ea5;hpb=715a85a7ac2a121d30b421b128cb96533c3001b2 diff --git a/diff2html.py b/diff2html.py index 59d445d..14094de 100644 --- a/diff2html.py +++ b/diff2html.py @@ -18,23 +18,12 @@ # Transform a unified diff from stdin to a colored # side-by-side HTML page on stdout. # -# Author: Olivier MATZ +# Authors: Olivier MATZ +# Alan De Smet # # Inspired by diff2html.rb from Dave Burt # (mainly for html theme) # -# Changed by Alan De Smet 2009-01-25 -# - Remove headers and footers that are undesirable for for CVSTrac -# integration. -# - Change style sheet to own preferences -# - Adjust LINESIZE code to reset counter and allow breaks whenever -# WORDBREAK characters are encountered. -# - Don't display offset info (it's redundant with the line numbers) -# instead show vertical ellipsis -# - If part of a "change" is actually blank, it's an addition or -# deletion, not a "change" where the entire line changed. -# - Don't display "\" for end of line; unnecessary noise. - # TODO: # - The sane function currently mashes non-ASCII characters to "." # Instead be clever and convert to something like "xF0" @@ -43,28 +32,61 @@ # and display those directly. -import sys, re, htmlentitydefs +import sys, re, htmlentitydefs, getopt -html_hdr=""" - +# minimum line size, we add a zero-sized breakable space every +# 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 Diff + + + """ html_footer=""" + +""" + +table_hdr=""" +
+""" + +table_footer="""
""" DIFFON="\x01" DIFFOFF="\x02" -buffer=[] +buf=[] add_cpt, del_cpt = 0,0 line1, line2 = 0,0 hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0,0,0,0 -# minimum line size, we add a zero-sized breakable space every -# LINESIZE characters -LINESIZE=20 -TAB=4 # Characters we're willing to word wrap on WORDBREAK=" \t;.,/):" @@ -161,15 +183,15 @@ def convert(s, linesize=0, ponct=0): # special highlighted chars elif c=="\t" and ponct==1: - n = TAB-(i%TAB) + n = tabsize-(i%tabsize) if n==0: - n=TAB + n=tabsize t += ('»'+' '*(n-1)) elif c==" " and ponct==1: t += '·' elif c=="\n" and ponct==1: - 1 # Quietly drop it. - #t += '\' + if show_CR: + t += '\' else: t += c i += 1 @@ -185,22 +207,21 @@ def convert(s, linesize=0, ponct=0): def add_comment(s): - sys.stdout.write('%s\n'%convert(s)) + outputfile.write('%s\n'%convert(s)) def add_filename(f1, f2): - sys.stdout.write("%s"%convert(f1, linesize=LINESIZE)) - sys.stdout.write("%s\n"%convert(f2, linesize=LINESIZE)) + outputfile.write("%s"%convert(f1, linesize=linesize)) + outputfile.write("%s\n"%convert(f2, linesize=linesize)) def add_hunk(): - global hunk_off1 - global hunk_size1 - global hunk_off2 - global hunk_size2 - # Don't bother displaying, it's redundant with the line numbers. - #sys.stdout.write('Offset %d, %d lines modified'%(hunk_off1, hunk_size1)) - #sys.stdout.write('Offset %d, %d lines modified\n'%(hunk_off2, hunk_size2)) - # ⋮ - vertical ellipsis - sys.stdout.write('⋮⋮'); + global hunk_off1, hunk_size1, hunk_off2, hunk_size2 + global 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)) + else: + # ⋮ - vertical ellipsis + outputfile.write('⋮⋮'); def add_line(s1, s2): @@ -208,37 +229,37 @@ def add_line(s1, s2): global line2 if s1==None and s2==None: - type="unmodified" + type_name="unmodified" elif s1==None or s1=="": - type="added" + type_name="added" elif s2==None or s1=="": - type="deleted" + type_name="deleted" elif s1==s2: - type="unmodified" + type_name="unmodified" else: - type="changed" + type_name="changed" s1,s2 = linediff(s1, s2) - sys.stdout.write(''%type) + outputfile.write(''%type_name) if s1!=None and s1!="": - sys.stdout.write('%d '%line1) - sys.stdout.write('') - sys.stdout.write(convert(s1, linesize=LINESIZE, ponct=1)) - sys.stdout.write('') + outputfile.write('%d '%line1) + outputfile.write('') + outputfile.write(convert(s1, linesize=linesize, ponct=1)) + outputfile.write('') else: s1="" - sys.stdout.write(' ') + outputfile.write(' ') if s2!=None and s2!="": - sys.stdout.write('%d '%line2) - sys.stdout.write('') - sys.stdout.write(convert(s2, linesize=LINESIZE, ponct=1)) - sys.stdout.write('') + outputfile.write('%d '%line2) + outputfile.write('') + outputfile.write(convert(s2, linesize=linesize, ponct=1)) + outputfile.write('') else: s2="" - sys.stdout.write('') + outputfile.write('') - sys.stdout.write('\n') + outputfile.write('\n') if s1!="": line1 += 1 @@ -247,23 +268,23 @@ def add_line(s1, s2): def empty_buffer(): - global buffer + global buf global add_cpt global del_cpt if del_cpt == 0 or add_cpt == 0: - for l in buffer: + for l in buf: add_line(l[0], l[1]) elif del_cpt != 0 and add_cpt != 0: l0, l1 = [], [] - for l in buffer: + for l in buf: if l[0] != None: 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