3 * A class to render Diffs in different formats.
5 * This class renders the diff in classic diff format. It is intended that
6 * this class be customized via inheritance, to obtain fancier outputs.
8 * $Horde: framework/Text_Diff/Diff/Renderer.php,v 1.21 2008/01/04 10:07:50 jan Exp $
10 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
12 * See the enclosed file COPYING for license information (LGPL). If you did
13 * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
17 class Text_Diff_Renderer {
20 * Number of leading context "lines" to preserve.
22 * This should be left at zero for this class, but subclasses may want to
23 * set this to other values.
25 var $_leading_context_lines = 0;
28 * Number of trailing context "lines" to preserve.
30 * This should be left at zero for this class, but subclasses may want to
31 * set this to other values.
33 var $_trailing_context_lines = 0;
38 function Text_Diff_Renderer($params = array())
40 foreach ($params as $param => $value) {
42 if (isset($this->$v)) {
49 * Get any renderer parameters.
51 * @return array All parameters of this renderer object.
56 foreach (get_object_vars($this) as $k => $v) {
58 $params[substr($k, 1)] = $v;
68 * @param Text_Diff $diff A Text_Diff object.
70 * @return string The formatted output.
72 function render($diff)
78 $nlead = $this->_leading_context_lines;
79 $ntrail = $this->_trailing_context_lines;
81 $output = $this->_startDiff();
83 $diffs = $diff->getDiff();
84 foreach ($diffs as $i => $edit) {
85 /* If these are unchanged (copied) lines, and we want to keep
86 * leading or trailing context lines, extract them from the copy
88 if (is_a($edit, 'Text_Diff_Op_copy')) {
89 /* Do we have any diff blocks yet? */
90 if (is_array($block)) {
91 /* How many lines to keep as context from the copy
93 $keep = $i == count($diffs) - 1 ? $ntrail : $nlead + $ntrail;
94 if (count($edit->orig) <= $keep) {
95 /* We have less lines in the block than we want for
96 * context => keep the whole block. */
100 /* Create a new block with as many lines as we need
101 * for the trailing context. */
102 $context = array_slice($edit->orig, 0, $ntrail);
103 $block[] = &new Text_Diff_Op_copy($context);
106 $output .= $this->_block($x0, $ntrail + $xi - $x0,
107 $y0, $ntrail + $yi - $y0,
112 /* Keep the copy block as the context for the next block. */
113 $context = $edit->orig;
115 /* Don't we have any diff blocks yet? */
116 if (!is_array($block)) {
117 /* Extract context lines from the preceding copy block. */
118 $context = array_slice($context, count($context) - $nlead);
119 $x0 = $xi - count($context);
120 $y0 = $yi - count($context);
123 $block[] = &new Text_Diff_Op_copy($context);
130 $xi += count($edit->orig);
133 $yi += count($edit->final);
137 if (is_array($block)) {
138 $output .= $this->_block($x0, $xi - $x0,
143 return $output . $this->_endDiff();
146 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits)
148 $output = $this->_startBlock($this->_blockHeader($xbeg, $xlen, $ybeg, $ylen));
150 foreach ($edits as $edit) {
151 switch (strtolower(get_class($edit))) {
152 case 'text_diff_op_copy':
153 $output .= $this->_context($edit->orig);
156 case 'text_diff_op_add':
157 $output .= $this->_added($edit->final);
160 case 'text_diff_op_delete':
161 $output .= $this->_deleted($edit->orig);
164 case 'text_diff_op_change':
165 $output .= $this->_changed($edit->orig, $edit->final);
170 return $output . $this->_endBlock();
173 function _startDiff()
183 function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
186 $xbeg .= ',' . ($xbeg + $xlen - 1);
189 $ybeg .= ',' . ($ybeg + $ylen - 1);
192 // this matches the GNU Diff behaviour
193 if ($xlen && !$ylen) {
199 return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
202 function _startBlock($header)
204 return $header . "\n";
212 function _lines($lines, $prefix = ' ')
214 return $prefix . implode("\n$prefix", $lines) . "\n";
217 function _context($lines)
219 return $this->_lines($lines, ' ');
222 function _added($lines)
224 return $this->_lines($lines, '> ');
227 function _deleted($lines)
229 return $this->_lines($lines, '< ');
232 function _changed($orig, $final)
234 return $this->_deleted($orig) . "---\n" . $this->_added($final);