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

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