* Merging all changes from TYPO3_4-0 branch back into HEAD
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_syntaxhl.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2006 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains a class for various syntax highlighting.
29 *
30 * $Id$
31 *
32 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
33 */
34 /**
35 * [CLASS/FUNCTION INDEX of SCRIPT]
36 *
37 *
38 *
39 * 84: class t3lib_syntaxhl
40 *
41 * SECTION: Markup of Data Structure, <T3DataStructure>
42 * 156: function highLight_DS($str)
43 * 183: function highLight_DS_markUpRecursively($struct,$parent='',$app='')
44 *
45 * SECTION: Markup of Data Structure, <T3FlexForms>
46 * 268: function highLight_FF($str)
47 * 295: function highLight_FF_markUpRecursively($struct,$parent='',$app='')
48 *
49 * SECTION: Various
50 * 376: function getAllTags($str)
51 * 407: function splitXMLbyTags($tagList,$str)
52 *
53 * TOTAL FUNCTIONS: 6
54 * (This index is automatically created/updated by the extension "extdeveval")
55 *
56 */
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 require_once(PATH_t3lib.'class.t3lib_parsehtml.php');
75
76
77 /**
78 * Syntax Highlighting class.
79 *
80 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
81 * @package TYPO3
82 * @subpackage t3lib
83 */
84 class t3lib_syntaxhl {
85
86 // Internal, dynamic:
87 var $htmlParse; // Parse object.
88
89 // External, static:
90 var $DS_wrapTags = array(
91 'T3DataStructure' => array('<span style="font-weight: bold;">','</span>'),
92 'type' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
93 'section' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
94 'el' => array('<span style="font-weight: bold; color: #800000;">','</span>'),
95 'meta' => array('<span style="font-weight: bold; color: #800080;">','</span>'),
96 '_unknown' => array('<span style="font-style: italic; color: #666666;">','</span>'),
97
98 '_applicationTag' => array('<span style="font-weight: bold; color: #FF6600;">','</span>'),
99 '_applicationContents' => array('<span style="font-style: italic; color: #C29336;">','</span>'),
100
101 'sheets' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
102 'parent:sheets' => array('<span style="color: #008000;">','</span>'),
103
104 'ROOT' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
105 'parent:el' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
106
107 'langDisable' => array('<span style="color: #000080;">','</span>'),
108 'langChildren' => array('<span style="color: #000080;">','</span>'),
109 );
110
111 var $FF_wrapTags = array(
112 'T3FlexForms' => array('<span style="font-weight: bold;">','</span>'),
113 'meta' => array('<span style="font-weight: bold; color: #800080;">','</span>'),
114 'data' => array('<span style="font-weight: bold; color: #800080;">','</span>'),
115 'el' => array('<span style="font-weight: bold; color: #80a000;">','</span>'),
116 'itemType' => array('<span style="font-weight: bold; color: #804000;">','</span>'),
117 'section' => array('<span style="font-weight: bold; color: #604080;">','</span>'),
118 'numIndex' => array('<span style="color: #333333;">','</span>'),
119 '_unknown' => array('<span style="font-style: italic; color: #666666;">','</span>'),
120
121
122 'sDEF' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
123 'level:sheet' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
124
125 'lDEF' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
126 'level:language' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
127
128 'level:fieldname' => array('<span style="font-weight: bold; color: #666666;">','</span>'),
129
130 'vDEF' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
131 'level:value' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
132
133 'currentSheetId' => array('<span style="color: #000080;">','</span>'),
134 'currentLangId' => array('<span style="color: #000080;">','</span>'),
135 );
136
137
138
139
140
141
142
143
144 /*************************************
145 *
146 * Markup of Data Structure, <T3DataStructure>
147 *
148 *************************************/
149
150 /**
151 * Makes syntax highlighting of a Data Structure, <T3DataStructure>
152 *
153 * @param string Data Structure XML, must be valid since it's parsed.
154 * @return string HTML code with highlighted content. Must be wrapped in <PRE> tags
155 */
156 function highLight_DS($str) {
157
158 // Parse DS to verify that it is valid:
159 $DS = t3lib_div::xml2array($str);
160 if (is_array($DS)) {
161 $completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
162
163 // Highlighting source:
164 $this->htmlParse = t3lib_div::makeInstance('t3lib_parsehtml'); // Init parser object
165 $struct = $this->splitXMLbyTags(implode(',',$completeTagList),$str); // Split the XML by the found tags, recursively into LARGE array.
166 $markUp = $this->highLight_DS_markUpRecursively($struct); // Perform color-markup on the parsed content. Markup preserves the LINE formatting of the XML.
167
168 // Return content:
169 return $markUp;
170 } else $error = 'ERROR: The input content failed XML parsing: '.$DS;
171 return $error;
172 }
173
174 /**
175 * Making syntax highlighting of the parsed Data Structure XML.
176 * Called recursively.
177 *
178 * @param array The structure, see splitXMLbyTags()
179 * @param string Parent tag.
180 * @param string "Application" - used to denote if we are 'inside' a section
181 * @return string HTML
182 */
183 function highLight_DS_markUpRecursively($struct,$parent='',$app='') {
184 $output='';
185 foreach($struct as $k => $v) {
186 if ($k%2) {
187 $nextApp = $app;
188 $wrap = array('','');
189
190 switch($app) {
191 case 'TCEforms':
192 case 'tx_templavoila':
193 $wrap = $this->DS_wrapTags['_applicationContents'];
194 break;
195 case 'el':
196 default:
197 if ($parent=='el') {
198 $wrap = $this->DS_wrapTags['parent:el'];
199 $nextApp = 'el';
200 } elseif ($parent=='sheets') {
201 $wrap = $this->DS_wrapTags['parent:sheets'];
202 } else {
203 $wrap = $this->DS_wrapTags[$v['tagName']];
204 $nextApp = '';
205 }
206
207 // If no wrap defined, us "unknown" definition
208 if (!is_array($wrap)) {
209 $wrap = $this->DS_wrapTags['_unknown'];
210 }
211
212 // Check for application sections in the XML:
213 if ($app=='el' || $parent=='ROOT') {
214 switch($v['tagName']) {
215 case 'TCEforms':
216 case 'tx_templavoila':
217 $nextApp = $v['tagName'];
218 $wrap = $this->DS_wrapTags['_applicationTag'];
219 break;
220 }
221 }
222 break;
223 }
224
225 $output.=$wrap[0].htmlspecialchars($v['tag']).$wrap[1];
226 $output.=$this->highLight_DS_markUpRecursively($v['sub'],$v['tagName'],$nextApp);
227 $output.=$wrap[0].htmlspecialchars('</'.$v['tagName'].'>').$wrap[1];
228 } else {
229 $output.=htmlspecialchars($v);
230 }
231 }
232
233 return $output;
234 }
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256 /*************************************
257 *
258 * Markup of Data Structure, <T3FlexForms>
259 *
260 *************************************/
261
262 /**
263 * Makes syntax highlighting of a FlexForm Data, <T3FlexForms>
264 *
265 * @param string Data Structure XML, must be valid since it's parsed.
266 * @return string HTML code with highlighted content. Must be wrapped in <PRE> tags
267 */
268 function highLight_FF($str) {
269
270 // Parse DS to verify that it is valid:
271 $DS = t3lib_div::xml2array($str);
272 if (is_array($DS)) {
273 $completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
274
275 // Highlighting source:
276 $this->htmlParse = t3lib_div::makeInstance('t3lib_parsehtml'); // Init parser object
277 $struct = $this->splitXMLbyTags(implode(',',$completeTagList),$str); // Split the XML by the found tags, recursively into LARGE array.
278 $markUp = $this->highLight_FF_markUpRecursively($struct); // Perform color-markup on the parsed content. Markup preserves the LINE formatting of the XML.
279
280 // Return content:
281 return $markUp;
282 } else $error = 'ERROR: The input content failed XML parsing: '.$DS;
283 return $error;
284 }
285
286 /**
287 * Making syntax highlighting of the parsed FlexForm XML.
288 * Called recursively.
289 *
290 * @param array The structure, see splitXMLbyTags()
291 * @param string Parent tag.
292 * @param string "Application" - used to denote if we are 'inside' a section
293 * @return string HTML
294 */
295 function highLight_FF_markUpRecursively($struct,$parent='',$app='') {
296 $output='';
297
298 // Setting levels:
299 if ($parent=='data') {
300 $app='sheet';
301 } elseif($app=='sheet') {
302 $app='language';
303 } elseif($app=='language') {
304 $app='fieldname';
305 } elseif($app=='fieldname') {
306 $app='value';
307 } elseif($app=='el' || $app=='numIndex') {
308 $app='fieldname';
309 }
310
311 // Traverse structure:
312 foreach($struct as $k => $v) {
313 if ($k%2) {
314 $wrap = array('','');
315
316 if ($v['tagName'] == 'numIndex') {
317 $app = 'numIndex';
318 }
319
320 // Default wrap:
321 $wrap = $this->FF_wrapTags[$v['tagName']];
322
323 // If no wrap defined, us "unknown" definition
324 if (!is_array($wrap)) {
325 switch($app) {
326 case 'sheet':
327 case 'language':
328 case 'fieldname':
329 case 'value':
330 $wrap = $this->FF_wrapTags['level:'.$app];
331 break;
332 default:
333 $wrap = $this->FF_wrapTags['_unknown'];
334 break;
335 }
336 }
337
338 if ($v['tagName']=='el') {
339 $app='el';
340 }
341
342 $output.=$wrap[0].htmlspecialchars($v['tag']).$wrap[1];
343 $output.=$this->highLight_FF_markUpRecursively($v['sub'],$v['tagName'],$app);
344 $output.=$wrap[0].htmlspecialchars('</'.$v['tagName'].'>').$wrap[1];
345 } else {
346 $output.=htmlspecialchars($v);
347 }
348 }
349
350 return $output;
351 }
352
353
354
355
356
357
358
359
360
361
362
363
364 /*************************************
365 *
366 * Various
367 *
368 *************************************/
369
370 /**
371 * Returning all tag names found in XML/HTML input string
372 *
373 * @param string HTML/XML input
374 * @return array Array with all found tags (starttags only)
375 */
376 function getAllTags($str) {
377
378 // Init:
379 $tags = array();
380 $token = md5(microtime());
381
382 // Markup all tag names with token.
383 $markUpStr = ereg_replace('<([[:alnum:]_]+)[^>]*>',$token.'\1'.$token,$str);
384
385 // Splitting by token:
386 $parts = explode($token,$markUpStr);
387
388 // Traversing parts:
389 foreach($parts as $k => $v) {
390 if ($k%2) {
391 $tags[]=$v;
392 }
393 }
394
395 // Returning tags:
396 return $tags;
397 }
398
399 /**
400 * Splitting the input source by the tags listing in $tagList.
401 * Called recursively.
402 *
403 * @param string Commalist of tags to split source by (into blocks, ALL being block-tags!)
404 * @param string Input string.
405 * @return array Array with the content arranged hierarchically.
406 */
407 function splitXMLbyTags($tagList,$str) {
408 $struct = $this->htmlParse->splitIntoBlock($tagList,$str);
409
410 // Traverse level:
411 foreach($struct as $k => $v) {
412 if ($k%2) {
413 $tag = $this->htmlParse->getFirstTag($v);
414 $tagName = $this->htmlParse->getFirstTagName($tag,TRUE);
415 $struct[$k] = array(
416 'tag' => $tag,
417 'tagName' => $tagName,
418 'sub' => $this->splitXMLbyTags($tagList,$this->htmlParse->removeFirstAndLastTag($struct[$k]))
419 );
420 }
421 }
422
423 return $struct;
424 }
425 }
426
427
428 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_syntaxhl.php']) {
429 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_syntaxhl.php']);
430 }
431 ?>