forked from mitsuhiko/htmldiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmldiff.py
285 lines (240 loc) · 9.75 KB
/
htmldiff.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- coding: utf-8 -*-
"""
htmldiff
~~~~~~~~
Diffs HTML fragments. Nice to show what changed between two revisions
of a document for an arbitrary user. Examples:
>>> from htmldiff import render_html_diff
>>> print(render_html_diff('Foo <b>bar</b> baz', 'Foo <i>bar</i> baz'))
<div class="diff">Foo <i class="tagdiff_replaced">bar</i> baz</div>
>>> print(render_html_diff('Foo bar baz', 'Foo baz'))
<div class="diff">Foo <del>bar</del> baz</div>
>>> print(render_html_diff('Foo baz', 'Foo blah baz'))
<div class="diff">Foo <ins>blah</ins> baz</div>
:copyright: (c) 2011 by Armin Ronacher, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
import re
from difflib import SequenceMatcher
from itertools import chain
from genshi.core import Stream, QName, Attrs, START, END, TEXT
from genshi.input import ET
from contextlib import contextmanager
import html5lib
_leading_space_re = re.compile(r'(?u)^(\s+)')
_diff_split_re = re.compile(r'(?u)(\s+)')
def diff_genshi_stream(old_stream, new_stream):
"""Renders a creole diff for two texts."""
differ = StreamDiffer(old_stream, new_stream)
return differ.get_diff_stream()
def render_html_diff(old, new, wrapper_element='div',
wrapper_class='diff'):
"""Renders the diff between two HTML fragments."""
old_stream = parse_html(old, wrapper_element, wrapper_class)
new_stream = parse_html(new, wrapper_element, wrapper_class)
rv = diff_genshi_stream(old_stream, new_stream)
return rv.render('html', encoding=None)
def parse_html(html, wrapper_element='div', wrapper_class='diff'):
"""Parse an HTML fragment into a Genshi stream."""
builder = html5lib.getTreeBuilder('etree')
parser = html5lib.HTMLParser(tree=builder)
tree = parser.parseFragment(html)
tree.tag = wrapper_element
if wrapper_class is not None:
tree.set('class', wrapper_class)
return ET(tree)
def longzip(a, b):
"""Like `izip` but yields `None` for missing items."""
aiter = iter(a)
biter = iter(b)
try:
for item1 in aiter:
yield item1, next(biter)
except StopIteration:
for item1 in aiter:
yield item1, None
else:
for item2 in biter:
yield None, item2
class StreamDiffer(object):
"""A class that can diff a stream of Genshi events. It will inject
``<ins>`` and ``<del>`` tags into the stream. It probably breaks
in very ugly ways if you pass a random Genshi stream to it. I'm
not exactly sure if it's correct what creoleparser is doing here,
but it appears that it's not using a namespace. That's fine with me
so the tags the `StreamDiffer` adds are also unnamespaced.
"""
def __init__(self, old_stream, new_stream):
self._old = list(old_stream)
self._new = list(new_stream)
self._result = None
self._stack = []
self._context = None
@contextmanager
def context(self, kind):
old_context = self._context
self._context = kind
try:
yield
finally:
self._context = old_context
def inject_class(self, attrs, classname):
cls = attrs.get('class')
attrs |= [(QName('class'), cls and cls + ' ' + classname or classname)]
return attrs
def append(self, type, data, pos):
self._result.append((type, data, pos))
def text_split(self, text):
worditer = chain([u''], _diff_split_re.split(text))
return [x + next(worditer) for x in worditer]
def cut_leading_space(self, s):
match = _leading_space_re.match(s)
if match is None:
return u'', s
return match.group(), s[match.end():]
def mark_text(self, pos, text, tag):
ws, text = self.cut_leading_space(text)
tag = QName(tag)
if ws:
self.append(TEXT, ws, pos)
self.append(START, (tag, Attrs()), pos)
self.append(TEXT, text, pos)
self.append(END, tag, pos)
def diff_text(self, pos, old_text, new_text):
old = self.text_split(old_text)
new = self.text_split(new_text)
matcher = SequenceMatcher(None, old, new)
def wrap(tag, words):
return self.mark_text(pos, u''.join(words), tag)
for tag, i1, i2, j1, j2 in matcher.get_opcodes():
if tag == 'replace':
wrap('del', old[i1:i2])
wrap('ins', new[j1:j2])
elif tag == 'delete':
wrap('del', old[i1:i2])
elif tag == 'insert':
wrap('ins', new[j1:j2])
else:
self.append(TEXT, u''.join(old[i1:i2]), pos)
def replace(self, old_start, old_end, new_start, new_end):
old = self._old[old_start:old_end]
new = self._new[new_start:new_end]
for idx, (old_event, new_event) in enumerate(longzip(old, new)):
if old_event is None:
self.insert(new_start + idx, new_end + idx)
break
elif new_event is None:
self.delete(old_start + idx, old_end + idx)
break
# the best case. We're in both cases dealing with the same
# event type. This is the easiest because all routines we
# have can deal with that.
if old_event[0] == new_event[0]:
type = old_event[0]
# start tags are easy. handle them first.
if type == START:
_, (tag, attrs), pos = new_event
self.enter_mark_replaced(pos, tag, attrs)
# ends in replacements are a bit tricker, we try to
# leave the new one first, then the old one. One
# should succeed.
elif type == END:
_, tag, pos = new_event
if not self.leave(pos, tag):
self.leave(pos, old_event[1])
# replaced text is internally diffed again
elif type == TEXT:
_, new_text, pos = new_event
self.diff_text(pos, old_event[1], new_text)
# for all other stuff we ignore the old event
else:
self.append(*new_event)
# ob boy, now the ugly stuff starts. Let's handle the
# easy one first. If the old event was text and the
# new one is the start or end of a tag, we just process
# both of them. The text is deleted, the rest is handled.
elif old_event[0] == TEXT and new_event[0] in (START, END):
_, text, pos = old_event
self.mark_text(pos, text, 'del')
type, data, pos = new_event
if type == START:
self.enter(pos, *data)
else:
self.leave(pos, data)
# now the case that the old stream opened or closed a tag
# that went away in the new one. In this case we just
# insert the text and totally ignore the fact that we had
# a tag. There is no way this could be rendered in a sane
# way.
elif old_event[0] in (START, END) and new_event[0] == TEXT:
_, text, pos = new_event
self.mark_text(pos, text, 'ins')
# meh. no idea how to handle that, let's just say nothing
# happened.
else:
pass
def delete(self, start, end):
with self.context('del'):
self.block_process(self._old[start:end])
def insert(self, start, end):
with self.context('ins'):
self.block_process(self._new[start:end])
def unchanged(self, start, end):
with self.context(None):
self.block_process(self._old[start:end])
def enter(self, pos, tag, attrs):
self._stack.append(tag)
self.append(START, (tag, attrs), pos)
def enter_mark_replaced(self, pos, tag, attrs):
attrs = self.inject_class(attrs, 'tagdiff_replaced')
self._stack.append(tag)
self.append(START, (tag, attrs), pos)
def leave(self, pos, tag):
if not self._stack:
return False
if tag == self._stack[-1]:
self.append(END, tag, pos)
self._stack.pop()
return True
return False
def leave_all(self):
if self._stack:
last_pos = (self._new or self._old)[-1][2]
for tag in reversed(self._stack):
self.append(END, tag, last_pos)
del self._stack[:]
def block_process(self, events):
for event in events:
type, data, pos = event
if type == START:
self.enter(pos, *data)
elif type == END:
self.leave(pos, data)
elif type == TEXT:
if self._context is not None and data.strip():
tag = QName(self._context)
self.append(START, (QName(tag), Attrs()), pos)
self.append(type, data, pos)
self.append(END, tag, pos)
else:
self.append(type, data, pos)
else:
self.append(type, data, pos)
def process(self):
self._result = []
matcher = SequenceMatcher(None, self._old, self._new)
for tag, i1, i2, j1, j2 in matcher.get_opcodes():
if tag == 'replace':
self.replace(i1, i2, j1, j2)
elif tag == 'delete':
self.delete(i1, i2)
elif tag == 'insert':
self.insert(j1, j2)
else:
self.unchanged(i1, i2)
self.leave_all()
def get_diff_stream(self):
if self._result is None:
self.process()
return Stream(self._result)