3 * "Inline" diff renderer.
5 * $Horde: framework/Text_Diff/Diff/Renderer/inline.php,v 1.21 2008/01/04 10:07:51 jan Exp $
7 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
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.
12 * @author Ciprian Popovici
16 /** Text_Diff_Renderer */
19 require_once dirname(dirname(__FILE__)) . '/Renderer.php';
22 * "Inline" diff renderer.
24 * This class renders diffs in the Wiki-style "inline" format.
26 * @author Ciprian Popovici
29 class Text_Diff_Renderer_inline extends Text_Diff_Renderer {
32 * Number of leading context "lines" to preserve.
34 var $_leading_context_lines = 10000;
37 * Number of trailing context "lines" to preserve.
39 var $_trailing_context_lines = 10000;
42 * Prefix for inserted text.
44 var $_ins_prefix = '<ins>';
47 * Suffix for inserted text.
49 var $_ins_suffix = '</ins>';
52 * Prefix for deleted text.
54 var $_del_prefix = '<del>';
57 * Suffix for deleted text.
59 var $_del_suffix = '</del>';
62 * Header for each change block.
64 var $_block_header = '';
67 * What are we currently splitting on? Used to recurse to show word-level
70 var $_split_level = 'lines';
72 function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
74 return $this->_block_header;
77 function _startBlock($header)
82 function _lines($lines, $prefix = ' ', $encode = true)
85 array_walk($lines, array(&$this, '_encode'));
88 if ($this->_split_level == 'words') {
89 return implode('', $lines);
91 return implode("\n", $lines) . "\n";
95 function _added($lines)
97 array_walk($lines, array(&$this, '_encode'));
98 $lines[0] = $this->_ins_prefix . $lines[0];
99 $lines[count($lines) - 1] .= $this->_ins_suffix;
100 return $this->_lines($lines, ' ', false);
103 function _deleted($lines, $words = false)
105 array_walk($lines, array(&$this, '_encode'));
106 $lines[0] = $this->_del_prefix . $lines[0];
107 $lines[count($lines) - 1] .= $this->_del_suffix;
108 return $this->_lines($lines, ' ', false);
111 function _changed($orig, $final)
113 /* If we've already split on words, don't try to do so again - just
115 if ($this->_split_level == 'words') {
117 while ($orig[0] !== false && $final[0] !== false &&
118 substr($orig[0], 0, 1) == ' ' &&
119 substr($final[0], 0, 1) == ' ') {
120 $prefix .= substr($orig[0], 0, 1);
121 $orig[0] = substr($orig[0], 1);
122 $final[0] = substr($final[0], 1);
124 return $prefix . $this->_deleted($orig) . $this->_added($final);
127 $text1 = implode("\n", $orig);
128 $text2 = implode("\n", $final);
130 /* Non-printing newline marker. */
133 /* We want to split on word boundaries, but we need to
134 * preserve whitespace as well. Therefore we split on words,
135 * but include all blocks of whitespace in the wordlist. */
136 $diff = new Text_Diff($this->_splitOnWords($text1, $nl),
137 $this->_splitOnWords($text2, $nl));
139 /* Get the diff in inline format. */
140 $renderer = new Text_Diff_Renderer_inline(array_merge($this->getParams(),
141 array('split_level' => 'words')));
143 /* Run the diff and get the output. */
144 return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
147 function _splitOnWords($string, $newlineEscape = "\n")
149 // Ignore \0; otherwise the while loop will never finish.
150 $string = str_replace("\0", '', $string);
153 $length = strlen($string);
156 while ($pos < $length) {
157 // Eat a word with any preceding whitespace.
158 $spaces = strspn(substr($string, $pos), " \n");
159 $nextpos = strcspn(substr($string, $pos + $spaces), " \n");
160 $words[] = str_replace("\n", $newlineEscape, substr($string, $pos, $spaces + $nextpos));
161 $pos += $spaces + $nextpos;
167 function _encode(&$string)
169 $string = htmlspecialchars($string);