inline.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * "Inline" diff renderer.
  4. *
  5. * $Horde: framework/Text_Diff/Diff/Renderer/inline.php,v 1.4.10.14 2008/01/04 10:37:27 jan Exp $
  6. *
  7. * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
  8. *
  9. * See the enclosed file COPYING for license information (LGPL). If you did
  10. * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
  11. *
  12. * @author Ciprian Popovici
  13. * @package Text_Diff
  14. */
  15. /** Text_Diff_Renderer */
  16. require_once 'Text/Diff/Renderer.php';
  17. /**
  18. * "Inline" diff renderer.
  19. *
  20. * This class renders diffs in the Wiki-style "inline" format.
  21. *
  22. * @author Ciprian Popovici
  23. * @package Text_Diff
  24. */
  25. class Text_Diff_Renderer_inline extends Text_Diff_Renderer {
  26. /**
  27. * Number of leading context "lines" to preserve.
  28. */
  29. var $_leading_context_lines = 10000;
  30. /**
  31. * Number of trailing context "lines" to preserve.
  32. */
  33. var $_trailing_context_lines = 10000;
  34. /**
  35. * Prefix for inserted text.
  36. */
  37. var $_ins_prefix = '<ins>';
  38. /**
  39. * Suffix for inserted text.
  40. */
  41. var $_ins_suffix = '</ins>';
  42. /**
  43. * Prefix for deleted text.
  44. */
  45. var $_del_prefix = '<del>';
  46. /**
  47. * Suffix for deleted text.
  48. */
  49. var $_del_suffix = '</del>';
  50. /**
  51. * Header for each change block.
  52. */
  53. var $_block_header = '';
  54. /**
  55. * What are we currently splitting on? Used to recurse to show word-level
  56. * changes.
  57. */
  58. var $_split_level = 'lines';
  59. function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
  60. {
  61. return $this->_block_header;
  62. }
  63. function _startBlock($header)
  64. {
  65. return $header;
  66. }
  67. function _lines($lines, $prefix = ' ', $encode = true)
  68. {
  69. if ($encode) {
  70. array_walk($lines, array(&$this, '_encode'));
  71. }
  72. if ($this->_split_level == 'words') {
  73. return implode('', $lines);
  74. } else {
  75. return implode("\n", $lines) . "\n";
  76. }
  77. }
  78. function _added($lines)
  79. {
  80. array_walk($lines, array(&$this, '_encode'));
  81. $lines[0] = $this->_ins_prefix . $lines[0];
  82. $lines[count($lines) - 1] .= $this->_ins_suffix;
  83. return $this->_lines($lines, ' ', false);
  84. }
  85. function _deleted($lines, $words = false)
  86. {
  87. array_walk($lines, array(&$this, '_encode'));
  88. $lines[0] = $this->_del_prefix . $lines[0];
  89. $lines[count($lines) - 1] .= $this->_del_suffix;
  90. return $this->_lines($lines, ' ', false);
  91. }
  92. function _changed($orig, $final)
  93. {
  94. /* If we've already split on words, don't try to do so again - just
  95. * display. */
  96. if ($this->_split_level == 'words') {
  97. $prefix = '';
  98. while ($orig[0] !== false && $final[0] !== false &&
  99. substr($orig[0], 0, 1) == ' ' &&
  100. substr($final[0], 0, 1) == ' ') {
  101. $prefix .= substr($orig[0], 0, 1);
  102. $orig[0] = substr($orig[0], 1);
  103. $final[0] = substr($final[0], 1);
  104. }
  105. return $prefix . $this->_deleted($orig) . $this->_added($final);
  106. }
  107. $text1 = implode("\n", $orig);
  108. $text2 = implode("\n", $final);
  109. /* Non-printing newline marker. */
  110. $nl = "\0";
  111. /* We want to split on word boundaries, but we need to
  112. * preserve whitespace as well. Therefore we split on words,
  113. * but include all blocks of whitespace in the wordlist. */
  114. $diff = new Text_Diff($this->_splitOnWords($text1, $nl),
  115. $this->_splitOnWords($text2, $nl));
  116. /* Get the diff in inline format. */
  117. $renderer = new Text_Diff_Renderer_inline(array_merge($this->getParams(),
  118. array('split_level' => 'words')));
  119. /* Run the diff and get the output. */
  120. return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
  121. }
  122. function _splitOnWords($string, $newlineEscape = "\n")
  123. {
  124. // Ignore \0; otherwise the while loop will never finish.
  125. $string = str_replace("\0", '', $string);
  126. $words = array();
  127. $length = strlen($string);
  128. $pos = 0;
  129. while ($pos < $length) {
  130. // Eat a word with any preceding whitespace.
  131. $spaces = strspn(substr($string, $pos), " \n");
  132. $nextpos = strcspn(substr($string, $pos + $spaces), " \n");
  133. $words[] = str_replace("\n", $newlineEscape, substr($string, $pos, $spaces + $nextpos));
  134. $pos += $spaces + $nextpos;
  135. }
  136. return $words;
  137. }
  138. function _encode(&$string)
  139. {
  140. $string = htmlspecialchars($string);
  141. }
  142. }