README 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. Files
  2. -----
  3. finediff.php : where the FineDiff class is defined, standalone, no dependencies
  4. viewdiff-ex.php : demo page, render diff in HTML, show opcodes and other stats
  5. sample_from.txt : sample text file for demo
  6. sample_to.txt : sample text file for demo
  7. [Text_Diff] : required for viewdiff-ex.php demo page,
  8. available at http://download.pear.php.net/package/Text_Diff-1.1.1.tgz
  9. viewdiff.php : demo page, plainly render diff in HTML
  10. Demo can be seen online at:
  11. http://www.raymondhill.net/finediff/
  12. Main Class: FineDiff
  13. --------------------
  14. A class which implements a high-granularity (though selectable) diff engine.
  15. Up to character-level diffs can be computed.
  16. A diff is described by a string of opcodes, which can be stored
  17. and combined later with the left hand string to re-create the
  18. right-hand string.
  19. The code was started from scratch, with particular attention to
  20. performance. The key to the performance of the FineDiff engine is
  21. to incrementally increase the granularity.
  22. Usage
  23. -----
  24. The simplest way to create a diff of two strings is as follow:
  25. include 'finediff.php';
  26. $opcodes = FineDiff::getDiffOpcodes($from_text, $to_text /, default granularity is set to character */);
  27. // store opcodes for later use...
  28. Later, $to_text can be re-created from $from_text using $opcodes as follow:
  29. include 'finediff.php';
  30. $to_text = FineDiff::renderToTextFromOpcodes($from_text, $opcodes);
  31. If you wish a different granularity from the default one, you can use
  32. one of the provided stock granularity stacks:
  33. FineDiff::$paragraphGranularity
  34. FineDiff::$sentenceGranularity
  35. FineDiff::$wordGranularity
  36. FineDiff::$characterGranularity (default)
  37. A basic HTML renderer is provided:
  38. echo FineDiff::renderDiffToHTMLFromOpcodes($from_text, $opcodes);
  39. Customize
  40. ---------
  41. It is possible to customize the engine by providing a custom "granularity stack"
  42. at your own risk.
  43. It is also possible to provide a custom renderer through a user supplied callback
  44. function/method:
  45. FineDiff::renderFromOpcodes($from, $opcodes, $callback);
  46. FAQ
  47. ---
  48. * Does it work with UTF-8?
  49. As of now, the code assume single-byte characters. To use UTF-8 text, you can
  50. always convert the encoding using mb_convert_encoding():
  51. ...
  52. $from_text = mb_convert_encoding($from_text_utf8, 'HTML-ENTITIES', 'UTF-8');
  53. $to_text = mb_convert_encoding($to_text_utf8, 'HTML-ENTITIES', 'UTF-8');
  54. $diff_opcodes = FineDiff::getDiffOpcodes($from_text, $to_text);
  55. ...
  56. If ever you want to re-generate the $to_text_utf8 from the $from_text_utf8:
  57. ...
  58. $from_text = mb_convert_encoding($from_text_utf8, 'HTML-ENTITIES', 'UTF-8');
  59. $to_text = FineDiff::renderToTextFromOpcodes($from_text, $diff_opcodes);
  60. $to_text_utf8 = mb_convert_encoding($to_text, 'UTF-8', 'HTML-ENTITIES');
  61. ....
  62. License
  63. -------
  64. Copyright (c) 2011 Raymond Hill (http://raymondhill.net/blog/?p=441)
  65. Licensed under The MIT License
  66. Permission is hereby granted, free of charge, to any person obtaining a copy
  67. of this software and associated documentation files (the "Software"), to deal
  68. in the Software without restriction, including without limitation the rights
  69. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  70. copies of the Software, and to permit persons to whom the Software is
  71. furnished to do so, subject to the following conditions:
  72. The above copyright notice and this permission notice shall be included in
  73. all copies or substantial portions of the Software.
  74. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  75. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  76. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  77. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  78. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  79. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  80. THE SOFTWARE.