forked from arnab/jQuery.PrettyTextDiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.pretty-text-diff.coffee
61 lines (50 loc) · 1.96 KB
/
jquery.pretty-text-diff.coffee
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
###
@preserve jQuery.PrettyTextDiff 1.0.4
See https://github.com/arnab/jQuery.PrettyTextDiff/
###
$ = jQuery
$.fn.extend
prettyTextDiff: (options) ->
# Defaults
settings =
originalContainer: ".original",
changedContainer: ".changed",
diffContainer: ".diff",
cleanup: true
debug: false
settings = $.extend settings, options
$.fn.prettyTextDiff.debug "Options: ", settings, settings
dmp = new diff_match_patch();
@each ->
if settings.originalContent and settings.changedContent
original = $('<div />').html(settings.originalContent).text()
changed = $('<div />').html(settings.changedContent).text()
else
original = $(settings.originalContainer, this).text()
changed = $(settings.changedContainer, this).text()
$.fn.prettyTextDiff.debug "Original text found: ", original, settings
$.fn.prettyTextDiff.debug "Changed text found: ", changed, settings
diffs = dmp.diff_main(original, changed)
dmp.diff_cleanupSemantic(diffs) if settings.cleanup
$.fn.prettyTextDiff.debug "Diffs: ", diffs, settings
diff_as_html = $.map(diffs, (diff) ->
$.fn.prettyTextDiff.createHTML(diff))
$(settings.diffContainer, this).html(diff_as_html.join(''));
@
$.fn.prettyTextDiff.debug = (message, object, settings) ->
console.log(message, object) if settings.debug
$.fn.prettyTextDiff.createHTML = (diff) ->
html = [];
pattern_amp = /&/g
pattern_lt = /</g
pattern_gt = />/g
pattern_para = /\n/g
[operation, data] = diff
text = data.replace(pattern_amp, '&') \
.replace(pattern_lt, '<') \
.replace(pattern_gt, '>') \
.replace(pattern_para, '<br>')
switch operation
when DIFF_INSERT then '<ins>' + text + '</ins>'
when DIFF_DELETE then '<del>' + text + '</del>'
when DIFF_EQUAL then '<span>' + text + '</span>'