2 /***************************************************************
5 * (c) 1999-2008 Kasper Skaarhoj (kasperYYYY@typo3.com)
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
28 * Contains a class for various syntax highlighting.
32 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
35 * [CLASS/FUNCTION INDEX of SCRIPT]
39 * 84: class t3lib_syntaxhl
41 * SECTION: Markup of Data Structure, <T3DataStructure>
42 * 156: function highLight_DS($str)
43 * 183: function highLight_DS_markUpRecursively($struct,$parent='',$app='')
45 * SECTION: Markup of Data Structure, <T3FlexForms>
46 * 268: function highLight_FF($str)
47 * 295: function highLight_FF_markUpRecursively($struct,$parent='',$app='')
50 * 376: function getAllTags($str)
51 * 407: function splitXMLbyTags($tagList,$str)
54 * (This index is automatically created/updated by the extension "extdeveval")
74 require_once(PATH_t3lib
.'class.t3lib_parsehtml.php');
78 * Syntax Highlighting class.
80 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
84 class t3lib_syntaxhl
{
87 var $htmlParse; // Parse object.
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>'),
98 '_applicationTag' => array('<span style="font-weight: bold; color: #FF6600;">','</span>'),
99 '_applicationContents' => array('<span style="font-style: italic; color: #C29336;">','</span>'),
101 'sheets' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
102 'parent:sheets' => array('<span style="color: #008000;">','</span>'),
104 'ROOT' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
105 'parent:el' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
107 'langDisable' => array('<span style="color: #000080;">','</span>'),
108 'langChildren' => array('<span style="color: #000080;">','</span>'),
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>'),
122 'sDEF' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
123 'level:sheet' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
125 'lDEF' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
126 'level:language' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
128 'level:fieldname' => array('<span style="font-weight: bold; color: #666666;">','</span>'),
130 'vDEF' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
131 'level:value' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
133 'currentSheetId' => array('<span style="color: #000080;">','</span>'),
134 'currentLangId' => array('<span style="color: #000080;">','</span>'),
144 /*************************************
146 * Markup of Data Structure, <T3DataStructure>
148 *************************************/
151 * Makes syntax highlighting of a Data Structure, <T3DataStructure>
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
156 function highLight_DS($str) {
158 // Parse DS to verify that it is valid:
159 $DS = t3lib_div
::xml2array($str);
161 $completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
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.
170 } else $error = 'ERROR: The input content failed XML parsing: '.$DS;
175 * Making syntax highlighting of the parsed Data Structure XML.
176 * Called recursively.
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
183 function highLight_DS_markUpRecursively($struct,$parent='',$app='') {
185 foreach($struct as $k => $v) {
188 $wrap = array('','');
192 case 'tx_templavoila':
193 $wrap = $this->DS_wrapTags
['_applicationContents'];
198 $wrap = $this->DS_wrapTags
['parent:el'];
200 } elseif ($parent=='sheets') {
201 $wrap = $this->DS_wrapTags
['parent:sheets'];
203 $wrap = $this->DS_wrapTags
[$v['tagName']];
207 // If no wrap defined, us "unknown" definition
208 if (!is_array($wrap)) {
209 $wrap = $this->DS_wrapTags
['_unknown'];
212 // Check for application sections in the XML:
213 if ($app=='el' ||
$parent=='ROOT') {
214 switch($v['tagName']) {
216 case 'tx_templavoila':
217 $nextApp = $v['tagName'];
218 $wrap = $this->DS_wrapTags
['_applicationTag'];
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];
229 $output.=htmlspecialchars($v);
256 /*************************************
258 * Markup of Data Structure, <T3FlexForms>
260 *************************************/
263 * Makes syntax highlighting of a FlexForm Data, <T3FlexForms>
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
268 function highLight_FF($str) {
270 // Parse DS to verify that it is valid:
271 $DS = t3lib_div
::xml2array($str);
273 $completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
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.
282 } else $error = 'ERROR: The input content failed XML parsing: '.$DS;
287 * Making syntax highlighting of the parsed FlexForm XML.
288 * Called recursively.
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
295 function highLight_FF_markUpRecursively($struct,$parent='',$app='') {
299 if ($parent=='data') {
301 } elseif($app=='sheet') {
303 } elseif($app=='language') {
305 } elseif($app=='fieldname') {
307 } elseif($app=='el' ||
$app=='numIndex') {
311 // Traverse structure:
312 foreach($struct as $k => $v) {
314 $wrap = array('','');
316 if ($v['tagName'] == 'numIndex') {
321 $wrap = $this->FF_wrapTags
[$v['tagName']];
323 // If no wrap defined, us "unknown" definition
324 if (!is_array($wrap)) {
330 $wrap = $this->FF_wrapTags
['level:'.$app];
333 $wrap = $this->FF_wrapTags
['_unknown'];
338 if ($v['tagName']=='el') {
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];
346 $output.=htmlspecialchars($v);
364 /*************************************
368 *************************************/
371 * Returning all tag names found in XML/HTML input string
373 * @param string HTML/XML input
374 * @return array Array with all found tags (starttags only)
376 function getAllTags($str) {
380 $token = md5(microtime());
382 // Markup all tag names with token.
383 $markUpStr = ereg_replace('<([[:alnum:]_]+)[^>]*>',$token.'\1'.$token,$str);
385 // Splitting by token:
386 $parts = explode($token,$markUpStr);
389 foreach($parts as $k => $v) {
400 * Splitting the input source by the tags listing in $tagList.
401 * Called recursively.
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.
407 function splitXMLbyTags($tagList,$str) {
408 $struct = $this->htmlParse
->splitIntoBlock($tagList,$str);
411 foreach($struct as $k => $v) {
413 $tag = $this->htmlParse
->getFirstTag($v);
414 $tagName = $this->htmlParse
->getFirstTagName($tag,TRUE);
417 'tagName' => $tagName,
418 'sub' => $this->splitXMLbyTags($tagList,$this->htmlParse
->removeFirstAndLastTag($struct[$k]))
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']);