updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / diff2colorhtml / diff2colorhtml.sh
blob7e5601a4d1e8577d0946bdbff0ac38ac8935ccba
1 #!/bin/bash
3 # Convert diff output to colorized HTML.
4 # http://www.linuxjournal.com/content/convert-diff-output-colorized-html
5 # usage: diff -u v1.c v2.c | diff2html >v1-v2.html
7 cat <<XX
8 <html>
9 <head>
10 <title>Colorized Diff</title>
11 </head>
12 <style>
13 .diffdiv { border: solid 1px black; }
14 .comment { color: gray; }
15 .diff { color: #8A2BE2; }
16 .minus3 { color: blue; }
17 .plus3 { color: maroon; }
18 .at2 { color: lime; }
19 .plus { color: green; background: #E7E7E7; }
20 .minus { color: red; background: #D7D7D7; }
21 .only { color: purple; }
22 </style>
23 <body>
24 <pre>
27 echo -n '<span class="comment">'
29 first=1
30 diffseen=0
31 lastonly=0
33 OIFS=$IFS
34 IFS='
37 # The -r option keeps the backslash from being an escape char.
38 read -r s
40 while [[ $? -eq 0 ]]
42 # Get beginning of line to determine what type
43 # of diff line it is.
44 t1=${s:0:1}
45 t2=${s:0:2}
46 t3=${s:0:3}
47 t4=${s:0:4}
48 t7=${s:0:7}
50 # Determine HTML class to use.
51 if [[ "$t7" == 'Only in' ]]; then
52 cls='only'
53 if [[ $diffseen -eq 0 ]]; then
54 diffseen=1
55 echo -n '</span>'
56 else
57 if [[ $lastonly -eq 0 ]]; then
58 echo "</div>"
61 if [[ $lastonly -eq 0 ]]; then
62 echo "<div class='diffdiv'>"
64 lastonly=1
65 elif [[ "$t4" == 'diff' ]]; then
66 cls='diff'
67 if [[ $diffseen -eq 0 ]]; then
68 diffseen=1
69 echo -n '</span>'
70 else
71 echo "</div>"
73 echo "<div class='diffdiv'>"
74 lastonly=0
75 elif [[ "$t3" == '+++' ]]; then
76 cls='plus3'
77 lastonly=0
78 elif [[ "$t3" == '---' ]]; then
79 cls='minus3'
80 lastonly=0
81 elif [[ "$t2" == '@@' ]]; then
82 cls='at2'
83 lastonly=0
84 elif [[ "$t1" == '+' ]]; then
85 cls='plus'
86 lastonly=0
87 elif [[ "$t1" == '-' ]]; then
88 cls='minus'
89 lastonly=0
90 else
91 cls=
92 lastonly=0
95 # Convert &, <, > to HTML entities.
96 s=$(sed -e 's/\&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' <<<"$s")
97 if [[ $first -eq 1 ]]; then
98 first=0
99 else
100 echo
103 # Output the line.
104 if [[ "$cls" ]]; then
105 echo -n '<span class="'${cls}'">'${s}'</span>'
106 else
107 echo -n ${s}
109 read -r s
110 done
111 IFS=$OIFS
113 if [[ $diffseen -eq 0 && $onlyseen -eq 0 ]]; then
114 echo -n '</span>'
115 else
116 echo "</div>"
118 echo
120 cat <<XX
121 </pre>
122 </body>
123 </html>
126 # vim: tabstop=4: shiftwidth=4: noexpandtab:
127 # kate: tab-width 4; indent-width 4; replace-tabs false;