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

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