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.9 2005/05/04 20:21:52 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.         $params = array();
  50.         foreach (get_object_vars($this) as $k => $v) {
  51.             if ($k{0} == '_') {
  52.                 $params[substr($k, 1)] = $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.         foreach ($diff->getDiff() as $edit) {
  78.             if (is_a($edit, 'Text_Diff_Op_copy')) {
  79.                 if (is_array($block)) {
  80.                     if (count($edit->orig) <= $nlead + $ntrail) {
  81.                         $block[] = $edit;
  82.                     } else {
  83.                         if ($ntrail) {
  84.                             $context = array_slice($edit->orig, 0, $ntrail);
  85.                             $block[] = &new Text_Diff_Op_copy($context);
  86.                         }
  87.                         $output .= $this->_block($x0, $ntrail + $xi - $x0,
  88.                                                  $y0, $ntrail + $yi - $y0,
  89.                                                  $block);
  90.                         $block = false;
  91.                     }
  92.                 }
  93.                 $context = $edit->orig;
  94.             } else {
  95.                 if (!is_array($block)) {
  96.                     $context = array_slice($context, count($context) - $nlead);
  97.                     $x0 = $xi - count($context);
  98.                     $y0 = $yi - count($context);
  99.                     $block = array();
  100.                     if ($context) {
  101.                         $block[] = &new Text_Diff_Op_copy($context);
  102.                     }
  103.                 }
  104.                 $block[] = $edit;
  105.             }
  106.  
  107.             if ($edit->orig) {
  108.                 $xi += count($edit->orig);
  109.             }
  110.             if ($edit->final) {
  111.                 $yi += count($edit->final);
  112.             }
  113.         }
  114.  
  115.         if (is_array($block)) {
  116.             $output .= $this->_block($x0, $xi - $x0,
  117.                                      $y0, $yi - $y0,
  118.                                      $block);
  119.         }
  120.  
  121.         return $output . $this->_endDiff();
  122.     }
  123.  
  124.     function _block($xbeg, $xlen, $ybeg, $ylen, &$edits)
  125.     {
  126.         $output = $this->_startBlock($this->_blockHeader($xbeg, $xlen, $ybeg, $ylen));
  127.  
  128.         foreach ($edits as $edit) {
  129.             switch (strtolower(get_class($edit))) {
  130.             case 'text_diff_op_copy':
  131.                 $output .= $this->_context($edit->orig);
  132.                 break;
  133.  
  134.             case 'text_diff_op_add':
  135.                 $output .= $this->_added($edit->final);
  136.                 break;
  137.  
  138.             case 'text_diff_op_delete':
  139.                 $output .= $this->_deleted($edit->orig);
  140.                 break;
  141.  
  142.             case 'text_diff_op_change':
  143.                 $output .= $this->_changed($edit->orig, $edit->final);
  144.                 break;
  145.             }
  146.         }
  147.  
  148.         return $output . $this->_endBlock();
  149.     }
  150.  
  151.     function _startDiff()
  152.     {
  153.         return '';
  154.     }
  155.  
  156.     function _endDiff()
  157.     {
  158.         return '';
  159.     }
  160.  
  161.     function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
  162.     {
  163.         if ($xlen > 1) {
  164.             $xbeg .= ',' . ($xbeg + $xlen - 1);
  165.         }
  166.         if ($ylen > 1) {
  167.             $ybeg .= ',' . ($ybeg + $ylen - 1);
  168.         }
  169.  
  170.         return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
  171.     }
  172.  
  173.     function _startBlock($header)
  174.     {
  175.         return $header . "\n";
  176.     }
  177.  
  178.     function _endBlock()
  179.     {
  180.         return '';
  181.     }
  182.  
  183.     function _lines($lines, $prefix = ' ')
  184.     {
  185.         return $prefix . implode("\n$prefix", $lines) . "\n";
  186.     }
  187.  
  188.     function _context($lines)
  189.     {
  190.         return $this->_lines($lines);
  191.     }
  192.  
  193.     function _added($lines)
  194.     {
  195.         return $this->_lines($lines, '>');
  196.     }
  197.  
  198.     function _deleted($lines)
  199.     {
  200.         return $this->_lines($lines, '<');
  201.     }
  202.  
  203.     function _changed($orig, $final)
  204.     {
  205.         return $this->_deleted($orig) . "---\n" . $this->_added($final);
  206.     }
  207.  
  208. }

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