forked from zacker330/psydiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlize.py
192 lines (141 loc) · 4.45 KB
/
htmlize.py
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
#-------------------------------------------------------------
# HTML generation
#-------------------------------------------------------------
import os
from parameters import *
from ast import *
from utils import *
#-------------------- types and utilities ----------------------
class Tag:
def __init__(self, tag, idx, start=-1):
self.tag = tag
self.idx = idx
self.start = start
def __repr__(self):
return "tag:" + str(self.tag) + ":" + str(self.idx)
# escape for HTML
def escape(s):
s = s.replace('"', '"')
s = s.replace("'", ''')
s = s.replace("<", '<')
s = s.replace(">", '>')
return s
uid_count = -1
uid_hash = {}
def clear_uid():
global uid_count, uid_hash
uid_count = -1
uid_hash = {}
def uid(node):
if node in uid_hash:
return uid_hash[node]
global uid_count
uid_count += 1
uid_hash[node] = str(uid_count)
return str(uid_count)
def html_header():
install_path = get_install_path()
js_filename = ''.join([install_path, 'nav.js'])
js_file = open(js_filename, 'r')
js_text = js_file.read()
js_file.close()
css_filename = ''.join([install_path, 'diff.css'])
css_file = open(css_filename, 'r')
css_text = css_file.read()
css_file.close()
out = []
out.append('<html>\n')
out.append('<head>\n')
out.append('<META http-equiv="Content-Type" content="text/html; charset=utf-8">\n')
out.append('<style>\n')
out.append(css_text)
out.append('\n</style>\n')
out.append('<script type="text/javascript">\n')
out.append(js_text)
out.append('\n</script>\n')
out.append('</head>\n')
out.append('<body>\n')
return ''.join(out)
def html_footer():
out = []
out.append('</body>\n')
out.append('</html>\n')
return ''.join(out)
def write_html(text, side):
out = []
out.append('<div id="' + side + '" class="src">')
out.append('<pre>')
if side == 'left':
out.append('<a id="leftstart" tid="rightstart"></a>')
else:
out.append('<a id="rightstart" tid="leftstart"></a>')
out.append(text)
out.append('</pre>')
out.append('</div>')
return ''.join(out)
def htmlize(changes, file1, file2, text1, text2):
tags1 = change_tags(changes, 'left')
tags2 = change_tags(changes, 'right')
tagged_text1 = apply_tags(text1, tags1)
tagged_text2 = apply_tags(text2, tags2)
outname = base_name(file1) + '-' + base_name(file2) + '.html'
outfile = open(outname, 'w')
outfile.write(html_header())
outfile.write(write_html(tagged_text1, 'left'))
outfile.write(write_html(tagged_text2, 'right'))
outfile.write(html_footer())
outfile.close()
# put the tags generated by change_tags into the text and create HTML
def apply_tags(s, tags):
tags = sorted(tags, key = lambda t: (t.idx, -t.start))
curr = 0
out = []
for t in tags:
while curr < t.idx and curr < len(s):
out.append(escape(s[curr]))
curr += 1
out.append(t.tag)
while curr < len(s):
out.append(escape(s[curr]))
curr += 1
return ''.join(out)
#--------------------- tag generation functions ----------------------
def change_tags(changes, side):
tags = []
for c in changes:
key = c.orig if side == 'left' else c.cur
if hasattr(key, 'lineno'):
start = node_start(key)
end = node_end(key)
if c.orig != None and c.cur != None:
# <a ...> for change and move
tags.append(Tag(link_start(c, side), start))
tags.append(Tag("</a>", end, start))
else:
# <span ...> for deletion and insertion
tags.append(Tag(span_start(c), start))
tags.append(Tag('</span>', end, start))
return tags
def change_class(change):
if (change.cur == None):
return 'd'
elif (change.orig == None):
return 'i'
elif (change.cost > 0):
return 'c'
else:
return 'u'
def span_start(change):
return '<span class=' + qs(change_class(change)) + '>'
def link_start(change, side):
cls = change_class(change)
if side == 'left':
me, other = change.orig, change.cur
else:
me, other = change.cur, change.orig
return ('<a id=' + qs(uid(me)) +
' tid=' + qs(uid(other)) +
' class=' + qs(cls) +
'>')
def qs(s):
return "'" + s + "'"