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