[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Text/Diff/Renderer/ -> inline.php (source)

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


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7