| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| 2 |
<HTML> |
| 3 |
<HEAD> |
| 4 |
<TITLE>Diff, Match and Patch: Demo of Patch</TITLE> |
| 5 |
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript" SRC="../javascript/diff_match_patch.js"></SCRIPT> |
| 6 |
</HEAD> |
| 7 |
|
| 8 |
<BODY> |
| 9 |
<H1>Diff, Match and Patch</H1> |
| 10 |
<H2>Demo of Patch</H2> |
| 11 |
|
| 12 |
<P>Two texts can be diffed against each other, generating a list of patches. |
| 13 |
These patches can then be applied against a third text. If the third text has edits of its own, this version of patch |
| 14 |
will apply its changes on a best-effort basis, reporting which patches succeeded and which failed.</P> |
| 15 |
|
| 16 |
<P>In this scenario Shakespeare wrote Hamlet in old English, the source document. Then two derivative |
| 17 |
works were created. One is Hamlet updated to modern English. The other is a Star Trek parody in old English. |
| 18 |
This demonstrantion creates a list of patches between the source and the modern English version. Then it |
| 19 |
applies those patches onto the Star Trek parody, thus creating a Star Trek parody in |
| 20 |
modern English.</P> |
| 21 |
|
| 22 |
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript"> |
| 23 |
var dmp = new diff_match_patch(); |
| 24 |
|
| 25 |
var patch_text = ''; |
| 26 |
|
| 27 |
function diff_launch() { |
| 28 |
var text1 = document.getElementById('text1a').value; |
| 29 |
var text2 = document.getElementById('text2a').value; |
| 30 |
|
| 31 |
var ms_start = (new Date).getTime(); |
| 32 |
var diff = dmp.diff_main(text1, text2, true); |
| 33 |
var ms_end = (new Date).getTime(); |
| 34 |
|
| 35 |
if (diff.length > 2) { |
| 36 |
dmp.diff_cleanupSemantic(diff); |
| 37 |
} |
| 38 |
|
| 39 |
var patch_list = dmp.patch_make(text1, text2, diff); |
| 40 |
patch_text = dmp.patch_toText(patch_list); |
| 41 |
|
| 42 |
document.getElementById('diffdatediv').innerHTML = |
| 43 |
'Time: ' + (ms_end - ms_start) / 1000 + 's'; |
| 44 |
document.getElementById('diffoutputdiv').innerHTML = |
| 45 |
'<FIELDSET><LEGEND>Patch:</' + 'LEGEND><PRE>' + patch_text + |
| 46 |
'</' + 'PRE></' + 'FIELDSET>'; |
| 47 |
//document.getElementById('diffoutputdiv').innerHTML = dmp.diff_prettyHtml(diff); |
| 48 |
document.getElementById('patchbutton').disabled = false; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
function patch_launch() { |
| 53 |
var text1 = document.getElementById('text1b').value; |
| 54 |
var patches = dmp.patch_fromText(patch_text); |
| 55 |
|
| 56 |
var ms_start = (new Date).getTime(); |
| 57 |
var results = dmp.patch_apply(patches, text1); |
| 58 |
var ms_end = (new Date).getTime(); |
| 59 |
|
| 60 |
document.getElementById('patchdatediv').innerHTML = |
| 61 |
'Time: ' + (ms_end - ms_start) / 1000 + 's'; |
| 62 |
document.getElementById('text2b').value = results[0]; |
| 63 |
results = results[1]; |
| 64 |
var html = ''; |
| 65 |
for (var x = 0; x < results.length; x++) { |
| 66 |
if (results[x]) { |
| 67 |
html += '<LI><FONT COLOR="#009900">Ok</' + 'FONT>'; |
| 68 |
} else { |
| 69 |
html += '<LI><FONT COLOR="#990000">Fail</' + 'FONT>'; |
| 70 |
} |
| 71 |
} |
| 72 |
document.getElementById('passfaildiv').innerHTML = html; |
| 73 |
} |
| 74 |
</SCRIPT> |
| 75 |
|
| 76 |
<FORM action="#" onsubmit="return false"> |
| 77 |
<H3>Shakespeare's copy:</H3> |
| 78 |
<TABLE WIDTH="100%"><TR> |
| 79 |
<TD WIDTH="50%">Old Version:<BR><TEXTAREA ID="text1a" STYLE="width: 100%" ROWS=10>Hamlet: Do you see yonder cloud that's almost in shape of a camel? |
| 80 |
Polonius: By the mass, and 'tis like a camel, indeed. |
| 81 |
Hamlet: Methinks it is like a weasel. |
| 82 |
Polonius: It is backed like a weasel. |
| 83 |
Hamlet: Or like a whale? |
| 84 |
Polonius: Very like a whale. |
| 85 |
-- Shakespeare</TEXTAREA></TD> |
| 86 |
<TD WIDTH="50%">New Version:<BR><TEXTAREA ID="text2a" STYLE="width: 100%" ROWS=10>Hamlet: Do you see the cloud over there that's almost the shape of a camel? |
| 87 |
Polonius: By golly, it is like a camel, indeed. |
| 88 |
Hamlet: I think it looks like a weasel. |
| 89 |
Polonius: It is shaped like a weasel. |
| 90 |
Hamlet: Or like a whale? |
| 91 |
Polonius: It's totally like a whale. |
| 92 |
-- Shakespeare</TEXTAREA></TD> |
| 93 |
</TR></TABLE> |
| 94 |
<P><INPUT TYPE="button" onClick="diff_launch()" VALUE="Compute Patch"></P> |
| 95 |
<BLOCKQUOTE><DIV ID="diffoutputdiv"></DIV></BLOCKQUOTE> |
| 96 |
|
| 97 |
<DIV ID="diffdatediv"></DIV> |
| 98 |
|
| 99 |
<H3>Trekkie's copy:</H3> |
| 100 |
<TABLE WIDTH="100%"><TR> |
| 101 |
<TD WIDTH="50%">Old Version:<BR><TEXTAREA ID="text1b" STYLE="width: 100%" ROWS=10>Kirk: Do you see yonder cloud that's almost in shape of a Klingon? |
| 102 |
Spock: By the mass, and 'tis like a Klingon, indeed. |
| 103 |
Kirk: Methinks it is like a Vulcan. |
| 104 |
Spock: It is backed like a Vulcan. |
| 105 |
Kirk: Or like a Romulan? |
| 106 |
Spock: Very like a Romulan. |
| 107 |
-- Trekkie</TEXTAREA></TD> |
| 108 |
<TD WIDTH="50%">New Version:<BR><TEXTAREA READONLY ID="text2b" STYLE="width: 100%" ROWS=10></TEXTAREA></TD> |
| 109 |
</TR></TABLE> |
| 110 |
<INPUT TYPE="button" ID="patchbutton" onClick="patch_launch()" VALUE="Apply Patch" DISABLED> |
| 111 |
</FORM> |
| 112 |
|
| 113 |
<OL ID="passfaildiv"></OL> |
| 114 |
<DIV ID="patchdatediv"></DIV> |
| 115 |
|
| 116 |
<HR> |
| 117 |
Back to <A HREF="http://code.google.com/p/google-diff-match-patch/">Diff, Match and Patch</A> |
| 118 |
|
| 119 |
</BODY> |
| 120 |
</HTML> |
| 121 |
|