PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/Horde/Text/Diff/Renderer/Inline.php

https://gitlab.com/noirscape/myimouto
PHP | 193 lines | 98 code | 26 blank | 69 comment | 15 complexity | afe73db3cc1b7eff28ee0587ee6a9d60 MD5 | raw file
  1. <?php
  2. /**
  3. * "Inline" diff renderer.
  4. *
  5. * This class renders diffs in the Wiki-style "inline" format.
  6. *
  7. * Copyright 2004-2012 Horde LLC (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://www.horde.org/licenses/lgpl21.
  11. *
  12. * @author Ciprian Popovici
  13. * @package Text_Diff
  14. */
  15. class Horde_Text_Diff_Renderer_Inline extends Horde_Text_Diff_Renderer
  16. {
  17. /**
  18. * Number of leading context "lines" to preserve.
  19. *
  20. * @var integer
  21. */
  22. protected $_leading_context_lines = 10000;
  23. /**
  24. * Number of trailing context "lines" to preserve.
  25. *
  26. * @var integer
  27. */
  28. protected $_trailing_context_lines = 10000;
  29. /**
  30. * Prefix for inserted text.
  31. *
  32. * @var string
  33. */
  34. protected $_ins_prefix = '<ins>';
  35. /**
  36. * Suffix for inserted text.
  37. *
  38. * @var string
  39. */
  40. protected $_ins_suffix = '</ins>';
  41. /**
  42. * Prefix for deleted text.
  43. *
  44. * @var string
  45. */
  46. protected $_del_prefix = '<del>';
  47. /**
  48. * Suffix for deleted text.
  49. *
  50. * @var string
  51. */
  52. protected $_del_suffix = '</del>';
  53. /**
  54. * Header for each change block.
  55. *
  56. * @var string
  57. */
  58. protected $_block_header = '';
  59. /**
  60. * Whether to split down to character-level.
  61. *
  62. * @var boolean
  63. */
  64. protected $_split_characters = false;
  65. /**
  66. * What are we currently splitting on? Used to recurse to show word-level
  67. * or character-level changes.
  68. *
  69. * @var string
  70. */
  71. protected $_split_level = 'lines';
  72. protected function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
  73. {
  74. return $this->_block_header;
  75. }
  76. protected function _startBlock($header)
  77. {
  78. return $header;
  79. }
  80. protected function _lines($lines, $prefix = ' ', $encode = true)
  81. {
  82. if ($encode) {
  83. array_walk($lines, array(&$this, '_encode'));
  84. }
  85. if ($this->_split_level == 'lines') {
  86. return implode("\n", $lines) . "\n";
  87. } else {
  88. return implode('', $lines);
  89. }
  90. }
  91. protected function _added($lines)
  92. {
  93. array_walk($lines, array(&$this, '_encode'));
  94. $lines[0] = $this->_ins_prefix . $lines[0];
  95. $lines[count($lines) - 1] .= $this->_ins_suffix;
  96. return $this->_lines($lines, ' ', false);
  97. }
  98. protected function _deleted($lines, $words = false)
  99. {
  100. array_walk($lines, array(&$this, '_encode'));
  101. $lines[0] = $this->_del_prefix . $lines[0];
  102. $lines[count($lines) - 1] .= $this->_del_suffix;
  103. return $this->_lines($lines, ' ', false);
  104. }
  105. protected function _changed($orig, $final)
  106. {
  107. /* If we've already split on characters, just display. */
  108. if ($this->_split_level == 'characters') {
  109. return $this->_deleted($orig)
  110. . $this->_added($final);
  111. }
  112. /* If we've already split on words, just display. */
  113. if ($this->_split_level == 'words') {
  114. $prefix = '';
  115. while ($orig[0] !== false && $final[0] !== false &&
  116. substr($orig[0], 0, 1) == ' ' &&
  117. substr($final[0], 0, 1) == ' ') {
  118. $prefix .= substr($orig[0], 0, 1);
  119. $orig[0] = substr($orig[0], 1);
  120. $final[0] = substr($final[0], 1);
  121. }
  122. return $prefix . $this->_deleted($orig) . $this->_added($final);
  123. }
  124. $text1 = implode("\n", $orig);
  125. $text2 = implode("\n", $final);
  126. /* Non-printing newline marker. */
  127. $nl = "\0";
  128. if ($this->_split_characters) {
  129. $diff = new Horde_Text_Diff('native',
  130. array(preg_split('//', $text1),
  131. preg_split('//', $text2)));
  132. } else {
  133. /* We want to split on word boundaries, but we need to preserve
  134. * whitespace as well. Therefore we split on words, but include
  135. * all blocks of whitespace in the wordlist. */
  136. $diff = new Horde_Text_Diff('native',
  137. array($this->_splitOnWords($text1, $nl),
  138. $this->_splitOnWords($text2, $nl)));
  139. }
  140. /* Get the diff in inline format. */
  141. $renderer = new Horde_Text_Diff_Renderer_inline
  142. (array_merge($this->getParams(),
  143. array('split_level' => $this->_split_characters ? 'characters' : 'words')));
  144. /* Run the diff and get the output. */
  145. return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
  146. }
  147. protected function _splitOnWords($string, $newlineEscape = "\n")
  148. {
  149. // Ignore \0; otherwise the while loop will never finish.
  150. $string = str_replace("\0", '', $string);
  151. $words = array();
  152. $length = strlen($string);
  153. $pos = 0;
  154. while ($pos < $length) {
  155. // Eat a word with any preceding whitespace.
  156. $spaces = strspn(substr($string, $pos), " \n");
  157. $nextpos = strcspn(substr($string, $pos + $spaces), " \n");
  158. $words[] = str_replace("\n", $newlineEscape, substr($string, $pos, $spaces + $nextpos));
  159. $pos += $spaces + $nextpos;
  160. }
  161. return $words;
  162. }
  163. protected function _encode(&$string)
  164. {
  165. $string = htmlspecialchars($string);
  166. }
  167. }