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

Documentation generated on Fri, 24 Jul 2009 15:00:03 +0000 by phpDocumentor 1.4.2. PEAR Logo Copyright © PHP Group 2004.