2 /***************************************************************
5 * (c) 1999-2009 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")
77 * Syntax Highlighting class.
79 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
83 class t3lib_syntaxhl
{
86 var $htmlParse; // Parse object.
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>'),
97 '_applicationTag' => array('<span style="font-weight: bold; color: #FF6600;">','</span>'),
98 '_applicationContents' => array('<span style="font-style: italic; color: #C29336;">','</span>'),
100 'sheets' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
101 'parent:sheets' => array('<span style="color: #008000;">','</span>'),
103 'ROOT' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
104 'parent:el' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
106 'langDisable' => array('<span style="color: #000080;">','</span>'),
107 'langChildren' => array('<span style="color: #000080;">','</span>'),
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>'),
121 'sDEF' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
122 'level:sheet' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
124 'lDEF' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
125 'level:language' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
127 'level:fieldname' => array('<span style="font-weight: bold; color: #666666;">','</span>'),
129 'vDEF' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
130 'level:value' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
132 'currentSheetId' => array('<span style="color: #000080;">','</span>'),
133 'currentLangId' => array('<span style="color: #000080;">','</span>'),
143 /*************************************
145 * Markup of Data Structure, <T3DataStructure>
147 *************************************/
150 * Makes syntax highlighting of a Data Structure, <T3DataStructure>
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
155 function highLight_DS($str) {
157 // Parse DS to verify that it is valid:
158 $DS = t3lib_div
::xml2array($str);
160 $completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
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.
169 } else $error = 'ERROR: The input content failed XML parsing: '.$DS;
174 * Making syntax highlighting of the parsed Data Structure XML.
175 * Called recursively.
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
182 function highLight_DS_markUpRecursively($struct,$parent='',$app='') {
184 foreach($struct as $k => $v) {
187 $wrap = array('','');
191 case 'tx_templavoila':
192 $wrap = $this->DS_wrapTags
['_applicationContents'];
197 $wrap = $this->DS_wrapTags
['parent:el'];
199 } elseif ($parent=='sheets') {
200 $wrap = $this->DS_wrapTags
['parent:sheets'];
202 $wrap = $this->DS_wrapTags
[$v['tagName']];
206 // If no wrap defined, us "unknown" definition
207 if (!is_array($wrap)) {
208 $wrap = $this->DS_wrapTags
['_unknown'];
211 // Check for application sections in the XML:
212 if ($app=='el' ||
$parent=='ROOT') {
213 switch($v['tagName']) {
215 case 'tx_templavoila':
216 $nextApp = $v['tagName'];
217 $wrap = $this->DS_wrapTags
['_applicationTag'];
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];
228 $output.=htmlspecialchars($v);
255 /*************************************
257 * Markup of Data Structure, <T3FlexForms>
259 *************************************/
262 * Makes syntax highlighting of a FlexForm Data, <T3FlexForms>
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
267 function highLight_FF($str) {
269 // Parse DS to verify that it is valid:
270 $DS = t3lib_div
::xml2array($str);
272 $completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
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.
281 } else $error = 'ERROR: The input content failed XML parsing: '.$DS;
286 * Making syntax highlighting of the parsed FlexForm XML.
287 * Called recursively.
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
294 function highLight_FF_markUpRecursively($struct,$parent='',$app='') {
298 if ($parent=='data') {
300 } elseif($app=='sheet') {
302 } elseif($app=='language') {
304 } elseif($app=='fieldname') {
306 } elseif($app=='el' ||
$app=='numIndex') {
310 // Traverse structure:
311 foreach($struct as $k => $v) {
313 $wrap = array('','');
315 if ($v['tagName'] == 'numIndex') {
320 $wrap = $this->FF_wrapTags
[$v['tagName']];
322 // If no wrap defined, us "unknown" definition
323 if (!is_array($wrap)) {
329 $wrap = $this->FF_wrapTags
['level:'.$app];
332 $wrap = $this->FF_wrapTags
['_unknown'];
337 if ($v['tagName']=='el') {
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];
345 $output.=htmlspecialchars($v);
363 /*************************************
367 *************************************/
370 * Returning all tag names found in XML/HTML input string
372 * @param string HTML/XML input
373 * @return array Array with all found tags (starttags only)
375 function getAllTags($str) {
379 $token = md5(microtime());
381 // Markup all tag names with token.
382 $markUpStr = preg_replace('/<([[:alnum:]_]+)[^>]*>/', $token . '${1}' . $token, $str);
384 // Splitting by token:
385 $parts = explode($token,$markUpStr);
388 foreach($parts as $k => $v) {
399 * Splitting the input source by the tags listing in $tagList.
400 * Called recursively.
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.
406 function splitXMLbyTags($tagList,$str) {
407 $struct = $this->htmlParse
->splitIntoBlock($tagList,$str);
410 foreach($struct as $k => $v) {
412 $tag = $this->htmlParse
->getFirstTag($v);
413 $tagName = $this->htmlParse
->getFirstTagName($tag,TRUE
);
416 'tagName' => $tagName,
417 'sub' => $this->splitXMLbyTags($tagList,$this->htmlParse
->removeFirstAndLastTag($struct[$k]))
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']);