author | Anthony Ly <anthonyly.com@gmail.com> |
Mon, 19 Nov 2012 18:26:13 +0100 | |
changeset 194 | 32102edaa81b |
parent 136 | bde1974c263b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/** |
|
3 |
* "Inline" diff renderer. |
|
4 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
5 |
* Copyright 2004-2010 The Horde Project (http://www.horde.org/) |
136 | 6 |
* |
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. |
|
9 |
* |
|
10 |
* @author Ciprian Popovici |
|
11 |
* @package Text_Diff |
|
12 |
*/ |
|
13 |
||
14 |
/** Text_Diff_Renderer */ |
|
15 |
||
16 |
// WP #7391 |
|
17 |
require_once dirname(dirname(__FILE__)) . '/Renderer.php'; |
|
18 |
||
19 |
/** |
|
20 |
* "Inline" diff renderer. |
|
21 |
* |
|
22 |
* This class renders diffs in the Wiki-style "inline" format. |
|
23 |
* |
|
24 |
* @author Ciprian Popovici |
|
25 |
* @package Text_Diff |
|
26 |
*/ |
|
27 |
class Text_Diff_Renderer_inline extends Text_Diff_Renderer { |
|
28 |
||
29 |
/** |
|
30 |
* Number of leading context "lines" to preserve. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
31 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
32 |
* @var integer |
136 | 33 |
*/ |
34 |
var $_leading_context_lines = 10000; |
|
35 |
||
36 |
/** |
|
37 |
* Number of trailing context "lines" to preserve. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
38 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
39 |
* @var integer |
136 | 40 |
*/ |
41 |
var $_trailing_context_lines = 10000; |
|
42 |
||
43 |
/** |
|
44 |
* Prefix for inserted text. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
45 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
46 |
* @var string |
136 | 47 |
*/ |
48 |
var $_ins_prefix = '<ins>'; |
|
49 |
||
50 |
/** |
|
51 |
* Suffix for inserted text. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
52 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
53 |
* @var string |
136 | 54 |
*/ |
55 |
var $_ins_suffix = '</ins>'; |
|
56 |
||
57 |
/** |
|
58 |
* Prefix for deleted text. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
59 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
60 |
* @var string |
136 | 61 |
*/ |
62 |
var $_del_prefix = '<del>'; |
|
63 |
||
64 |
/** |
|
65 |
* Suffix for deleted text. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
66 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
67 |
* @var string |
136 | 68 |
*/ |
69 |
var $_del_suffix = '</del>'; |
|
70 |
||
71 |
/** |
|
72 |
* Header for each change block. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
73 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
74 |
* @var string |
136 | 75 |
*/ |
76 |
var $_block_header = ''; |
|
77 |
||
78 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
79 |
* Whether to split down to character-level. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
80 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
81 |
* @var boolean |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
82 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
83 |
var $_split_characters = false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
84 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
85 |
/** |
136 | 86 |
* What are we currently splitting on? Used to recurse to show word-level |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
87 |
* or character-level changes. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
88 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
89 |
* @var string |
136 | 90 |
*/ |
91 |
var $_split_level = 'lines'; |
|
92 |
||
93 |
function _blockHeader($xbeg, $xlen, $ybeg, $ylen) |
|
94 |
{ |
|
95 |
return $this->_block_header; |
|
96 |
} |
|
97 |
||
98 |
function _startBlock($header) |
|
99 |
{ |
|
100 |
return $header; |
|
101 |
} |
|
102 |
||
103 |
function _lines($lines, $prefix = ' ', $encode = true) |
|
104 |
{ |
|
105 |
if ($encode) { |
|
106 |
array_walk($lines, array(&$this, '_encode')); |
|
107 |
} |
|
108 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
109 |
if ($this->_split_level == 'lines') { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
110 |
return implode("\n", $lines) . "\n"; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
111 |
} else { |
136 | 112 |
return implode('', $lines); |
113 |
} |
|
114 |
} |
|
115 |
||
116 |
function _added($lines) |
|
117 |
{ |
|
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); |
|
122 |
} |
|
123 |
||
124 |
function _deleted($lines, $words = false) |
|
125 |
{ |
|
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); |
|
130 |
} |
|
131 |
||
132 |
function _changed($orig, $final) |
|
133 |
{ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
134 |
/* If we've already split on characters, just display. */ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
135 |
if ($this->_split_level == 'characters') { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
136 |
return $this->_deleted($orig) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
137 |
. $this->_added($final); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
138 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
139 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
140 |
/* If we've already split on words, just display. */ |
136 | 141 |
if ($this->_split_level == 'words') { |
142 |
$prefix = ''; |
|
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); |
|
149 |
} |
|
150 |
return $prefix . $this->_deleted($orig) . $this->_added($final); |
|
151 |
} |
|
152 |
||
153 |
$text1 = implode("\n", $orig); |
|
154 |
$text2 = implode("\n", $final); |
|
155 |
||
156 |
/* Non-printing newline marker. */ |
|
157 |
$nl = "\0"; |
|
158 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
159 |
if ($this->_split_characters) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
160 |
$diff = new Text_Diff('native', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
161 |
array(preg_split('//', $text1), |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
162 |
preg_split('//', $text2))); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
163 |
} else { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
164 |
/* We want to split on word boundaries, but we need to preserve |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
165 |
* whitespace as well. Therefore we split on words, but include |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
166 |
* all blocks of whitespace in the wordlist. */ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
167 |
$diff = new Text_Diff('native', |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
168 |
array($this->_splitOnWords($text1, $nl), |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
169 |
$this->_splitOnWords($text2, $nl))); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
170 |
} |
136 | 171 |
|
172 |
/* Get the diff in inline format. */ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
173 |
$renderer = new Text_Diff_Renderer_inline |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
174 |
(array_merge($this->getParams(), |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
175 |
array('split_level' => $this->_split_characters ? 'characters' : 'words'))); |
136 | 176 |
|
177 |
/* Run the diff and get the output. */ |
|
178 |
return str_replace($nl, "\n", $renderer->render($diff)) . "\n"; |
|
179 |
} |
|
180 |
||
181 |
function _splitOnWords($string, $newlineEscape = "\n") |
|
182 |
{ |
|
183 |
// Ignore \0; otherwise the while loop will never finish. |
|
184 |
$string = str_replace("\0", '', $string); |
|
185 |
||
186 |
$words = array(); |
|
187 |
$length = strlen($string); |
|
188 |
$pos = 0; |
|
189 |
||
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; |
|
196 |
} |
|
197 |
||
198 |
return $words; |
|
199 |
} |
|
200 |
||
201 |
function _encode(&$string) |
|
202 |
{ |
|
203 |
$string = htmlspecialchars($string); |
|
204 |
} |
|
205 |
||
206 |
} |