-
+ 9B9C95946B31ECD65CDBABF802864EBC87D3611BDDF2247DCB2F501B8C5B7D039FF2937AA66D3643455FB76ABA225D2DDB961BEC6BC8A57C9833A87B042E3A71
mp-wp/wp-includes/Text/Diff/Renderer.php
(0 . 0)(1 . 237)
70185 <?php
70186 /**
70187 * A class to render Diffs in different formats.
70188 *
70189 * This class renders the diff in classic diff format. It is intended that
70190 * this class be customized via inheritance, to obtain fancier outputs.
70191 *
70192 * $Horde: framework/Text_Diff/Diff/Renderer.php,v 1.21 2008/01/04 10:07:50 jan Exp $
70193 *
70194 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
70195 *
70196 * See the enclosed file COPYING for license information (LGPL). If you did
70197 * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
70198 *
70199 * @package Text_Diff
70200 */
70201 class Text_Diff_Renderer {
70202
70203 /**
70204 * Number of leading context "lines" to preserve.
70205 *
70206 * This should be left at zero for this class, but subclasses may want to
70207 * set this to other values.
70208 */
70209 var $_leading_context_lines = 0;
70210
70211 /**
70212 * Number of trailing context "lines" to preserve.
70213 *
70214 * This should be left at zero for this class, but subclasses may want to
70215 * set this to other values.
70216 */
70217 var $_trailing_context_lines = 0;
70218
70219 /**
70220 * Constructor.
70221 */
70222 function Text_Diff_Renderer($params = array())
70223 {
70224 foreach ($params as $param => $value) {
70225 $v = '_' . $param;
70226 if (isset($this->$v)) {
70227 $this->$v = $value;
70228 }
70229 }
70230 }
70231
70232 /**
70233 * Get any renderer parameters.
70234 *
70235 * @return array All parameters of this renderer object.
70236 */
70237 function getParams()
70238 {
70239 $params = array();
70240 foreach (get_object_vars($this) as $k => $v) {
70241 if ($k[0] == '_') {
70242 $params[substr($k, 1)] = $v;
70243 }
70244 }
70245
70246 return $params;
70247 }
70248
70249 /**
70250 * Renders a diff.
70251 *
70252 * @param Text_Diff $diff A Text_Diff object.
70253 *
70254 * @return string The formatted output.
70255 */
70256 function render($diff)
70257 {
70258 $xi = $yi = 1;
70259 $block = false;
70260 $context = array();
70261
70262 $nlead = $this->_leading_context_lines;
70263 $ntrail = $this->_trailing_context_lines;
70264
70265 $output = $this->_startDiff();
70266
70267 $diffs = $diff->getDiff();
70268 foreach ($diffs as $i => $edit) {
70269 /* If these are unchanged (copied) lines, and we want to keep
70270 * leading or trailing context lines, extract them from the copy
70271 * block. */
70272 if (is_a($edit, 'Text_Diff_Op_copy')) {
70273 /* Do we have any diff blocks yet? */
70274 if (is_array($block)) {
70275 /* How many lines to keep as context from the copy
70276 * block. */
70277 $keep = $i == count($diffs) - 1 ? $ntrail : $nlead + $ntrail;
70278 if (count($edit->orig) <= $keep) {
70279 /* We have less lines in the block than we want for
70280 * context => keep the whole block. */
70281 $block[] = $edit;
70282 } else {
70283 if ($ntrail) {
70284 /* Create a new block with as many lines as we need
70285 * for the trailing context. */
70286 $context = array_slice($edit->orig, 0, $ntrail);
70287 $block[] = &new Text_Diff_Op_copy($context);
70288 }
70289 /* @todo */
70290 $output .= $this->_block($x0, $ntrail + $xi - $x0,
70291 $y0, $ntrail + $yi - $y0,
70292 $block);
70293 $block = false;
70294 }
70295 }
70296 /* Keep the copy block as the context for the next block. */
70297 $context = $edit->orig;
70298 } else {
70299 /* Don't we have any diff blocks yet? */
70300 if (!is_array($block)) {
70301 /* Extract context lines from the preceding copy block. */
70302 $context = array_slice($context, count($context) - $nlead);
70303 $x0 = $xi - count($context);
70304 $y0 = $yi - count($context);
70305 $block = array();
70306 if ($context) {
70307 $block[] = &new Text_Diff_Op_copy($context);
70308 }
70309 }
70310 $block[] = $edit;
70311 }
70312
70313 if ($edit->orig) {
70314 $xi += count($edit->orig);
70315 }
70316 if ($edit->final) {
70317 $yi += count($edit->final);
70318 }
70319 }
70320
70321 if (is_array($block)) {
70322 $output .= $this->_block($x0, $xi - $x0,
70323 $y0, $yi - $y0,
70324 $block);
70325 }
70326
70327 return $output . $this->_endDiff();
70328 }
70329
70330 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits)
70331 {
70332 $output = $this->_startBlock($this->_blockHeader($xbeg, $xlen, $ybeg, $ylen));
70333
70334 foreach ($edits as $edit) {
70335 switch (strtolower(get_class($edit))) {
70336 case 'text_diff_op_copy':
70337 $output .= $this->_context($edit->orig);
70338 break;
70339
70340 case 'text_diff_op_add':
70341 $output .= $this->_added($edit->final);
70342 break;
70343
70344 case 'text_diff_op_delete':
70345 $output .= $this->_deleted($edit->orig);
70346 break;
70347
70348 case 'text_diff_op_change':
70349 $output .= $this->_changed($edit->orig, $edit->final);
70350 break;
70351 }
70352 }
70353
70354 return $output . $this->_endBlock();
70355 }
70356
70357 function _startDiff()
70358 {
70359 return '';
70360 }
70361
70362 function _endDiff()
70363 {
70364 return '';
70365 }
70366
70367 function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
70368 {
70369 if ($xlen > 1) {
70370 $xbeg .= ',' . ($xbeg + $xlen - 1);
70371 }
70372 if ($ylen > 1) {
70373 $ybeg .= ',' . ($ybeg + $ylen - 1);
70374 }
70375
70376 // this matches the GNU Diff behaviour
70377 if ($xlen && !$ylen) {
70378 $ybeg--;
70379 } elseif (!$xlen) {
70380 $xbeg--;
70381 }
70382
70383 return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
70384 }
70385
70386 function _startBlock($header)
70387 {
70388 return $header . "\n";
70389 }
70390
70391 function _endBlock()
70392 {
70393 return '';
70394 }
70395
70396 function _lines($lines, $prefix = ' ')
70397 {
70398 return $prefix . implode("\n$prefix", $lines) . "\n";
70399 }
70400
70401 function _context($lines)
70402 {
70403 return $this->_lines($lines, ' ');
70404 }
70405
70406 function _added($lines)
70407 {
70408 return $this->_lines($lines, '> ');
70409 }
70410
70411 function _deleted($lines)
70412 {
70413 return $this->_lines($lines, '< ');
70414 }
70415
70416 function _changed($orig, $final)
70417 {
70418 return $this->_deleted($orig) . "---\n" . $this->_added($final);
70419 }
70420
70421 }