Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
jQuery.PrettyTextDiff/jquery.pretty-text-diff.coffee
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
61 lines (50 sloc)
1.96 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
@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>' |