web/wp-includes/Text/Diff/Renderer/inline.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * "Inline" diff renderer.
     3  * "Inline" diff renderer.
     4  *
     4  *
     5  * $Horde: framework/Text_Diff/Diff/Renderer/inline.php,v 1.21 2008/01/04 10:07:51 jan Exp $
     5  * Copyright 2004-2010 The Horde Project (http://www.horde.org/)
     6  *
       
     7  * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
       
     8  *
     6  *
     9  * See the enclosed file COPYING for license information (LGPL). If you did
     7  * 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.
     8  * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
    11  *
     9  *
    12  * @author  Ciprian Popovici
    10  * @author  Ciprian Popovici
    28  */
    26  */
    29 class Text_Diff_Renderer_inline extends Text_Diff_Renderer {
    27 class Text_Diff_Renderer_inline extends Text_Diff_Renderer {
    30 
    28 
    31     /**
    29     /**
    32      * Number of leading context "lines" to preserve.
    30      * Number of leading context "lines" to preserve.
       
    31      *
       
    32      * @var integer
    33      */
    33      */
    34     var $_leading_context_lines = 10000;
    34     var $_leading_context_lines = 10000;
    35 
    35 
    36     /**
    36     /**
    37      * Number of trailing context "lines" to preserve.
    37      * Number of trailing context "lines" to preserve.
       
    38      *
       
    39      * @var integer
    38      */
    40      */
    39     var $_trailing_context_lines = 10000;
    41     var $_trailing_context_lines = 10000;
    40 
    42 
    41     /**
    43     /**
    42      * Prefix for inserted text.
    44      * Prefix for inserted text.
       
    45      *
       
    46      * @var string
    43      */
    47      */
    44     var $_ins_prefix = '<ins>';
    48     var $_ins_prefix = '<ins>';
    45 
    49 
    46     /**
    50     /**
    47      * Suffix for inserted text.
    51      * Suffix for inserted text.
       
    52      *
       
    53      * @var string
    48      */
    54      */
    49     var $_ins_suffix = '</ins>';
    55     var $_ins_suffix = '</ins>';
    50 
    56 
    51     /**
    57     /**
    52      * Prefix for deleted text.
    58      * Prefix for deleted text.
       
    59      *
       
    60      * @var string
    53      */
    61      */
    54     var $_del_prefix = '<del>';
    62     var $_del_prefix = '<del>';
    55 
    63 
    56     /**
    64     /**
    57      * Suffix for deleted text.
    65      * Suffix for deleted text.
       
    66      *
       
    67      * @var string
    58      */
    68      */
    59     var $_del_suffix = '</del>';
    69     var $_del_suffix = '</del>';
    60 
    70 
    61     /**
    71     /**
    62      * Header for each change block.
    72      * Header for each change block.
       
    73      *
       
    74      * @var string
    63      */
    75      */
    64     var $_block_header = '';
    76     var $_block_header = '';
    65 
    77 
    66     /**
    78     /**
       
    79      * Whether to split down to character-level.
       
    80      *
       
    81      * @var boolean
       
    82      */
       
    83     var $_split_characters = false;
       
    84 
       
    85     /**
    67      * What are we currently splitting on? Used to recurse to show word-level
    86      * What are we currently splitting on? Used to recurse to show word-level
    68      * changes.
    87      * or character-level changes.
       
    88      *
       
    89      * @var string
    69      */
    90      */
    70     var $_split_level = 'lines';
    91     var $_split_level = 'lines';
    71 
    92 
    72     function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
    93     function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
    73     {
    94     {
    83     {
   104     {
    84         if ($encode) {
   105         if ($encode) {
    85             array_walk($lines, array(&$this, '_encode'));
   106             array_walk($lines, array(&$this, '_encode'));
    86         }
   107         }
    87 
   108 
    88         if ($this->_split_level == 'words') {
   109         if ($this->_split_level == 'lines') {
       
   110             return implode("\n", $lines) . "\n";
       
   111         } else {
    89             return implode('', $lines);
   112             return implode('', $lines);
    90         } else {
       
    91             return implode("\n", $lines) . "\n";
       
    92         }
   113         }
    93     }
   114     }
    94 
   115 
    95     function _added($lines)
   116     function _added($lines)
    96     {
   117     {
   108         return $this->_lines($lines, ' ', false);
   129         return $this->_lines($lines, ' ', false);
   109     }
   130     }
   110 
   131 
   111     function _changed($orig, $final)
   132     function _changed($orig, $final)
   112     {
   133     {
   113         /* If we've already split on words, don't try to do so again - just
   134         /* If we've already split on characters, just display. */
   114          * display. */
   135         if ($this->_split_level == 'characters') {
       
   136             return $this->_deleted($orig)
       
   137                 . $this->_added($final);
       
   138         }
       
   139 
       
   140         /* If we've already split on words, just display. */
   115         if ($this->_split_level == 'words') {
   141         if ($this->_split_level == 'words') {
   116             $prefix = '';
   142             $prefix = '';
   117             while ($orig[0] !== false && $final[0] !== false &&
   143             while ($orig[0] !== false && $final[0] !== false &&
   118                    substr($orig[0], 0, 1) == ' ' &&
   144                    substr($orig[0], 0, 1) == ' ' &&
   119                    substr($final[0], 0, 1) == ' ') {
   145                    substr($final[0], 0, 1) == ' ') {
   128         $text2 = implode("\n", $final);
   154         $text2 = implode("\n", $final);
   129 
   155 
   130         /* Non-printing newline marker. */
   156         /* Non-printing newline marker. */
   131         $nl = "\0";
   157         $nl = "\0";
   132 
   158 
   133         /* We want to split on word boundaries, but we need to
   159         if ($this->_split_characters) {
   134          * preserve whitespace as well. Therefore we split on words,
   160             $diff = new Text_Diff('native',
   135          * but include all blocks of whitespace in the wordlist. */
   161                                   array(preg_split('//', $text1),
   136         $diff = new Text_Diff($this->_splitOnWords($text1, $nl),
   162                                         preg_split('//', $text2)));
   137                               $this->_splitOnWords($text2, $nl));
   163         } else {
       
   164             /* We want to split on word boundaries, but we need to preserve
       
   165              * whitespace as well. Therefore we split on words, but include
       
   166              * all blocks of whitespace in the wordlist. */
       
   167             $diff = new Text_Diff('native',
       
   168                                   array($this->_splitOnWords($text1, $nl),
       
   169                                         $this->_splitOnWords($text2, $nl)));
       
   170         }
   138 
   171 
   139         /* Get the diff in inline format. */
   172         /* Get the diff in inline format. */
   140         $renderer = new Text_Diff_Renderer_inline(array_merge($this->getParams(),
   173         $renderer = new Text_Diff_Renderer_inline
   141                                                               array('split_level' => 'words')));
   174             (array_merge($this->getParams(),
       
   175                          array('split_level' => $this->_split_characters ? 'characters' : 'words')));
   142 
   176 
   143         /* Run the diff and get the output. */
   177         /* Run the diff and get the output. */
   144         return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
   178         return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
   145     }
   179     }
   146 
   180