Text_Diff
[ class tree: Text_Diff ] [ index: Text_Diff ] [ all elements ]

Source for file Renderer.php

Documentation is available at Renderer.php

  1. <?php
  2. /**
  3.  * A class to render Diffs in different formats.
  4.  *
  5.  * This class renders the diff in classic diff format. It is intended that
  6.  * this class be customized via inheritance, to obtain fancier outputs.
  7.  *
  8.  * $Horde: framework/Text_Diff/Diff/Renderer.php,v 1.5.10.12 2009/07/24 13:26:40 jan Exp $
  9.  *
  10.  * Copyright 2004-2009 The Horde Project (http://www.horde.org/)
  11.  *
  12.  * See the enclosed file COPYING for license information (LGPL). If you did
  13.  * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
  14.  *
  15.  * @package Text_Diff
  16.  */
  17.  
  18.     /**
  19.      * Number of leading context "lines" to preserve.
  20.      *
  21.      * This should be left at zero for this class, but subclasses may want to
  22.      * set this to other values.
  23.      */
  24.     var $_leading_context_lines = 0;
  25.  
  26.     /**
  27.      * Number of trailing context "lines" to preserve.
  28.      *
  29.      * This should be left at zero for this class, but subclasses may want to
  30.      * set this to other values.
  31.      */
  32.     var $_trailing_context_lines = 0;
  33.  
  34.     /**
  35.      * Constructor.
  36.      */
  37.     function __construct($params = array())
  38.     {
  39.         foreach ($params as $param => $value{
  40.             $v '_' $param;
  41.             if (isset($this->$v)) {
  42.                 $this->$v $value;
  43.             }
  44.         }
  45.     }
  46.  
  47.     /**
  48.      * Get any renderer parameters.
  49.      *
  50.      * @return array  All parameters of this renderer object.
  51.      */
  52.     function getParams()
  53.     {
  54.         $params = array();
  55.         foreach (get_object_vars($thisas $k => $v{
  56.             if ($k[0== '_'{
  57.                 $params[substr($k1)$v;
  58.             }
  59.         }
  60.  
  61.         return $params;
  62.     }
  63.  
  64.     /**
  65.      * Renders a diff.
  66.      *
  67.      * @param Text_Diff $diff  A Text_Diff object.
  68.      *
  69.      * @return string  The formatted output.
  70.      */
  71.     function render($diff)
  72.     {
  73.         $xi $yi = 1;
  74.         $block = false;
  75.         $context = array();
  76.  
  77.         $nlead $this->_leading_context_lines;
  78.         $ntrail $this->_trailing_context_lines;
  79.  
  80.         $output $this->_startDiff();
  81.  
  82.         $diffs $diff->getDiff();
  83.         foreach ($diffs as $i => $edit{
  84.             /* If these are unchanged (copied) lines, and we want to keep
  85.              * leading or trailing context lines, extract them from the copy
  86.              * block. */
  87.             if (is_a($edit'Text_Diff_Op_copy')) {
  88.                 /* Do we have any diff blocks yet? */
  89.                 if (is_array($block)) {
  90.                     /* How many lines to keep as context from the copy
  91.                      * block. */
  92.                     $keep $i == count($diffs- 1 ? $ntrail $nlead $ntrail;
  93.                     if (count($edit->orig<= $keep{
  94.                         /* We have less lines in the block than we want for
  95.                          * context => keep the whole block. */
  96.                         $block[$edit;
  97.                     else {
  98.                         if ($ntrail{
  99.                             /* Create a new block with as many lines as we need
  100.                              * for the trailing context. */
  101.                             $context array_slice($edit->orig0$ntrail);
  102.                             $block[= new Text_Diff_Op_copy($context);
  103.                         }
  104.                         /* @todo */
  105.                         $output .= $this->_block($x0$ntrail $xi $x0,
  106.                                                  $y0$ntrail $yi $y0,
  107.                                                  $block);
  108.                         $block = false;
  109.                     }
  110.                 }
  111.                 /* Keep the copy block as the context for the next block. */
  112.                 $context $edit->orig;
  113.             else {
  114.                 /* Don't we have any diff blocks yet? */
  115.                 if (!is_array($block)) {
  116.                     /* Extract context lines from the preceding copy block. */
  117.                     $context array_slice($contextcount($context$nlead);
  118.                     $x0 $xi count($context);
  119.                     $y0 $yi count($context);
  120.                     $block = array();
  121.                     if ($context{
  122.                         $block[= new Text_Diff_Op_copy($context);
  123.                     }
  124.                 }
  125.                 $block[$edit;
  126.             }
  127.  
  128.             if ($edit->orig{
  129.                 $xi += count($edit->orig);
  130.             }
  131.             if ($edit->final{
  132.                 $yi += count($edit->final);
  133.             }
  134.         }
  135.  
  136.         if (is_array($block)) {
  137.             $output .= $this->_block($x0$xi $x0,
  138.                                      $y0$yi $y0,
  139.                                      $block);
  140.         }
  141.  
  142.         return $output $this->_endDiff();
  143.     }
  144.  
  145.     function _block($xbeg$xlen$ybeg$ylen&$edits)
  146.     {
  147.         $output $this->_startBlock($this->_blockHeader($xbeg$xlen$ybeg$ylen));
  148.  
  149.         foreach ($edits as $edit{
  150.             switch (strtolower(get_class($edit))) {
  151.             case 'text_diff_op_copy':
  152.                 $output .= $this->_context($edit->orig);
  153.                 break;
  154.  
  155.             case 'text_diff_op_add':
  156.                 $output .= $this->_added($edit->final);
  157.                 break;
  158.  
  159.             case 'text_diff_op_delete':
  160.                 $output .= $this->_deleted($edit->orig);
  161.                 break;
  162.  
  163.             case 'text_diff_op_change':
  164.                 $output .= $this->_changed($edit->orig$edit->final);
  165.                 break;
  166.             }
  167.         }
  168.  
  169.         return $output $this->_endBlock();
  170.     }
  171.  
  172.     function _startDiff()
  173.     {
  174.         return '';
  175.     }
  176.  
  177.     function _endDiff()
  178.     {
  179.         return '';
  180.     }
  181.  
  182.     function _blockHeader($xbeg$xlen$ybeg$ylen)
  183.     {
  184.         if ($xlen > 1{
  185.             $xbeg .= ',' ($xbeg $xlen - 1);
  186.         }
  187.         if ($ylen > 1{
  188.             $ybeg .= ',' ($ybeg $ylen - 1);
  189.         }
  190.  
  191.         // this matches the GNU Diff behaviour
  192.         if ($xlen && !$ylen{
  193.             $ybeg--;
  194.         elseif (!$xlen{
  195.             $xbeg--;
  196.         }
  197.  
  198.         return $xbeg ($xlen ($ylen 'c' 'd''a'$ybeg;
  199.     }
  200.  
  201.     function _startBlock($header)
  202.     {
  203.         return $header "\n";
  204.     }
  205.  
  206.     function _endBlock()
  207.     {
  208.         return '';
  209.     }
  210.  
  211.     function _lines($lines$prefix ' ')
  212.     {
  213.         return $prefix implode("\n$prefix"$lines"\n";
  214.     }
  215.  
  216.     function _context($lines)
  217.     {
  218.         return $this->_lines($lines'  ');
  219.     }
  220.  
  221.     function _added($lines)
  222.     {
  223.         return $this->_lines($lines'> ');
  224.     }
  225.  
  226.     function _deleted($lines)
  227.     {
  228.         return $this->_lines($lines'< ');
  229.     }
  230.  
  231.     function _changed($orig$final)
  232.     {
  233.         return $this->_deleted($orig"---\n" $this->_added($final);
  234.     }
  235.  
  236. }

Documentation generated on Mon, 11 Mar 2019 16:03:26 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.