[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/3rdparty/diff/Diff/Renderer/ -> Inline.php (source)

   1  <?php
   2  /**
   3   * "Inline" diff renderer.
   4   *
   5   * This class renders diffs in the Wiki-style "inline" format.
   6   *
   7   * Copyright 2004-2011 Horde LLC (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://www.horde.org/licenses/lgpl21.
  11   *
  12   * @author  Ciprian Popovici
  13   * @package Text_Diff
  14   */
  15  
  16  // Disallow direct access to this file for security reasons
  17  if(!defined("IN_MYBB"))
  18  {
  19      die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
  20  }
  21  
  22  class Horde_Text_Diff_Renderer_Inline extends Horde_Text_Diff_Renderer
  23  {
  24      /**
  25       * Number of leading context "lines" to preserve.
  26       *
  27       * @var integer
  28       */
  29      protected $_leading_context_lines = 10000;
  30  
  31      /**
  32       * Number of trailing context "lines" to preserve.
  33       *
  34       * @var integer
  35       */
  36      protected $_trailing_context_lines = 10000;
  37  
  38      /**
  39       * Prefix for inserted text.
  40       *
  41       * @var string
  42       */
  43      protected $_ins_prefix = '<ins>';
  44  
  45      /**
  46       * Suffix for inserted text.
  47       *
  48       * @var string
  49       */
  50      protected $_ins_suffix = '</ins>';
  51  
  52      /**
  53       * Prefix for deleted text.
  54       *
  55       * @var string
  56       */
  57      protected $_del_prefix = '<del>';
  58  
  59      /**
  60       * Suffix for deleted text.
  61       *
  62       * @var string
  63       */
  64      protected $_del_suffix = '</del>';
  65  
  66      /**
  67       * Header for each change block.
  68       *
  69       * @var string
  70       */
  71      protected $_block_header = '';
  72  
  73      /**
  74       * Whether to split down to character-level.
  75       *
  76       * @var boolean
  77       */
  78      protected $_split_characters = false;
  79  
  80      /**
  81       * What are we currently splitting on? Used to recurse to show word-level
  82       * or character-level changes.
  83       *
  84       * @var string
  85       */
  86      protected $_split_level = 'lines';
  87  
  88      protected function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
  89      {
  90          return $this->_block_header;
  91      }
  92  
  93      protected function _startBlock($header)
  94      {
  95          return $header;
  96      }
  97  
  98      protected function _lines($lines, $prefix = ' ', $encode = true)
  99      {
 100          if ($encode) {
 101              array_walk($lines, array(&$this, '_encode'));
 102          }
 103  
 104          if ($this->_split_level == 'lines') {
 105              return implode("\n", $lines) . "\n";
 106          } else {
 107              return implode('', $lines);
 108          }
 109      }
 110  
 111      protected function _added($lines)
 112      {
 113          array_walk($lines, array(&$this, '_encode'));
 114          $lines[0] = $this->_ins_prefix . $lines[0];
 115          $lines[count($lines) - 1] .= $this->_ins_suffix;
 116          return $this->_lines($lines, ' ', false);
 117      }
 118  
 119      protected function _deleted($lines, $words = false)
 120      {
 121          array_walk($lines, array(&$this, '_encode'));
 122          $lines[0] = $this->_del_prefix . $lines[0];
 123          $lines[count($lines) - 1] .= $this->_del_suffix;
 124          return $this->_lines($lines, ' ', false);
 125      }
 126  
 127      protected function _changed($orig, $final)
 128      {
 129          /* If we've already split on characters, just display. */
 130          if ($this->_split_level == 'characters') {
 131              return $this->_deleted($orig)
 132                  . $this->_added($final);
 133          }
 134  
 135          /* If we've already split on words, just display. */
 136          if ($this->_split_level == 'words') {
 137              $prefix = '';
 138              while ($orig[0] !== false && $final[0] !== false &&
 139                     substr($orig[0], 0, 1) == ' ' &&
 140                     substr($final[0], 0, 1) == ' ') {
 141                  $prefix .= substr($orig[0], 0, 1);
 142                  $orig[0] = substr($orig[0], 1);
 143                  $final[0] = substr($final[0], 1);
 144              }
 145              return $prefix . $this->_deleted($orig) . $this->_added($final);
 146          }
 147  
 148          $text1 = implode("\n", $orig);
 149          $text2 = implode("\n", $final);
 150  
 151          /* Non-printing newline marker. */
 152          $nl = "\0";
 153  
 154          if ($this->_split_characters) {
 155              $diff = new Horde_Text_Diff('native',
 156                                    array(preg_split('//', $text1),
 157                                          preg_split('//', $text2)));
 158          } else {
 159              /* We want to split on word boundaries, but we need to preserve
 160               * whitespace as well. Therefore we split on words, but include
 161               * all blocks of whitespace in the wordlist. */
 162              $diff = new Horde_Text_Diff('native',
 163                                    array($this->_splitOnWords($text1, $nl),
 164                                          $this->_splitOnWords($text2, $nl)));
 165          }
 166  
 167          /* Get the diff in inline format. */
 168          $renderer = new Horde_Text_Diff_Renderer_inline
 169              (array_merge($this->getParams(),
 170                           array('split_level' => $this->_split_characters ? 'characters' : 'words')));
 171  
 172          /* Run the diff and get the output. */
 173          return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
 174      }
 175  
 176      protected function _splitOnWords($string, $newlineEscape = "\n")
 177      {
 178          // Ignore \0; otherwise the while loop will never finish.
 179          $string = str_replace("\0", '', $string);
 180  
 181          $words = array();
 182          $length = strlen($string);
 183          $pos = 0;
 184  
 185          while ($pos < $length) {
 186              // Eat a word with any preceding whitespace.
 187              $spaces = strspn(substr($string, $pos), " \n");
 188              $nextpos = strcspn(substr($string, $pos + $spaces), " \n");
 189              $words[] = str_replace("\n", $newlineEscape, substr($string, $pos, $spaces + $nextpos));
 190              $pos += $spaces + $nextpos;
 191          }
 192  
 193          return $words;
 194      }
 195  
 196      protected function _encode(&$string)
 197      {
 198          $string = htmlspecialchars($string);
 199      }
 200  }


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1