ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Renderer.php
Go to the documentation of this file.
1<?php
13
21
29
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
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
67 function render($diff)
68 {
69 $xi = $yi = 1;
70 $block = false;
71 $context = array();
72
75
76 $output = $this->_startDiff();
77
78 foreach ($diff->getDiff() as $edit) {
79 if ($edit instanceof 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
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}
_blockHeader($xbeg, $xlen, $ybeg, $ylen)
Definition: Renderer.php:162
$_leading_context_lines
Number of leading context "lines" to preserve.
Definition: Renderer.php:20
_startBlock($header)
Definition: Renderer.php:174
$_trailing_context_lines
Number of trailing context "lines" to preserve.
Definition: Renderer.php:28
_changed($orig, $final)
Definition: Renderer.php:204
_lines($lines, $prefix=' ')
Definition: Renderer.php:184
render($diff)
Renders a diff.
Definition: Renderer.php:67
getParams()
Get any renderer parameters.
Definition: Renderer.php:48
Text_Diff_Renderer($params=array())
Constructor.
Definition: Renderer.php:33
_block($xbeg, $xlen, $ybeg, $ylen, &$edits)
Definition: Renderer.php:125
$header
$params
Definition: example_049.php:96