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.13 2005/06/29 00:21:43 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 _blockHeader($xbeg, $xlen, $ybeg, $ylen)
  55.     {
  56.         return $this->_block_header;
  57.     }
  58.  
  59.     function _startBlock($header)
  60.     {
  61.         return $header;
  62.     }
  63.  
  64.     function _lines($lines, $prefix = ' ', $encode = true)
  65.     {
  66.         if ($encode) {
  67.             array_walk($lines, array(&$this, '_encode'));
  68.         }
  69.  
  70.         if ($this->_split_level == 'words') {
  71.             return implode('', $lines);
  72.         } else {
  73.             return implode("\n", $lines) . "\n";
  74.         }
  75.     }
  76.  
  77.     function _added($lines)
  78.     {
  79.         array_walk($lines, array(&$this, '_encode'));
  80.         $lines[0] = $this->_ins_prefix . $lines[0];
  81.         $lines[count($lines) - 1] .= $this->_ins_suffix;
  82.         return $this->_lines($lines, ' ', false);
  83.     }
  84.  
  85.     function _deleted($lines, $words = false)
  86.     {
  87.         array_walk($lines, array(&$this, '_encode'));
  88.         $lines[0] = $this->_del_prefix . $lines[0];
  89.         $lines[count($lines) - 1] .= $this->_del_suffix;
  90.         return $this->_lines($lines, ' ', false);
  91.     }
  92.  
  93.     function _changed($orig, $final)
  94.     {
  95.         /* If we've already split on words, don't try to do so again - just
  96.          * display. */
  97.         if ($this->_split_level == 'words') {
  98.             $prefix = '';
  99.             while ($orig[0] !== false && $final[0] !== false &&
  100.                    substr($orig[0], 0, 1) == ' ' &&
  101.                    substr($final[0], 0, 1) == ' ') {
  102.                 $prefix .= substr($orig[0], 0, 1);
  103.                 $orig[0] = substr($orig[0], 1);
  104.                 $final[0] = substr($final[0], 1);
  105.             }
  106.             return $prefix . $this->_deleted($orig) . $this->_added($final);
  107.         }
  108.  
  109.         $text1 = implode("\n", $orig);
  110.         $text2 = implode("\n", $final);
  111.  
  112.         /* Non-printing newline marker. */
  113.         $nl = "\0";
  114.  
  115.         /* We want to split on word boundaries, but we need to
  116.          * preserve whitespace as well. Therefore we split on words,
  117.          * but include all blocks of whitespace in the wordlist. */
  118.         $diff = &new Text_Diff($this->_splitOnWords($text1, $nl),
  119.                                $this->_splitOnWords($text2, $nl));
  120.  
  121.         /* Get the diff in inline format. */
  122.         $renderer = &new Text_Diff_Renderer_inline(array_merge($this->getParams(),
  123.                                                                array('split_level' => 'words')));
  124.  
  125.         /* Run the diff and get the output. */
  126.         return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
  127.     }
  128.  
  129.     function _splitOnWords($string, $newlineEscape = "\n")
  130.     {
  131.         $words = array();
  132.         $length = strlen($string);
  133.         $pos = 0;
  134.  
  135.         while ($pos < $length) {
  136.             // Eat a word with any preceding whitespace.
  137.             $spaces = strspn($string, " \n", $pos);
  138.             $nextpos = strcspn($string, " \n", $pos + $spaces);
  139.             $words[] = str_replace("\n", $newlineEscape, substr($string, $pos, $spaces + $nextpos));
  140.             $pos += $spaces + $nextpos;
  141.         }
  142.  
  143.         return $words;
  144.     }
  145.  
  146.     function _encode(&$string)
  147.     {
  148.         $string = htmlspecialchars($string);
  149.     }
  150.  
  151. }

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