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

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