1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Horde_Text_Diff_Renderer_Inline extends Horde_Text_Diff_Renderer
16: {
17: 18: 19: 20: 21:
22: protected $_leading_context_lines = 10000;
23:
24: 25: 26: 27: 28:
29: protected $_trailing_context_lines = 10000;
30:
31: 32: 33: 34: 35:
36: protected $_ins_prefix = '<ins>';
37:
38: 39: 40: 41: 42:
43: protected $_ins_suffix = '</ins>';
44:
45: 46: 47: 48: 49:
50: protected $_del_prefix = '<del>';
51:
52: 53: 54: 55: 56:
57: protected $_del_suffix = '</del>';
58:
59: 60: 61: 62: 63:
64: protected = '';
65:
66: 67: 68: 69: 70:
71: protected $_split_characters = false;
72:
73: 74: 75: 76: 77: 78:
79: protected $_split_level = 'lines';
80:
81: protected function ($xbeg, $xlen, $ybeg, $ylen)
82: {
83: return $this->_block_header;
84: }
85:
86: protected function _startBlock($header)
87: {
88: return $header;
89: }
90:
91: protected function _lines($lines, $prefix = ' ', $encode = true)
92: {
93: if ($encode) {
94: array_walk($lines, array(&$this, '_encode'));
95: }
96:
97: if ($this->_split_level == 'lines') {
98: return implode("\n", $lines) . "\n";
99: } else {
100: return implode('', $lines);
101: }
102: }
103:
104: protected function _added($lines)
105: {
106: array_walk($lines, array(&$this, '_encode'));
107: $lines[0] = $this->_ins_prefix . $lines[0];
108: $lines[count($lines) - 1] .= $this->_ins_suffix;
109: return $this->_lines($lines, ' ', false);
110: }
111:
112: protected function _deleted($lines, $words = false)
113: {
114: array_walk($lines, array(&$this, '_encode'));
115: $lines[0] = $this->_del_prefix . $lines[0];
116: $lines[count($lines) - 1] .= $this->_del_suffix;
117: return $this->_lines($lines, ' ', false);
118: }
119:
120: protected function _changed($orig, $final)
121: {
122:
123: if ($this->_split_level == 'characters') {
124: return $this->_deleted($orig)
125: . $this->_added($final);
126: }
127:
128:
129: if ($this->_split_level == 'words') {
130: $prefix = '';
131: while ($orig[0] !== false && $final[0] !== false &&
132: substr($orig[0], 0, 1) == ' ' &&
133: substr($final[0], 0, 1) == ' ') {
134: $prefix .= substr($orig[0], 0, 1);
135: $orig[0] = substr($orig[0], 1);
136: $final[0] = substr($final[0], 1);
137: }
138: return $prefix . $this->_deleted($orig) . $this->_added($final);
139: }
140:
141: $text1 = implode("\n", $orig);
142: $text2 = implode("\n", $final);
143:
144:
145: $nl = "\0";
146:
147: if ($this->_split_characters) {
148: $diff = new Horde_Text_Diff('native',
149: array(preg_split('//', $text1),
150: preg_split('//', $text2)));
151: } else {
152: 153: 154:
155: $diff = new Horde_Text_Diff('native',
156: array($this->_splitOnWords($text1, $nl),
157: $this->_splitOnWords($text2, $nl)));
158: }
159:
160:
161: $renderer = new Horde_Text_Diff_Renderer_inline
162: (array_merge($this->getParams(),
163: array('split_level' => $this->_split_characters ? 'characters' : 'words')));
164:
165:
166: return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
167: }
168:
169: protected function _splitOnWords($string, $newlineEscape = "\n")
170: {
171:
172: $string = str_replace("\0", '', $string);
173:
174: $words = array();
175: $length = strlen($string);
176: $pos = 0;
177:
178: while ($pos < $length) {
179:
180: $spaces = strspn(substr($string, $pos), " \n");
181: $nextpos = strcspn(substr($string, $pos + $spaces), " \n");
182: $words[] = str_replace("\n", $newlineEscape, substr($string, $pos, $spaces + $nextpos));
183: $pos += $spaces + $nextpos;
184: }
185:
186: return $words;
187: }
188:
189: protected function _encode(&$string)
190: {
191: $string = htmlspecialchars($string);
192: }
193: }
194: