inline.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * "Inline" diff renderer.
  4. *
  5. * $Horde: framework/Text_Diff/Diff/Renderer/inline.php,v 1.4.10.16 2009/07/24 13:25:29 jan Exp $
  6. *
  7. * Copyright 2004-2009 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. /**
  16. * "Inline" diff renderer.
  17. *
  18. * This class renders diffs in the Wiki-style "inline" format.
  19. *
  20. * @author Ciprian Popovici
  21. * @package Text_Diff
  22. */
  23. class Text_Diff_Renderer_inline extends Text_Diff_Renderer {
  24. /**
  25. * Number of leading context "lines" to preserve.
  26. */
  27. var $_leading_context_lines = 10000;
  28. /**
  29. * Number of trailing context "lines" to preserve.
  30. */
  31. var $_trailing_context_lines = 10000;
  32. /**
  33. * Prefix for inserted text.
  34. */
  35. var $_ins_prefix = '<ins>';
  36. /**
  37. * Suffix for inserted text.
  38. */
  39. var $_ins_suffix = '</ins>';
  40. /**
  41. * Prefix for deleted text.
  42. */
  43. var $_del_prefix = '<del>';
  44. /**
  45. * Suffix for deleted text.
  46. */
  47. var $_del_suffix = '</del>';
  48. /**
  49. * Header for each change block.
  50. */
  51. var $_block_header = '';
  52. /**
  53. * What are we currently splitting on? Used to recurse to show word-level
  54. * changes.
  55. */
  56. var $_split_level = 'lines';
  57. function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
  58. {
  59. return $this->_block_header;
  60. }
  61. function _startBlock($header)
  62. {
  63. return $header;
  64. }
  65. function _lines($lines, $prefix = ' ', $encode = true)
  66. {
  67. if ($encode) {
  68. array_walk($lines, array(&$this, '_encode'));
  69. }
  70. if ($this->_split_level == 'words') {
  71. return implode('', $lines);
  72. } else {
  73. return implode("\n", $lines) . "\n";
  74. }
  75. }
  76. function _added($lines)
  77. {
  78. array_walk($lines, array(&$this, '_encode'));
  79. $lines[0] = $this->_ins_prefix . $lines[0];
  80. $lines[count($lines) - 1] .= $this->_ins_suffix;
  81. return $this->_lines($lines, ' ', false);
  82. }
  83. function _deleted($lines, $words = false)
  84. {
  85. array_walk($lines, array(&$this, '_encode'));
  86. $lines[0] = $this->_del_prefix . $lines[0];
  87. $lines[count($lines) - 1] .= $this->_del_suffix;
  88. return $this->_lines($lines, ' ', false);
  89. }
  90. function _changed($orig, $final)
  91. {
  92. /* If we've already split on words, don't try to do so again - just
  93. * display. */
  94. if ($this->_split_level == 'words') {
  95. $prefix = '';
  96. while ($orig[0] !== false && $final[0] !== false &&
  97. substr($orig[0], 0, 1) == ' ' &&
  98. substr($final[0], 0, 1) == ' ') {
  99. $prefix .= substr($orig[0], 0, 1);
  100. $orig[0] = substr($orig[0], 1);
  101. $final[0] = substr($final[0], 1);
  102. }
  103. return $prefix . $this->_deleted($orig) . $this->_added($final);
  104. }
  105. $text1 = implode("\n", $orig);
  106. $text2 = implode("\n", $final);
  107. /* Non-printing newline marker. */
  108. $nl = "\0";
  109. /* We want to split on word boundaries, but we need to
  110. * preserve whitespace as well. Therefore we split on words,
  111. * but include all blocks of whitespace in the wordlist. */
  112. $diff = new Text_Diff('native',
  113. array($this->_splitOnWords($text1, $nl),
  114. $this->_splitOnWords($text2, $nl)));
  115. /* Get the diff in inline format. */
  116. $renderer = new Text_Diff_Renderer_inline
  117. (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. }