b2evolution PHP Cross Reference Blogging Systems

Source: /inc/_ext/phpsvnclient/ext/Diff/Diff/Renderer/inline.php - 174 lines - 4850 bytes - Summary - Text - Print

Description: "Inline" diff renderer. $Horde: framework/Text_Diff/Diff/Renderer/inline.php,v 1.4.10.16 2009/07/24 13:25:29 jan Exp $

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

title

Description

title

Description

title

Description

title

title

Body