Fixed bug #14050: CleanUp - CGL format of t3lib files - t3lib_scbase
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_syntaxhl.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2010 Kasper Skårhøj (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 Skårhøj <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
75
76 /**
77 * Syntax Highlighting class.
78 *
79 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
80 * @package TYPO3
81 * @subpackage t3lib
82 */
83 class t3lib_syntaxhl {
84
85 // Internal, dynamic:
86 var $htmlParse; // Parse object.
87
88 // External, static:
89 var $DS_wrapTags = array(
90 'T3DataStructure' => array('<span style="font-weight: bold;">','</span>'),
91 'type' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
92 'section' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
93 'el' => array('<span style="font-weight: bold; color: #800000;">','</span>'),
94 'meta' => array('<span style="font-weight: bold; color: #800080;">','</span>'),
95 '_unknown' => array('<span style="font-style: italic; color: #666666;">','</span>'),
96
97 '_applicationTag' => array('<span style="font-weight: bold; color: #FF6600;">','</span>'),
98 '_applicationContents' => array('<span style="font-style: italic; color: #C29336;">','</span>'),
99
100 'sheets' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
101 'parent:sheets' => array('<span style="color: #008000;">','</span>'),
102
103 'ROOT' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
104 'parent:el' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
105
106 'langDisable' => array('<span style="color: #000080;">','</span>'),
107 'langChildren' => array('<span style="color: #000080;">','</span>'),
108 );
109
110 var $FF_wrapTags = array(
111 'T3FlexForms' => array('<span style="font-weight: bold;">','</span>'),
112 'meta' => array('<span style="font-weight: bold; color: #800080;">','</span>'),
113 'data' => array('<span style="font-weight: bold; color: #800080;">','</span>'),
114 'el' => array('<span style="font-weight: bold; color: #80a000;">','</span>'),
115 'itemType' => array('<span style="font-weight: bold; color: #804000;">','</span>'),
116 'section' => array('<span style="font-weight: bold; color: #604080;">','</span>'),
117 'numIndex' => array('<span style="color: #333333;">','</span>'),
118 '_unknown' => array('<span style="font-style: italic; color: #666666;">','</span>'),
119
120
121 'sDEF' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
122 'level:sheet' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
123
124 'lDEF' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
125 'level:language' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
126
127 'level:fieldname' => array('<span style="font-weight: bold; color: #666666;">','</span>'),
128
129 'vDEF' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
130 'level:value' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
131
132 'currentSheetId' => array('<span style="color: #000080;">','</span>'),
133 'currentLangId' => array('<span style="color: #000080;">','</span>'),
134 );
135
136
137
138
139
140
141
142
143 /*************************************
144 *
145 * Markup of Data Structure, <T3DataStructure>
146 *
147 *************************************/
148
149 /**
150 * Makes syntax highlighting of a Data Structure, <T3DataStructure>
151 *
152 * @param string Data Structure XML, must be valid since it's parsed.
153 * @return string HTML code with highlighted content. Must be wrapped in <PRE> tags
154 */
155 function highLight_DS($str) {
156
157 // Parse DS to verify that it is valid:
158 $DS = t3lib_div::xml2array($str);
159 if (is_array($DS)) {
160 $completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
161
162 // Highlighting source:
163 $this->htmlParse = t3lib_div::makeInstance('t3lib_parsehtml'); // Init parser object
164 $struct = $this->splitXMLbyTags(implode(',',$completeTagList),$str); // Split the XML by the found tags, recursively into LARGE array.
165 $markUp = $this->highLight_DS_markUpRecursively($struct); // Perform color-markup on the parsed content. Markup preserves the LINE formatting of the XML.
166
167 // Return content:
168 return $markUp;
169 } else $error = 'ERROR: The input content failed XML parsing: '.$DS;
170 return $error;
171 }
172
173 /**
174 * Making syntax highlighting of the parsed Data Structure XML.
175 * Called recursively.
176 *
177 * @param array The structure, see splitXMLbyTags()
178 * @param string Parent tag.
179 * @param string "Application" - used to denote if we are 'inside' a section
180 * @return string HTML
181 */
182 function highLight_DS_markUpRecursively($struct,$parent='',$app='') {
183 $output='';
184 foreach($struct as $k => $v) {
185 if ($k%2) {
186 $nextApp = $app;
187 $wrap = array('','');
188
189 switch($app) {
190 case 'TCEforms':
191 case 'tx_templavoila':
192 $wrap = $this->DS_wrapTags['_applicationContents'];
193 break;
194 case 'el':
195 default:
196 if ($parent=='el') {
197 $wrap = $this->DS_wrapTags['parent:el'];
198 $nextApp = 'el';
199 } elseif ($parent=='sheets') {
200 $wrap = $this->DS_wrapTags['parent:sheets'];
201 } else {
202 $wrap = $this->DS_wrapTags[$v['tagName']];
203 $nextApp = '';
204 }
205
206 // If no wrap defined, us "unknown" definition
207 if (!is_array($wrap)) {
208 $wrap = $this->DS_wrapTags['_unknown'];
209 }
210
211 // Check for application sections in the XML:
212 if ($app=='el' || $parent=='ROOT') {
213 switch($v['tagName']) {
214 case 'TCEforms':
215 case 'tx_templavoila':
216 $nextApp = $v['tagName'];
217 $wrap = $this->DS_wrapTags['_applicationTag'];
218 break;
219 }
220 }
221 break;
222 }
223
224 $output.=$wrap[0].htmlspecialchars($v['tag']).$wrap[1];
225 $output.=$this->highLight_DS_markUpRecursively($v['sub'],$v['tagName'],$nextApp);
226 $output.=$wrap[0].htmlspecialchars('</'.$v['tagName'].'>').$wrap[1];
227 } else {
228 $output.=htmlspecialchars($v);
229 }
230 }
231
232 return $output;
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 /*************************************
256 *
257 * Markup of Data Structure, <T3FlexForms>
258 *
259 *************************************/
260
261 /**
262 * Makes syntax highlighting of a FlexForm Data, <T3FlexForms>
263 *
264 * @param string Data Structure XML, must be valid since it's parsed.
265 * @return string HTML code with highlighted content. Must be wrapped in <PRE> tags
266 */
267 function highLight_FF($str) {
268
269 // Parse DS to verify that it is valid:
270 $DS = t3lib_div::xml2array($str);
271 if (is_array($DS)) {
272 $completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
273
274 // Highlighting source:
275 $this->htmlParse = t3lib_div::makeInstance('t3lib_parsehtml'); // Init parser object
276 $struct = $this->splitXMLbyTags(implode(',',$completeTagList),$str); // Split the XML by the found tags, recursively into LARGE array.
277 $markUp = $this->highLight_FF_markUpRecursively($struct); // Perform color-markup on the parsed content. Markup preserves the LINE formatting of the XML.
278
279 // Return content:
280 return $markUp;
281 } else $error = 'ERROR: The input content failed XML parsing: '.$DS;
282 return $error;
283 }
284
285 /**
286 * Making syntax highlighting of the parsed FlexForm XML.
287 * Called recursively.
288 *
289 * @param array The structure, see splitXMLbyTags()
290 * @param string Parent tag.
291 * @param string "Application" - used to denote if we are 'inside' a section
292 * @return string HTML
293 */
294 function highLight_FF_markUpRecursively($struct,$parent='',$app='') {
295 $output='';
296
297 // Setting levels:
298 if ($parent=='data') {
299 $app='sheet';
300 } elseif($app=='sheet') {
301 $app='language';
302 } elseif($app=='language') {
303 $app='fieldname';
304 } elseif($app=='fieldname') {
305 $app='value';
306 } elseif($app=='el' || $app=='numIndex') {
307 $app='fieldname';
308 }
309
310 // Traverse structure:
311 foreach($struct as $k => $v) {
312 if ($k%2) {
313 $wrap = array('','');
314
315 if ($v['tagName'] == 'numIndex') {
316 $app = 'numIndex';
317 }
318
319 // Default wrap:
320 $wrap = $this->FF_wrapTags[$v['tagName']];
321
322 // If no wrap defined, us "unknown" definition
323 if (!is_array($wrap)) {
324 switch($app) {
325 case 'sheet':
326 case 'language':
327 case 'fieldname':
328 case 'value':
329 $wrap = $this->FF_wrapTags['level:'.$app];
330 break;
331 default:
332 $wrap = $this->FF_wrapTags['_unknown'];
333 break;
334 }
335 }
336
337 if ($v['tagName']=='el') {
338 $app='el';
339 }
340
341 $output.=$wrap[0].htmlspecialchars($v['tag']).$wrap[1];
342 $output.=$this->highLight_FF_markUpRecursively($v['sub'],$v['tagName'],$app);
343 $output.=$wrap[0].htmlspecialchars('</'.$v['tagName'].'>').$wrap[1];
344 } else {
345 $output.=htmlspecialchars($v);
346 }
347 }
348
349 return $output;
350 }
351
352
353
354
355
356
357
358
359
360
361
362
363 /*************************************
364 *
365 * Various
366 *
367 *************************************/
368
369 /**
370 * Returning all tag names found in XML/HTML input string
371 *
372 * @param string HTML/XML input
373 * @return array Array with all found tags (starttags only)
374 */
375 function getAllTags($str) {
376
377 // Init:
378 $tags = array();
379 $token = md5(microtime());
380
381 // Markup all tag names with token.
382 $markUpStr = preg_replace('/<([[:alnum:]_]+)[^>]*>/', $token . '${1}' . $token, $str);
383
384 // Splitting by token:
385 $parts = explode($token,$markUpStr);
386
387 // Traversing parts:
388 foreach($parts as $k => $v) {
389 if ($k%2) {
390 $tags[]=$v;
391 }
392 }
393
394 // Returning tags:
395 return $tags;
396 }
397
398 /**
399 * Splitting the input source by the tags listing in $tagList.
400 * Called recursively.
401 *
402 * @param string Commalist of tags to split source by (into blocks, ALL being block-tags!)
403 * @param string Input string.
404 * @return array Array with the content arranged hierarchically.
405 */
406 function splitXMLbyTags($tagList,$str) {
407 $struct = $this->htmlParse->splitIntoBlock($tagList,$str);
408
409 // Traverse level:
410 foreach($struct as $k => $v) {
411 if ($k%2) {
412 $tag = $this->htmlParse->getFirstTag($v);
413 $tagName = $this->htmlParse->getFirstTagName($tag,TRUE);
414 $struct[$k] = array(
415 'tag' => $tag,
416 'tagName' => $tagName,
417 'sub' => $this->splitXMLbyTags($tagList,$this->htmlParse->removeFirstAndLastTag($struct[$k]))
418 );
419 }
420 }
421
422 return $struct;
423 }
424 }
425
426
427 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_syntaxhl.php']) {
428 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_syntaxhl.php']);
429 }
430
431 ?>