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