-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiff2html
executable file
·131 lines (108 loc) · 3.14 KB
/
diff2html
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
#!/bin/bash
#
# Convert diff output to colorized HTML.
# (C) Mitch Frazier, 2008-08-27
# http://www.linuxjournal.com/content/convert-diff-output-colorized-html
# Modified by stopyoukid
# https://gist.github.com/stopyoukid/5888146
# Put this in your style/css
# .wrapper {display: inline-block;margin-top: 1em;min-width: 800px;text-align: left;}
# .diffh2 {background: #fafafa;background: -moz-linear-gradient(#fafafa, #eaeaea);background: -webkit-linear-gradient(#fafafa, #eaeaea);-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr='#fafafa',endColorstr='#eaeaea')";border: 1px solid #d8d8d8;border-bottom: 0;color: #555;font: 14px sans-serif;overflow: hidden;padding: 10px 6px;text-shadow: 0 1px 0 white;margin: 0;}
# .file-diff {border: 1px solid #d8d8d8;margin-bottom: 1em;overflow: auto;padding: 0.5em 0;}
# .file-diff > div {width: 100%:}
# .diffpre {margin: 0;font-family: "Bitstream Vera Sans Mono", Courier, monospace;font-size: 12px;line-height: 1.4em;text-indent: 0.5em;}
# .difffile {color: #aaa;}
# .delete {background-color: #fdd;}
# .insert {background-color: #dfd;}
# .info {color: #a0b;}
html="<div class=\"wrapper\">"
first=1
diffseen=0
lastonly=0
currSection=''
currFile=''
function addDiffToPage {
val=$(sed 's|\t.*||' <<< $1) # remove date/time
html+="<h2 class=\".diffh2\">$val</h2>"
html+="<div class=\"file-diff\">"
html+=$2
html+="</div>"
}
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
else
if [[ $lastonly -eq 0 ]]; then
addDiffToPage $currFile $currSection
fi
fi
if [[ $lastonly -eq 0 ]]; then
currSection=""
fi
lastonly=1
elif [[ "$t4" == 'diff' ]]; then
cls='difffile'
if [[ $diffseen -eq 1 ]]; then
addDiffToPage $currFile $currSection
fi
diffseen=1
currSection=""
lastonly=0
elif [[ "$t3" == '+++' ]]; then
# --- always comes before +++
# currFile=${s#+++ */}
cls='insert'
lastonly=0
s=$(sed 's|\t.*||' <<< $s) # remove date/time
elif [[ "$t3" == '---' ]]; then
currFile=${s#--- */}
cls='delete'
lastonly=0
s=$(sed 's|\t.*||' <<< $s) # remove date/time
elif [[ "$t2" == '@@' ]]; then
cls='info'
lastonly=0
elif [[ "$t1" == '+' ]]; then
cls='insert'
lastonly=0
elif [[ "$t1" == '-' ]]; then
cls='delete'
lastonly=0
else
cls='context'
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
fi
# Output the line.
if [[ "$cls" ]]; then
currSection+='<pre class="diffpre '${cls}'">'${s}'</pre>'
else
currSection+='<pre class="diffpre">'${s}'</pre>'
fi
read -r s
done
#if [[ $diffseen -eq 1 && $onlyseen -eq 0 ]]; then
if [[ "$currSection" ]]; then
addDiffToPage $currFile $currSection
fi
html+="</div>"
echo "$html"
IFS=$OIFS