3
* "Inline" diff renderer.
5
* Copyright 2004-2010 The Horde Project (http://www.horde.org/)
7
* See the enclosed file COPYING for license information (LGPL). If you did
8
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
10
* @author Ciprian Popovici
14
/** Text_Diff_Renderer */
17
require_once dirname(dirname(__FILE__)) . '/Renderer.php';
20
* "Inline" diff renderer.
22
* This class renders diffs in the Wiki-style "inline" format.
24
* @author Ciprian Popovici
27
class Text_Diff_Renderer_inline extends Text_Diff_Renderer {
30
* Number of leading context "lines" to preserve.
34
var $_leading_context_lines = 10000;
37
* Number of trailing context "lines" to preserve.
41
var $_trailing_context_lines = 10000;
44
* Prefix for inserted text.
48
var $_ins_prefix = '<ins>';
51
* Suffix for inserted text.
55
var $_ins_suffix = '</ins>';
58
* Prefix for deleted text.
62
var $_del_prefix = '<del>';
65
* Suffix for deleted text.
69
var $_del_suffix = '</del>';
72
* Header for each change block.
76
var $_block_header = '';
79
* Whether to split down to character-level.
83
var $_split_characters = false;
86
* What are we currently splitting on? Used to recurse to show word-level
87
* or character-level changes.
91
var $_split_level = 'lines';
93
function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
95
return $this->_block_header;
98
function _startBlock($header)
103
function _lines($lines, $prefix = ' ', $encode = true)
106
array_walk($lines, array(&$this, '_encode'));
109
if ($this->_split_level == 'lines') {
110
return implode("\n", $lines) . "\n";
112
return implode('', $lines);
116
function _added($lines)
118
array_walk($lines, array(&$this, '_encode'));
119
$lines[0] = $this->_ins_prefix . $lines[0];
120
$lines[count($lines) - 1] .= $this->_ins_suffix;
121
return $this->_lines($lines, ' ', false);
124
function _deleted($lines, $words = false)
126
array_walk($lines, array(&$this, '_encode'));
127
$lines[0] = $this->_del_prefix . $lines[0];
128
$lines[count($lines) - 1] .= $this->_del_suffix;
129
return $this->_lines($lines, ' ', false);
132
function _changed($orig, $final)
134
/* If we've already split on characters, just display. */
135
if ($this->_split_level == 'characters') {
136
return $this->_deleted($orig)
137
. $this->_added($final);
140
/* If we've already split on words, just display. */
141
if ($this->_split_level == 'words') {
143
while ($orig[0] !== false && $final[0] !== false &&
144
substr($orig[0], 0, 1) == ' ' &&
145
substr($final[0], 0, 1) == ' ') {
146
$prefix .= substr($orig[0], 0, 1);
147
$orig[0] = substr($orig[0], 1);
148
$final[0] = substr($final[0], 1);
150
return $prefix . $this->_deleted($orig) . $this->_added($final);
153
$text1 = implode("\n", $orig);
154
$text2 = implode("\n", $final);
156
/* Non-printing newline marker. */
159
if ($this->_split_characters) {
160
$diff = new Text_Diff('native',
161
array(preg_split('//', $text1),
162
preg_split('//', $text2)));
164
/* We want to split on word boundaries, but we need to preserve
165
* whitespace as well. Therefore we split on words, but include
166
* all blocks of whitespace in the wordlist. */
167
$diff = new Text_Diff('native',
168
array($this->_splitOnWords($text1, $nl),
169
$this->_splitOnWords($text2, $nl)));
172
/* Get the diff in inline format. */
173
$renderer = new Text_Diff_Renderer_inline
174
(array_merge($this->getParams(),
175
array('split_level' => $this->_split_characters ? 'characters' : 'words')));
177
/* Run the diff and get the output. */
178
return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
181
function _splitOnWords($string, $newlineEscape = "\n")
183
// Ignore \0; otherwise the while loop will never finish.
184
$string = str_replace("\0", '', $string);
187
$length = strlen($string);
190
while ($pos < $length) {
191
// Eat a word with any preceding whitespace.
192
$spaces = strspn(substr($string, $pos), " \n");
193
$nextpos = strcspn(substr($string, $pos + $spaces), " \n");
194
$words[] = str_replace("\n", $newlineEscape, substr($string, $pos, $spaces + $nextpos));
195
$pos += $spaces + $nextpos;
201
function _encode(&$string)
203
$string = htmlspecialchars($string);