Text_Diff
[ class tree: Text_Diff ] [ index: Text_Diff ] [ all elements ]

Source for file inline.php

Documentation is available at inline.php

  1. <?php
  2.  
  3. require_once 'Text/Diff/Renderer.php';
  4.  
  5. /**
  6.  * "Inline" diff renderer.
  7.  *
  8.  * This class renders diffs in the Wiki-style "inline" format.
  9.  *
  10.  * $Horde: framework/Text_Diff/Diff/Renderer/inline.php,v 1.16 2006/01/08 00:06:57 jan Exp $
  11.  *
  12.  * @author  Ciprian Popovici
  13.  * @package Text_Diff
  14.  */
  15.  
  16.     /**
  17.      * Number of leading context "lines" to preserve.
  18.      */
  19.     var $_leading_context_lines = 10000;
  20.  
  21.     /**
  22.      * Number of trailing context "lines" to preserve.
  23.      */
  24.     var $_trailing_context_lines = 10000;
  25.  
  26.     /**
  27.      * Prefix for inserted text.
  28.      */
  29.     var $_ins_prefix '<ins>';
  30.  
  31.     /**
  32.      * Suffix for inserted text.
  33.      */
  34.     var $_ins_suffix '</ins>';
  35.  
  36.     /**
  37.      * Prefix for deleted text.
  38.      */
  39.     var $_del_prefix '<del>';
  40.  
  41.     /**
  42.      * Suffix for deleted text.
  43.      */
  44.     var $_del_suffix '</del>';
  45.  
  46.     /**
  47.      * Header for each change block.
  48.      */
  49.     var $_block_header '';
  50.  
  51.     /**
  52.      * What are we currently splitting on? Used to recurse to show word-level
  53.      * changes.
  54.      */
  55.     var $_split_level 'lines';
  56.  
  57.     function _blockHeader($xbeg$xlen$ybeg$ylen)
  58.     {
  59.         return $this->_block_header;
  60.     }
  61.  
  62.     function _startBlock($header)
  63.     {
  64.         return $header;
  65.     }
  66.  
  67.     function _lines($lines$prefix ' '$encode = true)
  68.     {
  69.         if ($encode{
  70.             array_walk($linesarray(&$this'_encode'));
  71.         }
  72.  
  73.         if ($this->_split_level == 'words'{
  74.             return implode(''$lines);
  75.         else {
  76.             return implode("\n"$lines"\n";
  77.         }
  78.     }
  79.  
  80.     function _added($lines)
  81.     {
  82.         array_walk($linesarray(&$this'_encode'));
  83.         $lines[0$this->_ins_prefix $lines[0];
  84.         $lines[count($lines- 1.= $this->_ins_suffix;
  85.         return $this->_lines($lines' 'false);
  86.     }
  87.  
  88.     function _deleted($lines$words = false)
  89.     {
  90.         array_walk($linesarray(&$this'_encode'));
  91.         $lines[0$this->_del_prefix $lines[0];
  92.         $lines[count($lines- 1.= $this->_del_suffix;
  93.         return $this->_lines($lines' 'false);
  94.     }
  95.  
  96.     function _changed($orig$final)
  97.     {
  98.         /* If we've already split on words, don't try to do so again - just
  99.          * display. */
  100.         if ($this->_split_level == 'words'{
  101.             $prefix '';
  102.             while ($orig[0!== false && $final[0!== false &&
  103.                    substr($orig[0]01== ' ' &&
  104.                    substr($final[0]01== ' '{
  105.                 $prefix .= substr($orig[0]01);
  106.                 $orig[0substr($orig[0]1);
  107.                 $final[0substr($final[0]1);
  108.             }
  109.             return $prefix $this->_deleted($orig$this->_added($final);
  110.         }
  111.  
  112.         $text1 implode("\n"$orig);
  113.         $text2 implode("\n"$final);
  114.  
  115.         /* Non-printing newline marker. */
  116.         $nl "\0";
  117.  
  118.         /* We want to split on word boundaries, but we need to
  119.          * preserve whitespace as well. Therefore we split on words,
  120.          * but include all blocks of whitespace in the wordlist. */
  121.         $diff &new Text_Diff($this->_splitOnWords($text1$nl),
  122.                                $this->_splitOnWords($text2$nl));
  123.  
  124.         /* Get the diff in inline format. */
  125.         $renderer &new Text_Diff_Renderer_inline(array_merge($this->getParams(),
  126.                                                                array('split_level' => 'words')));
  127.  
  128.         /* Run the diff and get the output. */
  129.         return str_replace($nl"\n"$renderer->render($diff)) "\n";
  130.     }
  131.  
  132.     function _splitOnWords($string$newlineEscape "\n")
  133.     {
  134.         $words = array();
  135.         $length strlen($string);
  136.         $pos = 0;
  137.  
  138.         while ($pos $length{
  139.             // Eat a word with any preceding whitespace.
  140.             $spaces strspn(substr($string$pos)" \n");
  141.             $nextpos strcspn(substr($string$pos $spaces)" \n");
  142.             $words[str_replace("\n"$newlineEscapesubstr($string$pos$spaces $nextpos));
  143.             $pos += $spaces $nextpos;
  144.         }
  145.  
  146.         return $words;
  147.     }
  148.  
  149.     function _encode(&$string)
  150.     {
  151.         $string htmlspecialchars($string);
  152.     }
  153.  
  154. }

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