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.8 2005/05/02 19:44:53 chuck Exp $
  9.  *
  10.  * @package Text_Diff
  11.  */
  12.  
  13.     /**
  14.      * Number of leading context "lines" to preserve.
  15.      *
  16.      * This should be left at zero for this class, but subclasses may want to
  17.      * set this to other values.
  18.      */
  19.     var $_leading_context_lines = 0;
  20.  
  21.     /**
  22.      * Number of trailing context "lines" to preserve.
  23.      *
  24.      * This should be left at zero for this class, but subclasses may want to
  25.      * set this to other values.
  26.      */
  27.     var $_trailing_context_lines = 0;
  28.  
  29.     /**
  30.      * Constructor.
  31.      */
  32.     function Text_Diff_Renderer($params = array())
  33.     {
  34.         foreach ($params as $param => $value{
  35.             $v '_' $param;
  36.             if (isset($this->$v)) {
  37.                 $this->$v $value;
  38.             }
  39.         }
  40.     }
  41.  
  42.     /**
  43.      * Get any renderer parameters.
  44.      *
  45.      * @return array  All parameters of this renderer object.
  46.      */
  47.     function getParams()
  48.     {
  49.         return get_object_vars($this);
  50.     }
  51.  
  52.     /**
  53.      * Renders a diff.
  54.      *
  55.      * @param Text_Diff $diff  A Text_Diff object.
  56.      *
  57.      * @return string  The formatted output.
  58.      */
  59.     function render($diff)
  60.     {
  61.         $xi $yi = 1;
  62.         $block = false;
  63.         $context = array();
  64.  
  65.         $nlead $this->_leading_context_lines;
  66.         $ntrail $this->_trailing_context_lines;
  67.  
  68.         $output $this->_startDiff();
  69.  
  70.         foreach ($diff->getDiff(as $edit{
  71.             if (is_a($edit'Text_Diff_Op_copy')) {
  72.                 if (is_array($block)) {
  73.                     if (count($edit->orig<= $nlead $ntrail{
  74.                         $block[$edit;
  75.                     else {
  76.                         if ($ntrail{
  77.                             $context array_slice($edit->orig0$ntrail);
  78.                             $block[&new Text_Diff_Op_copy($context);
  79.                         }
  80.                         $output .= $this->_block($x0$ntrail $xi $x0,
  81.                                                  $y0$ntrail $yi $y0,
  82.                                                  $block);
  83.                         $block = false;
  84.                     }
  85.                 }
  86.                 $context $edit->orig;
  87.             else {
  88.                 if (!is_array($block)) {
  89.                     $context array_slice($contextcount($context$nlead);
  90.                     $x0 $xi count($context);
  91.                     $y0 $yi count($context);
  92.                     $block = array();
  93.                     if ($context{
  94.                         $block[&new Text_Diff_Op_copy($context);
  95.                     }
  96.                 }
  97.                 $block[$edit;
  98.             }
  99.  
  100.             if ($edit->orig{
  101.                 $xi += count($edit->orig);
  102.             }
  103.             if ($edit->final{
  104.                 $yi += count($edit->final);
  105.             }
  106.         }
  107.  
  108.         if (is_array($block)) {
  109.             $output .= $this->_block($x0$xi $x0,
  110.                                      $y0$yi $y0,
  111.                                      $block);
  112.         }
  113.  
  114.         return $output $this->_endDiff();
  115.     }
  116.  
  117.     function _block($xbeg$xlen$ybeg$ylen&$edits)
  118.     {
  119.         $output $this->_startBlock($this->_blockHeader($xbeg$xlen$ybeg$ylen));
  120.  
  121.         foreach ($edits as $edit{
  122.             switch (strtolower(get_class($edit))) {
  123.             case 'text_diff_op_copy':
  124.                 $output .= $this->_context($edit->orig);
  125.                 break;
  126.  
  127.             case 'text_diff_op_add':
  128.                 $output .= $this->_added($edit->final);
  129.                 break;
  130.  
  131.             case 'text_diff_op_delete':
  132.                 $output .= $this->_deleted($edit->orig);
  133.                 break;
  134.  
  135.             case 'text_diff_op_change':
  136.                 $output .= $this->_changed($edit->orig$edit->final);
  137.                 break;
  138.             }
  139.         }
  140.  
  141.         return $output $this->_endBlock();
  142.     }
  143.  
  144.     function _startDiff()
  145.     {
  146.         return '';
  147.     }
  148.  
  149.     function _endDiff()
  150.     {
  151.         return '';
  152.     }
  153.  
  154.     function _blockHeader($xbeg$xlen$ybeg$ylen)
  155.     {
  156.         if ($xlen > 1{
  157.             $xbeg .= ',' ($xbeg $xlen - 1);
  158.         }
  159.         if ($ylen > 1{
  160.             $ybeg .= ',' ($ybeg $ylen - 1);
  161.         }
  162.  
  163.         return $xbeg ($xlen ($ylen 'c' 'd''a'$ybeg;
  164.     }
  165.  
  166.     function _startBlock($header)
  167.     {
  168.         return $header "\n";
  169.     }
  170.  
  171.     function _endBlock()
  172.     {
  173.         return '';
  174.     }
  175.  
  176.     function _lines($lines$prefix ' ')
  177.     {
  178.         return $prefix implode("\n$prefix"$lines"\n";
  179.     }
  180.  
  181.     function _context($lines)
  182.     {
  183.         return $this->_lines($lines);
  184.     }
  185.  
  186.     function _added($lines)
  187.     {
  188.         return $this->_lines($lines'>');
  189.     }
  190.  
  191.     function _deleted($lines)
  192.     {
  193.         return $this->_lines($lines'<');
  194.     }
  195.  
  196.     function _changed($orig$final)
  197.     {
  198.         return $this->_deleted($orig"---\n" $this->_added($final);
  199.     }
  200.  
  201. }

Documentation generated on Mon, 11 Mar 2019 14:20:29 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.