Remove functions which are not used in the script, these are actually part of the...
[project-aon.git] / common / scripts / diff2html.sh
1 #!/bin/bash
2 #
3 # Convert diff output to colorized HTML.
4 #
5 # (http://www.linuxjournal.com/content/convert-diff-output-colorized-html)
6
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>
25 XX
26
27 echo -n '<span class="comment">'
28
29 first=1
30 diffseen=0
31 lastonly=0
32
33 OIFS=$IFS
34 IFS='
35 '
36
37 # The -r option keeps the backslash from being an escape char.
38 read -r s
39
40 while [[ $? -eq 0 ]]
41 do
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}
49
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>"
59             fi
60         fi
61         if [[ $lastonly -eq 0 ]]; then
62             echo "<div class='diffdiv'>"
63         fi
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>"
72         fi
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
93     fi
94
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
101     fi
102
103     # Output the line.
104     if [[ "$cls" ]]; then
105         echo -n '<span class="'${cls}'">'${s}'</span>'
106     else
107         echo -n ${s}
108     fi
109     read -r s
110 done
111 IFS=$OIFS
112
113 if [[ $diffseen -eq 0  &&  $onlyseen -eq 0 ]]; then 
114     echo -n '</span>'
115 else
116     echo "</div>"
117 fi
118 echo
119
120 cat <<XX
121 </pre>
122 </body>
123 </html>
124 XX