3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # Transform a unified diff from stdin to a colored
19 # side-by-side HTML page on stdout.
21 # Authors: Olivier MATZ <zer0@droids-corp.org>
22 # Alan De Smet <adesmet@cs.wisc.edu>
24 # Inspired by diff2html.rb from Dave Burt <dave (at) burt.id.au>
25 # (mainly for html theme)
28 # - The sane function currently mashes non-ASCII characters to "."
29 # Instead be clever and convert to something like "xF0"
30 # (the hex value), and mark with a <span>. Even more clever:
31 # Detect if the character is "printable" for whatever definition,
32 # and display those directly.
35 import sys, re, htmlentitydefs, getopt
37 # minimum line size, we add a zero-sized breakable space every
42 outputfile = sys.stdout
43 exclude_headers = False
45 show_hunk_infos = False
48 html_hdr="""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
50 <meta name="generator" content="diff2html.rb" />
51 <title>HTML Diff</title>
53 table { border:0px; border-collapse:collapse; width: 100%; font-size:0.75em; font-family: Lucida Console, monospace }
54 td.line { color:#8080a0 }
55 th { background: black; color: white }
56 tr.diffunmodified td { background: #D0D0E0 }
57 tr.diffhunk td { background: #A0A0A0 }
58 tr.diffadded td { background: #CCFFCC }
59 tr.diffdeleted td { background: #FFCCCC }
60 tr.diffchanged td { background: #FFFFA0 }
61 span.diffchanged2 { background: #E0C880 }
62 span.diffponct { color: #B08080 }
64 tr.diffseparator td {}
86 add_cpt, del_cpt = 0,0
88 hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0,0,0,0
91 # Characters we're willing to word wrap on
98 if i not in ['\t', '\n'] and ((j < 32) or (j >= 127)):
106 s=str(reduce(lambda x,y:x+y, [ sane(c) for c in s ]))
108 t=str(reduce(lambda x,y:x+y, [ sane(c) for c in t ]))
111 d=[[(0,0) for i in range(n+1)] for i in range(m+1)]
112 x=[[(0,0) for i in range(n+1)] for i in range(m+1)]
116 for i in range(m+1)[1:]:
117 d[i][0] = (i,(i-1,0))
118 for j in range(n+1)[1:]:
119 d[0][j] = (j,(0,j-1))
121 for i in range(m+1)[1:]:
122 for j in range(n+1)[1:]:
127 d[i][j] = min((d[i-1][j][0] + 1, (i-1,j)),
128 (d[i][j-1][0] + 1, (i,j-1)),
129 (d[i-1][j-1][0] + cost, (i-1,j-1)))
133 while coord != (0,0):
143 child_val = d[cx][cy][0]
145 father_coord = d[cx][cy][1]
147 father_val = d[fx][fy][0]
149 diff = (cx-fx, cy-fy)
153 l2.append(DIFFON + t[fy] + DIFFOFF)
155 l1.append(DIFFON + s[fx] + DIFFOFF)
157 elif child_val-father_val == 1:
158 l1.append(DIFFON + s[fx] + DIFFOFF)
159 l2.append(DIFFON + t[fy] + DIFFOFF)
164 r1,r2 = (reduce(lambda x,y:x+y, l1), reduce(lambda x,y:x+y, l2))
168 def convert(s, linesize=0, ponct=0):
175 t += '<span class="diffchanged2">'
180 elif htmlentitydefs.codepoint2name.has_key(ord(c)):
181 t += "&%s;"%(htmlentitydefs.codepoint2name[ord(c)])
184 # special highlighted chars
185 elif c=="\t" and ponct==1:
186 n = tabsize-(i%tabsize)
189 t += ('<span class="diffponct">»</span>'+' '*(n-1))
190 elif c==" " and ponct==1:
191 t += '<span class="diffponct">·</span>'
192 elif c=="\n" and ponct==1:
194 t += '<span class="diffponct">\</span>'
199 if linesize and (WORDBREAK.count(c)==1):
202 if linesize and i>linesize:
210 outputfile.write('<tr class="diffmisc"><td colspan="4">%s</td></tr>\n'%convert(s))
212 def add_filename(f1, f2):
213 outputfile.write("<tr><th colspan='2'>%s</th>"%convert(f1, linesize=linesize))
214 outputfile.write("<th colspan='2'>%s</th></tr>\n"%convert(f2, linesize=linesize))
217 global hunk_off1, hunk_size1, hunk_off2, hunk_size2
218 global show_hunk_infos
220 outputfile.write('<tr class="diffhunk"><td colspan="2">Offset %d, %d lines modified</td>'%(hunk_off1, hunk_size1))
221 outputfile.write('<td colspan="2">Offset %d, %d lines modified</td></tr>\n'%(hunk_off2, hunk_size2))
223 # ⋮ - vertical ellipsis
224 outputfile.write('<tr class="diffhunk"><td colspan="2">⋮</td><td colspan="2">⋮</td></tr>');
227 def add_line(s1, s2):
231 if s1==None and s2==None:
233 elif s1==None or s1=="":
235 elif s2==None or s1=="":
241 s1,s2 = linediff(s1, s2)
243 outputfile.write('<tr class="diff%s">'%type)
244 if s1!=None and s1!="":
245 outputfile.write('<td class="diffline">%d </td>'%line1)
246 outputfile.write('<td class="diffpresent">')
247 outputfile.write(convert(s1, linesize=linesize, ponct=1))
248 outputfile.write('</td>')
251 outputfile.write('<td colspan="2"> </td>')
253 if s2!=None and s2!="":
254 outputfile.write('<td class="diffline">%d </td>'%line2)
255 outputfile.write('<td class="diffpresent">')
256 outputfile.write(convert(s2, linesize=linesize, ponct=1))
257 outputfile.write('</td>')
260 outputfile.write('<td colspan="2"></td>')
262 outputfile.write('</tr>\n')
275 if del_cpt == 0 or add_cpt == 0:
279 elif del_cpt != 0 and add_cpt != 0:
286 max = (len(l0) > len(l1)) and len(l0) or len(l1)
295 add_cpt, del_cpt = 0,0
300 global buffer, add_cpt, del_cpt
302 global hunk_off1, hunk_size1, hunk_off2, hunk_size2
304 if not exclude_headers:
305 outputfile.write(html_hdr)
306 outputfile.write(table_hdr)
309 l=inputfile.readline()
313 m=re.match('^--- ([^\s]*)', l)
317 l=inputfile.readline()
318 m=re.match('^\+\+\+ ([^\s]*)', l)
321 add_filename(file1, file2)
322 hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0,0,0,0
325 m=re.match("@@ -(\d+),?(\d*) \+(\d+),?(\d*)", l)
328 hunk_data = map(lambda x:x=="" and 1 or int(x), m.groups())
329 hunk_off1, hunk_size1, hunk_off2, hunk_size2 = hunk_data
330 line1, line2 = hunk_off1, hunk_off2
334 if hunk_size1 == 0 and hunk_size2 == 0:
339 if re.match("^\+", l):
342 buffer.append((None, l[1:]))
345 if re.match("^\-", l):
348 buffer.append((l[1:], None))
351 if re.match("^\ ", l) and hunk_size1 and hunk_size2:
355 buffer.append((l[1:], l[1:]))
362 outputfile.write(table_footer)
363 if not exclude_headers:
364 outputfile.write(html_footer)
369 diff2html.py [-i file] [-o file] [-x]
372 Transform a unified diff from stdin to a colored side-by-side HTML
375 -i file set input file, else use stdin
376 -o file set output file, else use stdout
377 -x exclude html header and footer
378 -t tabsize set tab size (default 8)
379 -l linesize set maximum line size is there is no word break (default 20)
380 -r show \\r characters
382 -h show help and exit
386 global linesize, tabsize
387 global inputfile, outputfile
388 global exclude_headers, show_CR, show_hunk_infos
391 opts, args = getopt.getopt(sys.argv[1:], "hi:o:xt:l:rk",
392 ["help", "input=", "output=",
393 "exclude-html-headers", "tabsize=",
394 "linesize=", "show-cr", "show-hunk-infos"])
395 except getopt.GetoptError, err:
396 print str(err) # will print something like "option -a not recognized"
402 if o in ("-h", "--help"):
405 elif o in ("-i", "--input"):
406 inputfile = open(a, "r")
407 elif o in ("-o", "--output"):
408 outputfile = open(a, "w")
409 elif o in ("-x", "--exclude-html-headers"):
410 exclude_headers = True
411 elif o in ("-t", "--tabsize"):
413 elif o in ("-l", "--linesize"):
415 elif o in ("-r", "--show-cr"):
417 elif o in ("-k", "--show-hunk-infos"):
418 show_hunk_infos = True
420 assert False, "unhandled option"
423 if __name__ == "__main__":