-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff2html.py
More file actions
231 lines (219 loc) · 7.98 KB
/
diff2html.py
File metadata and controls
231 lines (219 loc) · 7.98 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""
Script for convert unified diff text into html
"""
import re
separator = '*' * 3
def handleBlock(block):
"""
Turn a block of lines into a list like this
- Line 1
+ Line 2
-> [("Line 1", "Line 2")]
- Line 1
- Line 2
-> [("Line 1", ""), ("Line 2", "")]
+ Line 1
+ Line 2
-> [("Line 1", ""), ("Line 2", "")]
- Line 1
+ Line 2
+ Line 3
-> [("Line 1", "Line 2"), ("", "Line 3")]
- Line 1
- Line 2
+ Line 3
-> [("Line 1", "Line 3"), ("Line 2", "")]
"""
mlines = filter(lambda line : line.startswith('-'), block)
plines = filter(lambda line : line.startswith('+'), block)
mcount = len(mlines)
pcount = len(plines)
if mcount > pcount:
plines.extend([''] * (mcount - pcount))
elif pcount > mcount:
mlines.extend([''] * (pcount - mcount))
count = max(mcount, pcount)
return [(mlines[i],plines[i]) for i in range(count)]
def adjust(lines):
newLines = []
for line in lines:
if not line.startswith('\\ No newline at end of file'):
newLines.append(line)
return newLines
def makeBlocks(diff):
blocks = []
if len(diff) <= 0:
return
pattern = re.compile('(\d+)')
lines = diff.split('\n')
lines = adjust(lines)
num = 0
total = len(lines)
while num < total:
line = lines[num]
if line.startswith('---') or line.startswith('+++') or \
line.startswith('===') or line.lower().startswith('index') or \
line.lower().startswith('diff'):
num = num + 1
continue
if line.startswith('@@'):
# Retrieve the line number
numbers = pattern.findall(line)
if len(numbers) != 4:
# TODO: Wrong numbers?
pass
numbers = map(lambda n : int(n), numbers)
leftLine = numbers[0]
rightLine = numbers[2]
num = num + 1
if len(blocks) > 0:
blocks.append((separator, separator, separator, separator));
while num < total:
line = lines[num]
# Leave this line to outside loop
if line.startswith('@@') or line.lower().startswith('index') or line.startswith('diff'):
break
# The lines after this line will make a block
if line.startswith('-') or line.startswith('+'):
# Find the next line not start with '-' or '+'
block = [line]
num = num + 1
while num < total:
line = lines[num]
if line.startswith('-') or line.startswith('+'):
block.append(line)
num = num + 1
else:
# When breaking out, line save the current line
# This line should be appended to blocks
break
block = handleBlock(block)
for left, right in block:
if left and right:
blocks.append((str(leftLine), left, str(rightLine), right))
leftLine = leftLine + 1
rightLine = rightLine + 1
elif left:
blocks.append((str(leftLine), left, '', right))
leftLine = leftLine + 1
else:
blocks.append(('', left, str(rightLine), right))
rightLine = rightLine + 1
# end if
if num < total:
blocks.append((str(leftLine), line, str(rightLine), line))
leftLine = leftLine + 1
rightLine = rightLine + 1
num = num + 1
else: # line.startswith('@@')
num = num + 1
return blocks
bg_table = '#eeeeee'
table = '<table style="width:100%%;text-align=left;background:' + bg_table + '#eeeeee;padding:0;margin:0;border:1px;cellspacing:0;">\n%s\n</table>'
row = '<tr><td style="width:2%%;color:red;text-align:center;">%s</td><td style="width:48%%;color:%s;background:%s;">%s</td><td style="width:2%%;color:red;text-align:center;">%s</td><td style="width:48%%;color:%s;background:%s;">%s</td></tr>'
empty_row = '<tr style="background:purple;"><td style="text-align:center;"><hr/></td><td style="text-align:center;"><hr/></td><td style="text-align:center;"><hr/></td><td style="text-align:center;"><hr/></td></tr>'
color_nor = 'black'
color_mod = 'black'
color_add = 'black'
color_del = 'black'
bg_mod = 'orange'
bg_add = '#00FF80'
bg_del = 'red'
def text2html(plain):
html = plain.replace('<', '<').\
replace('>', '>').\
replace('\n', '<br/>').\
replace('\t', ' ').\
replace(' ', ' ')
return html
def makeHTML(blocks):
# blocks is None
if not blocks:
return
rows = []
for block in blocks:
# TODO: Encode block data
leftLine, left, rightLine, right = block
if leftLine.startswith(separator):
rows.append(empty_row)
continue
color_left = color_right = color_nor
bg_left = bg_right = bg_table
if left.startswith('-'):
if right.startswith('+'):
color_left = color_mod
color_right = color_add
bg_left = bg_mod
bg_right = bg_add
else:
color_left = color_mod
color_right = color_del
bg_left = bg_mod
bg_right = bg_del
else:
if right.startswith('+'):
color_right = color_add
bg_right = bg_add
bg_left = bg_mod
# Remove the first -/+
if left.startswith('-'):
left = left[1:]
if right.startswith('+'):
right = right[1:]
rows.append(row % (leftLine, color_left, bg_left, text2html(left), rightLine, color_right, bg_right, text2html(right)))
return table % '\n\t'.join(rows)
def diff2html(diff):
blocks = makeBlocks(diff)
html = makeHTML(blocks)
return html
def main():
diff = """--- a/xcommon/unit_tests/xbyte_array_tests.cpp
+++ b/xcommon/unit_tests/xbyte_array_tests.cpp
@@ -208,3 +208,14 @@ TEST(xbyte_array_tests, test_function_replace)
ASSERT_TRUE(byte_array.replace(0, &_xi16, sizeof(_xi16)));
ASSERT_EQ(byte_array.value_at<xint16_t>(0), _xi16);
}
+
+TEST(xbyte_array_tests, test_function_clear)
+{
+ xbyte_array byte_array;
+ ASSERT_TRUE(byte_array.is_empty());
+ xint16_t xi16 = -16;
+ byte_array.append(xi16);
+ ASSERT_FALSE(byte_array.is_empty());
+ byte_array.clear();
+ ASSERT_TRUE(byte_array.is_empty());
+}
"""
html = diff2html(diff)
print html
diff = """--- a/xcommon/xbyte_array.h
+++ b/xcommon/xbyte_array.h
@@ -112,6 +112,7 @@ class xbyte_array
xsize_t capacity() const { return data_.capacity(); }
void reserve(xsize_t space) { data_.reserve(space); }
const xbyte_ptr data() const { return (const xbyte_ptr)(&data_[0]); }
+ void clear() { std::vector<xbyte> none; data_.swap(none); }
xbyte_array& operator += (const xbyte_array& rhs);
xbyte_array operator + (const xbyte_array& rhs) const;
"""
html = diff2html(diff)
print html
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
filenames = sys.argv[1:]
htmls = []
for filename in filenames:
file = open(filename, 'rt')
diff = file.read()
file.close()
html = diff2html(diff)
htmls.append(html)
if htmls:
print '\n'.join(htmls)
else:
print 'Usage: diff2html.py file [file...]'
#print 'The following is an output example.'
#print '-' * 80
#main()