| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * A class to render Diffs in different formats. |
|---|
| 4 | * |
|---|
| 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. |
|---|
| 7 | * |
|---|
| 8 | * $Horde: framework/Text_Diff/Diff/Renderer.php,v 1.9 2005/05/04 20:21:52 chuck Exp $ |
|---|
| 9 | * |
|---|
| 10 | * @package Text_Diff |
|---|
| 11 | */ |
|---|
| 12 | class Text_Diff_Renderer { |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * Number of leading context "lines" to preserve. |
|---|
| 16 | * |
|---|
| 17 | * This should be left at zero for this class, but subclasses may want to |
|---|
| 18 | * set this to other values. |
|---|
| 19 | */ |
|---|
| 20 | var $_leading_context_lines = 0; |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * Number of trailing context "lines" to preserve. |
|---|
| 24 | * |
|---|
| 25 | * This should be left at zero for this class, but subclasses may want to |
|---|
| 26 | * set this to other values. |
|---|
| 27 | */ |
|---|
| 28 | var $_trailing_context_lines = 0; |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Constructor. |
|---|
| 32 | */ |
|---|
| 33 | function Text_Diff_Renderer($params = array()) |
|---|
| 34 | { |
|---|
| 35 | foreach ($params as $param => $value) { |
|---|
| 36 | $v = '_' . $param; |
|---|
| 37 | if (isset($this->$v)) { |
|---|
| 38 | $this->$v = $value; |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Get any renderer parameters. |
|---|
| 45 | * |
|---|
| 46 | * @return array All parameters of this renderer object. |
|---|
| 47 | */ |
|---|
| 48 | function getParams() |
|---|
| 49 | { |
|---|
| 50 | $params = array(); |
|---|
| 51 | foreach (get_object_vars($this) as $k => $v) { |
|---|
| 52 | if ($k{0} == '_') { |
|---|
| 53 | $params[substr($k, 1)] = $v; |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | return $params; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Renders a diff. |
|---|
| 62 | * |
|---|
| 63 | * @param Text_Diff $diff A Text_Diff object. |
|---|
| 64 | * |
|---|
| 65 | * @return string The formatted output. |
|---|
| 66 | */ |
|---|
| 67 | function render($diff) |
|---|
| 68 | { |
|---|
| 69 | $xi = $yi = 1; |
|---|
| 70 | $block = false; |
|---|
| 71 | $context = array(); |
|---|
| 72 | |
|---|
| 73 | $nlead = $this->_leading_context_lines; |
|---|
| 74 | $ntrail = $this->_trailing_context_lines; |
|---|
| 75 | |
|---|
| 76 | $output = $this->_startDiff(); |
|---|
| 77 | |
|---|
| 78 | foreach ($diff->getDiff() as $edit) { |
|---|
| 79 | if (is_a($edit, 'Text_Diff_Op_copy')) { |
|---|
| 80 | if (is_array($block)) { |
|---|
| 81 | if (count($edit->orig) <= $nlead + $ntrail) { |
|---|
| 82 | $block[] = $edit; |
|---|
| 83 | } else { |
|---|
| 84 | if ($ntrail) { |
|---|
| 85 | $context = array_slice($edit->orig, 0, $ntrail); |
|---|
| 86 | $block[] = &new Text_Diff_Op_copy($context); |
|---|
| 87 | } |
|---|
| 88 | $output .= $this->_block($x0, $ntrail + $xi - $x0, |
|---|
| 89 | $y0, $ntrail + $yi - $y0, |
|---|
| 90 | $block); |
|---|
| 91 | $block = false; |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | $context = $edit->orig; |
|---|
| 95 | } else { |
|---|
| 96 | if (!is_array($block)) { |
|---|
| 97 | $context = array_slice($context, count($context) - $nlead); |
|---|
| 98 | $x0 = $xi - count($context); |
|---|
| 99 | $y0 = $yi - count($context); |
|---|
| 100 | $block = array(); |
|---|
| 101 | if ($context) { |
|---|
| 102 | $block[] = &new Text_Diff_Op_copy($context); |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | $block[] = $edit; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | if ($edit->orig) { |
|---|
| 109 | $xi += count($edit->orig); |
|---|
| 110 | } |
|---|
| 111 | if ($edit->final) { |
|---|
| 112 | $yi += count($edit->final); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | if (is_array($block)) { |
|---|
| 117 | $output .= $this->_block($x0, $xi - $x0, |
|---|
| 118 | $y0, $yi - $y0, |
|---|
| 119 | $block); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | return $output . $this->_endDiff(); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) |
|---|
| 126 | { |
|---|
| 127 | $output = $this->_startBlock($this->_blockHeader($xbeg, $xlen, $ybeg, $ylen)); |
|---|
| 128 | |
|---|
| 129 | foreach ($edits as $edit) { |
|---|
| 130 | switch (strtolower(get_class($edit))) { |
|---|
| 131 | case 'text_diff_op_copy': |
|---|
| 132 | $output .= $this->_context($edit->orig); |
|---|
| 133 | break; |
|---|
| 134 | |
|---|
| 135 | case 'text_diff_op_add': |
|---|
| 136 | $output .= $this->_added($edit->final); |
|---|
| 137 | break; |
|---|
| 138 | |
|---|
| 139 | case 'text_diff_op_delete': |
|---|
| 140 | $output .= $this->_deleted($edit->orig); |
|---|
| 141 | break; |
|---|
| 142 | |
|---|
| 143 | case 'text_diff_op_change': |
|---|
| 144 | $output .= $this->_changed($edit->orig, $edit->final); |
|---|
| 145 | break; |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | return $output . $this->_endBlock(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | function _startDiff() |
|---|
| 153 | { |
|---|
| 154 | return ''; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | function _endDiff() |
|---|
| 158 | { |
|---|
| 159 | return ''; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | function _blockHeader($xbeg, $xlen, $ybeg, $ylen) |
|---|
| 163 | { |
|---|
| 164 | if ($xlen > 1) { |
|---|
| 165 | $xbeg .= ',' . ($xbeg + $xlen - 1); |
|---|
| 166 | } |
|---|
| 167 | if ($ylen > 1) { |
|---|
| 168 | $ybeg .= ',' . ($ybeg + $ylen - 1); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | function _startBlock($header) |
|---|
| 175 | { |
|---|
| 176 | return $header . "\n"; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | function _endBlock() |
|---|
| 180 | { |
|---|
| 181 | return ''; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | function _lines($lines, $prefix = ' ') |
|---|
| 185 | { |
|---|
| 186 | return $prefix . implode("\n$prefix", $lines) . "\n"; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | function _context($lines) |
|---|
| 190 | { |
|---|
| 191 | return $this->_lines($lines); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | function _added($lines) |
|---|
| 195 | { |
|---|
| 196 | return $this->_lines($lines, '>'); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | function _deleted($lines) |
|---|
| 200 | { |
|---|
| 201 | return $this->_lines($lines, '<'); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | function _changed($orig, $final) |
|---|
| 205 | { |
|---|
| 206 | return $this->_deleted($orig) . "---\n" . $this->_added($final); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | } |
|---|