Added syntax highlighting class
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_syntaxhl.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 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 * 76: class t3lib_syntaxhl
40 * 109: function highLight_DS($str)
41 * 133: function getAllTags($str)
42 * 164: function splitXMLbyTags($tagList,$str)
43 * 192: function markUpRecursively($struct,$parent='',$app='')
44 *
45 * TOTAL FUNCTIONS: 4
46 * (This index is automatically created/updated by the extension "extdeveval")
47 *
48 */
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 require_once(PATH_t3lib.'class.t3lib_parsehtml.php');
67
68
69 /**
70 * Syntax Highlighting class.
71 *
72 * @author Kasper Skaarhoj <kasper@typo3.com>
73 * @package TYPO3
74 * @subpackage t3lib
75 */
76 class t3lib_syntaxhl {
77
78 // Internal, dynamic:
79 var $htmlParse; // Parse object.
80
81 // External, static:
82 var $wrapTags = array(
83 'T3DataStructure' => array('<span style="font-weight: bold;">','</span>'),
84 'type' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
85 'section' => array('<span style="font-weight: bold; color: #000080;">','</span>'),
86 'el' => array('<span style="font-weight: bold; color: #800000;">','</span>'),
87 'meta' => array('<span style="font-weight: bold; color: #800080;">','</span>'),
88 '_unknown' => array('<span style="font-style: italic; color: #666666;">','</span>'),
89
90 '_applicationTag' => array('<span style="font-weight: bold; color: #FF6600;">','</span>'),
91 '_applicationContents' => array('<span style="font-style: italic; color: #C29336;">','</span>'),
92
93 'sheets' => array('<span style="font-weight: bold; color: #008000;">','</span>'),
94 'parent:sheets' => array('<span style="color: #008000;">','</span>'),
95
96 'ROOT' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
97 'parent:el' => array('<span style="font-weight: bold; color: #008080;">','</span>'),
98
99 'langDisable' => array('<span style="color: #000080;">','</span>'),
100 'langChildren' => array('<span style="color: #000080;">','</span>'),
101 );
102
103 /**
104 * Makes syntax highlighting of a Data Structure, <T3DataStructure>
105 *
106 * @param string Data Structure XML, must be valid since it's parsed.
107 * @return string HTML code with highlighted content. Must be wrapped in <PRE> tags
108 */
109 function highLight_DS($str) {
110
111 // Parse DS to verify that it is valid:
112 $DS = t3lib_div::xml2array($str);
113 if (is_array($DS)) {
114 $completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
115
116 // Highlighting source:
117 $this->htmlParse = t3lib_div::makeInstance('t3lib_parsehtml'); // Init parser object
118 $struct = $this->splitXMLbyTags(implode(',',$completeTagList),$str); // Split the XML by the found tags, recursively into LARGE array.
119 $markUp = $this->markUpRecursively($struct); // Perform color-markup on the parsed content. Markup preserves the LINE formatting of the XML.
120
121 // Return content:
122 return $markUp;
123 } else $error = 'ERROR: The input content failed XML parsing: '.$DS;
124 return $error;
125 }
126
127 /**
128 * Returning all tag names found in XML/HTML input string
129 *
130 * @param string HTML/XML input
131 * @return array Array with all found tags (starttags only)
132 */
133 function getAllTags($str) {
134
135 // Init:
136 $tags = array();
137 $token = md5(microtime());
138
139 // Markup all tag names with token.
140 $markUpStr = ereg_replace('<([[:alnum:]_]+)[^>]*>',$token.'\1'.$token,$str);
141
142 // Splitting by token:
143 $parts = explode($token,$markUpStr);
144
145 // Traversing parts:
146 foreach($parts as $k => $v) {
147 if ($k%2) {
148 $tags[]=$v;
149 }
150 }
151
152 // Returning tags:
153 return $tags;
154 }
155
156 /**
157 * Splitting the input source by the tags listing in $tagList.
158 * Called recursively.
159 *
160 * @param string Commalist of tags to split source by (into blocks, ALL being block-tags!)
161 * @param string Input string.
162 * @return array Array with the content arranged hierarchically.
163 */
164 function splitXMLbyTags($tagList,$str) {
165 $struct = $this->htmlParse->splitIntoBlock($tagList,$str);
166
167 // Traverse level:
168 foreach($struct as $k => $v) {
169 if ($k%2) {
170 $tag = $this->htmlParse->getFirstTag($v);
171 $tagName = $this->htmlParse->getFirstTagName($tag,TRUE);
172 $struct[$k] = array(
173 'tag' => $tag,
174 'tagName' => $tagName,
175 'sub' => $this->splitXMLbyTags($tagList,$this->htmlParse->removeFirstAndLastTag($struct[$k]))
176 );
177 }
178 }
179
180 return $struct;
181 }
182
183 /**
184 * Making syntax highlighting of the parsed Data Structure XML.
185 * Called recursively.
186 *
187 * @param array The structure, see splitXMLbyTags()
188 * @param string Parent tag.
189 * @param string "Application" - used to denote if we are 'inside' a sectionl
190 * @return string HTML
191 */
192 function markUpRecursively($struct,$parent='',$app='') {
193 $output='';
194 foreach($struct as $k => $v) {
195 if ($k%2) {
196 $nextApp = $app;
197 $innerWrap = $wrap = array('','');
198
199 switch($app) {
200 case 'TCEforms':
201 case 'tx_templavoila':
202 $wrap = $this->wrapTags['_applicationContents'];
203 break;
204 case 'el':
205 default:
206 if ($parent=='el') {
207 $wrap = $this->wrapTags['parent:el'];
208 $nextApp = 'el';
209 } elseif ($parent=='sheets') {
210 $wrap = $this->wrapTags['parent:sheets'];
211 } else {
212 $wrap = $this->wrapTags[$v['tagName']];
213 $nextApp = '';
214 }
215
216 if (!is_array($wrap)) {
217 $wrap = $this->wrapTags['_unknown'];
218 }
219
220 if ($app=='el' || $parent=='ROOT') {
221 switch($v['tagName']) {
222 case 'TCEforms':
223 case 'tx_templavoila':
224 $nextApp = $v['tagName'];
225 $wrap = $this->wrapTags['_applicationTag'];
226 #$innerWrap = $this->wrapTags['_applicationContents'];
227 break;
228 }
229 }
230 break;
231 }
232
233 $output.=$wrap[0].htmlspecialchars($v['tag']).$wrap[1];
234 $output.=$innerWrap[0].$this->markUpRecursively($v['sub'],$v['tagName'],$nextApp).$innerWrap[1];
235 $output.=$wrap[0].htmlspecialchars('</'.$v['tagName'].'>').$wrap[1];
236 } else {
237 $output.=htmlspecialchars($v);
238 }
239 }
240
241 return $output;
242 }
243 }
244
245
246 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_syntaxhl.php']) {
247 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_syntaxhl.php']);
248 }
249 ?>