-
Notifications
You must be signed in to change notification settings - Fork 8
/
diff2html.sh
executable file
·135 lines (114 loc) · 2.36 KB
/
diff2html.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
#
# Convert diff output to colorized HTML.
# (C) Mitch Frazier, 2008-08-27
# https://web.archive.org/web/20190828131321/https://www.linuxjournal.com/content/convert-diff-output-colorized-html
#
cat <<XX
<html>
<head>
<title>Colorized Diff</title>
</head>
<style>
.diffdiv { border: solid 1px black; }
.comment { color: gray; }
.diff { color: #8A2BE2; }
.minus3 { color: blue; }
.plus3 { color: maroon; }
.at2 { color: lime; }
.plus { color: green; background: #E7E7E7; }
.minus { color: red; background: #D7D7D7; }
.only { color: purple; }
</style>
<body>
<pre>
XX
printf '<span class="comment">'
first=1
diffseen=0
lastonly=0
OIFS=$IFS
IFS='
'
# The -r option keeps the backslash from being an escape char.
read -r s
while [ $? -eq 0 ]; do
# Get beginning of line to determine what type of diff line it is.
t1=${s:0:1}
t2=${s:0:2}
t3=${s:0:3}
t4=${s:0:4}
t7=${s:0:7}
# Determine HTML class to use.
if [ "$t7" = 'Only in' ]; then
cls='only'
if [ $diffseen -eq 0 ]; then
diffseen=1
printf '</span>'
else
if [ $lastonly -eq 0 ]; then
echo "</div>"
fi
fi
if [ $lastonly -eq 0 ]; then
echo "<div class='diffdiv'>"
fi
lastonly=1
elif [ "$t4" = 'diff' ]; then
cls='diff'
if [ $diffseen -eq 0 ]; then
diffseen=1
printf '</span>'
else
echo "</div>"
fi
echo "<div class='diffdiv'>"
lastonly=0
elif [ "$t3" = '+++' ]; then
cls='plus3'
lastonly=0
elif [ "$t3" = '---' ]; then
cls='minus3'
lastonly=0
elif [ "$t2" = '@@' ]; then
cls='at2'
lastonly=0
elif [ "$t1" = '+' ]; then
cls='plus'
lastonly=0
elif [ "$t1" = '-' ]; then
cls='minus'
lastonly=0
else
cls=
lastonly=0
fi
# Convert &, <, > to HTML entities.
s=$(sed -e 's/\&/\&/g' -e 's/</\</g' -e 's/>/\>/g' <<<"$s")
if [ $first -eq 1 ]; then
first=0
else
echo
fi
# Output the line.
if [ "$cls" ]; then
printf '<span class="'${cls}'">'"${s}"'</span>'
else
printf "${s}"
fi
read -r s
done
IFS=$OIFS
if [ $diffseen -eq 0 ] && [ "$onlyseen" -eq 0 ]; then
printf '</span>'
else
echo "</div>"
fi
echo
cat <<XX
</pre>
</body>
</html>
XX
# vim: tabstop=4: shiftwidth=4: noexpandtab:
# kate: tab-width 4; indent-width 4; replace-tabs false;