2 /***************************************************************
5 * (c) 1999-2010 Kasper Skårhøj (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 Skårhøj <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")
60 * Syntax Highlighting class.
62 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
66 class t3lib_syntaxhl
{
69 var $htmlParse; // Parse object.
72 var $DS_wrapTags = array(
73 'T3DataStructure' => array('<span style="font-weight: bold;">', '</span>'),
74 'type' => array('<span style="font-weight: bold; color: #000080;">', '</span>'),
75 'section' => array('<span style="font-weight: bold; color: #000080;">', '</span>'),
76 'el' => array('<span style="font-weight: bold; color: #800000;">', '</span>'),
77 'meta' => array('<span style="font-weight: bold; color: #800080;">', '</span>'),
78 '_unknown' => array('<span style="font-style: italic; color: #666666;">', '</span>'),
80 '_applicationTag' => array('<span style="font-weight: bold; color: #FF6600;">', '</span>'),
81 '_applicationContents' => array('<span style="font-style: italic; color: #C29336;">', '</span>'),
83 'sheets' => array('<span style="font-weight: bold; color: #008000;">', '</span>'),
84 'parent:sheets' => array('<span style="color: #008000;">', '</span>'),
86 'ROOT' => array('<span style="font-weight: bold; color: #008080;">', '</span>'),
87 'parent:el' => array('<span style="font-weight: bold; color: #008080;">', '</span>'),
89 'langDisable' => array('<span style="color: #000080;">', '</span>'),
90 'langChildren' => array('<span style="color: #000080;">', '</span>'),
93 var $FF_wrapTags = array(
94 'T3FlexForms' => array('<span style="font-weight: bold;">', '</span>'),
95 'meta' => array('<span style="font-weight: bold; color: #800080;">', '</span>'),
96 'data' => array('<span style="font-weight: bold; color: #800080;">', '</span>'),
97 'el' => array('<span style="font-weight: bold; color: #80a000;">', '</span>'),
98 'itemType' => array('<span style="font-weight: bold; color: #804000;">', '</span>'),
99 'section' => array('<span style="font-weight: bold; color: #604080;">', '</span>'),
100 'numIndex' => array('<span style="color: #333333;">', '</span>'),
101 '_unknown' => array('<span style="font-style: italic; color: #666666;">', '</span>'),
104 'sDEF' => array('<span style="font-weight: bold; color: #008000;">', '</span>'),
105 'level:sheet' => array('<span style="font-weight: bold; color: #008000;">', '</span>'),
107 'lDEF' => array('<span style="font-weight: bold; color: #000080;">', '</span>'),
108 'level:language' => array('<span style="font-weight: bold; color: #000080;">', '</span>'),
110 'level:fieldname' => array('<span style="font-weight: bold; color: #666666;">', '</span>'),
112 'vDEF' => array('<span style="font-weight: bold; color: #008080;">', '</span>'),
113 'level:value' => array('<span style="font-weight: bold; color: #008080;">', '</span>'),
115 'currentSheetId' => array('<span style="color: #000080;">', '</span>'),
116 'currentLangId' => array('<span style="color: #000080;">', '</span>'),
120 /*************************************
122 * Markup of Data Structure, <T3DataStructure>
124 *************************************/
127 * Makes syntax highlighting of a Data Structure, <T3DataStructure>
129 * @param string Data Structure XML, must be valid since it's parsed.
130 * @return string HTML code with highlighted content. Must be wrapped in <PRE> tags
132 function highLight_DS($str) {
134 // Parse DS to verify that it is valid:
135 $DS = t3lib_div
::xml2array($str);
137 $completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
139 // Highlighting source:
140 $this->htmlParse
= t3lib_div
::makeInstance('t3lib_parsehtml'); // Init parser object
141 $struct = $this->splitXMLbyTags(implode(',', $completeTagList), $str); // Split the XML by the found tags, recursively into LARGE array.
142 $markUp = $this->highLight_DS_markUpRecursively($struct); // Perform color-markup on the parsed content. Markup preserves the LINE formatting of the XML.
147 $error = 'ERROR: The input content failed XML parsing: ' . $DS;
153 * Making syntax highlighting of the parsed Data Structure XML.
154 * Called recursively.
156 * @param array The structure, see splitXMLbyTags()
157 * @param string Parent tag.
158 * @param string "Application" - used to denote if we are 'inside' a section
159 * @return string HTML
161 function highLight_DS_markUpRecursively($struct, $parent = '', $app = '') {
163 foreach ($struct as $k => $v) {
166 $wrap = array('', '');
170 case 'tx_templavoila':
171 $wrap = $this->DS_wrapTags
['_applicationContents'];
175 if ($parent == 'el') {
176 $wrap = $this->DS_wrapTags
['parent:el'];
178 } elseif ($parent == 'sheets') {
179 $wrap = $this->DS_wrapTags
['parent:sheets'];
181 $wrap = $this->DS_wrapTags
[$v['tagName']];
185 // If no wrap defined, us "unknown" definition
186 if (!is_array($wrap)) {
187 $wrap = $this->DS_wrapTags
['_unknown'];
190 // Check for application sections in the XML:
191 if ($app == 'el' ||
$parent == 'ROOT') {
192 switch ($v['tagName']) {
194 case 'tx_templavoila':
195 $nextApp = $v['tagName'];
196 $wrap = $this->DS_wrapTags
['_applicationTag'];
203 $output .= $wrap[0] . htmlspecialchars($v['tag']) . $wrap[1];
204 $output .= $this->highLight_DS_markUpRecursively($v['sub'], $v['tagName'], $nextApp);
205 $output .= $wrap[0] . htmlspecialchars('</' . $v['tagName'] . '>') . $wrap[1];
207 $output .= htmlspecialchars($v);
215 /*************************************
217 * Markup of Data Structure, <T3FlexForms>
219 *************************************/
222 * Makes syntax highlighting of a FlexForm Data, <T3FlexForms>
224 * @param string Data Structure XML, must be valid since it's parsed.
225 * @return string HTML code with highlighted content. Must be wrapped in <PRE> tags
227 function highLight_FF($str) {
229 // Parse DS to verify that it is valid:
230 $DS = t3lib_div
::xml2array($str);
232 $completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
234 // Highlighting source:
235 $this->htmlParse
= t3lib_div
::makeInstance('t3lib_parsehtml'); // Init parser object
236 $struct = $this->splitXMLbyTags(implode(',', $completeTagList), $str); // Split the XML by the found tags, recursively into LARGE array.
237 $markUp = $this->highLight_FF_markUpRecursively($struct); // Perform color-markup on the parsed content. Markup preserves the LINE formatting of the XML.
242 $error = 'ERROR: The input content failed XML parsing: ' . $DS;
248 * Making syntax highlighting of the parsed FlexForm XML.
249 * Called recursively.
251 * @param array The structure, see splitXMLbyTags()
252 * @param string Parent tag.
253 * @param string "Application" - used to denote if we are 'inside' a section
254 * @return string HTML
256 function highLight_FF_markUpRecursively($struct, $parent = '', $app = '') {
260 if ($parent == 'data') {
262 } elseif ($app == 'sheet') {
264 } elseif ($app == 'language') {
266 } elseif ($app == 'fieldname') {
268 } elseif ($app == 'el' ||
$app == 'numIndex') {
272 // Traverse structure:
273 foreach ($struct as $k => $v) {
275 $wrap = array('', '');
277 if ($v['tagName'] == 'numIndex') {
282 $wrap = $this->FF_wrapTags
[$v['tagName']];
284 // If no wrap defined, us "unknown" definition
285 if (!is_array($wrap)) {
291 $wrap = $this->FF_wrapTags
['level:' . $app];
294 $wrap = $this->FF_wrapTags
['_unknown'];
299 if ($v['tagName'] == 'el') {
303 $output .= $wrap[0] . htmlspecialchars($v['tag']) . $wrap[1];
304 $output .= $this->highLight_FF_markUpRecursively($v['sub'], $v['tagName'], $app);
305 $output .= $wrap[0] . htmlspecialchars('</' . $v['tagName'] . '>') . $wrap[1];
307 $output .= htmlspecialchars($v);
315 /*************************************
319 *************************************/
322 * Returning all tag names found in XML/HTML input string
324 * @param string HTML/XML input
325 * @return array Array with all found tags (starttags only)
327 function getAllTags($str) {
331 $token = md5(microtime());
333 // Markup all tag names with token.
334 $markUpStr = preg_replace('/<([[:alnum:]_]+)[^>]*>/', $token . '${1}' . $token, $str);
336 // Splitting by token:
337 $parts = explode($token, $markUpStr);
340 foreach ($parts as $k => $v) {
351 * Splitting the input source by the tags listing in $tagList.
352 * Called recursively.
354 * @param string Commalist of tags to split source by (into blocks, ALL being block-tags!)
355 * @param string Input string.
356 * @return array Array with the content arranged hierarchically.
358 function splitXMLbyTags($tagList, $str) {
359 $struct = $this->htmlParse
->splitIntoBlock($tagList, $str);
362 foreach ($struct as $k => $v) {
364 $tag = $this->htmlParse
->getFirstTag($v);
365 $tagName = $this->htmlParse
->getFirstTagName($tag, TRUE);
368 'tagName' => $tagName,
369 'sub' => $this->splitXMLbyTags($tagList, $this->htmlParse
->removeFirstAndLastTag($struct[$k]))
379 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_syntaxhl.php']) {
380 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_syntaxhl.php']);