-
+ 3AD17908113FF5190FA8FC1EC967F1713C879881993D7C895AD157BA2B70C6C6549F8D21730B2E15FDD7A4B71023256E09AA87E22E9196BF1C8F5DC2FA16E3C0
mp-wp/wp-includes/Text/Diff/Renderer/inline.php
(0 . 0)(1 . 172)
70009 <?php
70010 /**
70011 * "Inline" diff renderer.
70012 *
70013 * $Horde: framework/Text_Diff/Diff/Renderer/inline.php,v 1.21 2008/01/04 10:07:51 jan Exp $
70014 *
70015 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
70016 *
70017 * See the enclosed file COPYING for license information (LGPL). If you did
70018 * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
70019 *
70020 * @author Ciprian Popovici
70021 * @package Text_Diff
70022 */
70023
70024 /** Text_Diff_Renderer */
70025
70026 // WP #7391
70027 require_once dirname(dirname(__FILE__)) . '/Renderer.php';
70028
70029 /**
70030 * "Inline" diff renderer.
70031 *
70032 * This class renders diffs in the Wiki-style "inline" format.
70033 *
70034 * @author Ciprian Popovici
70035 * @package Text_Diff
70036 */
70037 class Text_Diff_Renderer_inline extends Text_Diff_Renderer {
70038
70039 /**
70040 * Number of leading context "lines" to preserve.
70041 */
70042 var $_leading_context_lines = 10000;
70043
70044 /**
70045 * Number of trailing context "lines" to preserve.
70046 */
70047 var $_trailing_context_lines = 10000;
70048
70049 /**
70050 * Prefix for inserted text.
70051 */
70052 var $_ins_prefix = '<ins>';
70053
70054 /**
70055 * Suffix for inserted text.
70056 */
70057 var $_ins_suffix = '</ins>';
70058
70059 /**
70060 * Prefix for deleted text.
70061 */
70062 var $_del_prefix = '<del>';
70063
70064 /**
70065 * Suffix for deleted text.
70066 */
70067 var $_del_suffix = '</del>';
70068
70069 /**
70070 * Header for each change block.
70071 */
70072 var $_block_header = '';
70073
70074 /**
70075 * What are we currently splitting on? Used to recurse to show word-level
70076 * changes.
70077 */
70078 var $_split_level = 'lines';
70079
70080 function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
70081 {
70082 return $this->_block_header;
70083 }
70084
70085 function _startBlock($header)
70086 {
70087 return $header;
70088 }
70089
70090 function _lines($lines, $prefix = ' ', $encode = true)
70091 {
70092 if ($encode) {
70093 array_walk($lines, array(&$this, '_encode'));
70094 }
70095
70096 if ($this->_split_level == 'words') {
70097 return implode('', $lines);
70098 } else {
70099 return implode("\n", $lines) . "\n";
70100 }
70101 }
70102
70103 function _added($lines)
70104 {
70105 array_walk($lines, array(&$this, '_encode'));
70106 $lines[0] = $this->_ins_prefix . $lines[0];
70107 $lines[count($lines) - 1] .= $this->_ins_suffix;
70108 return $this->_lines($lines, ' ', false);
70109 }
70110
70111 function _deleted($lines, $words = false)
70112 {
70113 array_walk($lines, array(&$this, '_encode'));
70114 $lines[0] = $this->_del_prefix . $lines[0];
70115 $lines[count($lines) - 1] .= $this->_del_suffix;
70116 return $this->_lines($lines, ' ', false);
70117 }
70118
70119 function _changed($orig, $final)
70120 {
70121 /* If we've already split on words, don't try to do so again - just
70122 * display. */
70123 if ($this->_split_level == 'words') {
70124 $prefix = '';
70125 while ($orig[0] !== false && $final[0] !== false &&
70126 substr($orig[0], 0, 1) == ' ' &&
70127 substr($final[0], 0, 1) == ' ') {
70128 $prefix .= substr($orig[0], 0, 1);
70129 $orig[0] = substr($orig[0], 1);
70130 $final[0] = substr($final[0], 1);
70131 }
70132 return $prefix . $this->_deleted($orig) . $this->_added($final);
70133 }
70134
70135 $text1 = implode("\n", $orig);
70136 $text2 = implode("\n", $final);
70137
70138 /* Non-printing newline marker. */
70139 $nl = "\0";
70140
70141 /* We want to split on word boundaries, but we need to
70142 * preserve whitespace as well. Therefore we split on words,
70143 * but include all blocks of whitespace in the wordlist. */
70144 $diff = new Text_Diff($this->_splitOnWords($text1, $nl),
70145 $this->_splitOnWords($text2, $nl));
70146
70147 /* Get the diff in inline format. */
70148 $renderer = new Text_Diff_Renderer_inline(array_merge($this->getParams(),
70149 array('split_level' => 'words')));
70150
70151 /* Run the diff and get the output. */
70152 return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
70153 }
70154
70155 function _splitOnWords($string, $newlineEscape = "\n")
70156 {
70157 // Ignore \0; otherwise the while loop will never finish.
70158 $string = str_replace("\0", '', $string);
70159
70160 $words = array();
70161 $length = strlen($string);
70162 $pos = 0;
70163
70164 while ($pos < $length) {
70165 // Eat a word with any preceding whitespace.
70166 $spaces = strspn(substr($string, $pos), " \n");
70167 $nextpos = strcspn(substr($string, $pos + $spaces), " \n");
70168 $words[] = str_replace("\n", $newlineEscape, substr($string, $pos, $spaces + $nextpos));
70169 $pos += $spaces + $nextpos;
70170 }
70171
70172 return $words;
70173 }
70174
70175 function _encode(&$string)
70176 {
70177 $string = htmlspecialchars($string);
70178 }
70179
70180 }