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.12 2005/12/16 11:07:33 jan 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.         $params = array();
  50.         foreach (get_object_vars($thisas $k => $v{
  51.             if ($k[0== '_'{
  52.                 $params[substr($k1)$v;
  53.             }
  54.         }
  55.  
  56.         return $params;
  57.     }
  58.  
  59.     /**
  60.      * Renders a diff.
  61.      *
  62.      * @param Text_Diff $diff  A Text_Diff object.
  63.      *
  64.      * @return string  The formatted output.
  65.      */
  66.     function render($diff)
  67.     {
  68.         $xi $yi = 1;
  69.         $block = false;
  70.         $context = array();
  71.  
  72.         $nlead $this->_leading_context_lines;
  73.         $ntrail $this->_trailing_context_lines;
  74.  
  75.         $output $this->_startDiff();
  76.  
  77.         $diffs $diff->getDiff();
  78.         foreach ($diffs as $i => $edit{
  79.             if (is_a($edit'Text_Diff_Op_copy')) {
  80.                 if (is_array($block)) {
  81.                     $keep $i == count($diffs- 1 ? $ntrail $nlead $ntrail;
  82.                     if (count($edit->orig<= $keep{
  83.                         $block[$edit;
  84.                     else {
  85.                         if ($ntrail{
  86.                             $context array_slice($edit->orig0$ntrail);
  87.                             $block[&new Text_Diff_Op_copy($context);
  88.                         }
  89.                         $output .= $this->_block($x0$ntrail $xi $x0,
  90.                                                  $y0$ntrail $yi $y0,
  91.                                                  $block);
  92.                         $block = false;
  93.                     }
  94.                 }
  95.                 $context $edit->orig;
  96.             else {
  97.                 if (!is_array($block)) {
  98.                     $context array_slice($contextcount($context$nlead);
  99.                     $x0 $xi count($context);
  100.                     $y0 $yi count($context);
  101.                     $block = array();
  102.                     if ($context{
  103.                         $block[&new Text_Diff_Op_copy($context);
  104.                     }
  105.                 }
  106.                 $block[$edit;
  107.             }
  108.  
  109.             if ($edit->orig{
  110.                 $xi += count($edit->orig);
  111.             }
  112.             if ($edit->final{
  113.                 $yi += count($edit->final);
  114.             }
  115.         }
  116.  
  117.         if (is_array($block)) {
  118.             $output .= $this->_block($x0$xi $x0,
  119.                                      $y0$yi $y0,
  120.                                      $block);
  121.         }
  122.  
  123.         return $output $this->_endDiff();
  124.     }
  125.  
  126.     function _block($xbeg$xlen$ybeg$ylen&$edits)
  127.     {
  128.         $output $this->_startBlock($this->_blockHeader($xbeg$xlen$ybeg$ylen));
  129.  
  130.         foreach ($edits as $edit{
  131.             switch (strtolower(get_class($edit))) {
  132.             case 'text_diff_op_copy':
  133.                 $output .= $this->_context($edit->orig);
  134.                 break;
  135.  
  136.             case 'text_diff_op_add':
  137.                 $output .= $this->_added($edit->final);
  138.                 break;
  139.  
  140.             case 'text_diff_op_delete':
  141.                 $output .= $this->_deleted($edit->orig);
  142.                 break;
  143.  
  144.             case 'text_diff_op_change':
  145.                 $output .= $this->_changed($edit->orig$edit->final);
  146.                 break;
  147.             }
  148.         }
  149.  
  150.         return $output $this->_endBlock();
  151.     }
  152.  
  153.     function _startDiff()
  154.     {
  155.         return '';
  156.     }
  157.  
  158.     function _endDiff()
  159.     {
  160.         return '';
  161.     }
  162.  
  163.     function _blockHeader($xbeg$xlen$ybeg$ylen)
  164.     {
  165.         if ($xlen > 1{
  166.             $xbeg .= ',' ($xbeg $xlen - 1);
  167.         }
  168.         if ($ylen > 1{
  169.             $ybeg .= ',' ($ybeg $ylen - 1);
  170.         }
  171.  
  172.         return $xbeg ($xlen ($ylen 'c' 'd''a'$ybeg;
  173.     }
  174.  
  175.     function _startBlock($header)
  176.     {
  177.         return $header "\n";
  178.     }
  179.  
  180.     function _endBlock()
  181.     {
  182.         return '';
  183.     }
  184.  
  185.     function _lines($lines$prefix ' ')
  186.     {
  187.         return $prefix implode("\n$prefix"$lines"\n";
  188.     }
  189.  
  190.     function _context($lines)
  191.     {
  192.         return $this->_lines($lines);
  193.     }
  194.  
  195.     function _added($lines)
  196.     {
  197.         return $this->_lines($lines'>');
  198.     }
  199.  
  200.     function _deleted($lines)
  201.     {
  202.         return $this->_lines($lines'<');
  203.     }
  204.  
  205.     function _changed($orig$final)
  206.     {
  207.         return $this->_deleted($orig"---\n" $this->_added($final);
  208.     }
  209.  
  210. }

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