-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass.tx_templavoila_syntaxhl.php
378 lines (324 loc) · 12.1 KB
/
class.tx_templavoila_syntaxhl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/***************************************************************
* Copyright notice
*
* (c) 1999-2011 Kasper Skårhøj ([email protected])
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Contains a class for various syntax highlighting.
*
* $Id$
*
* @author Kasper Skårhøj <[email protected]>
*/
/**
* [CLASS/FUNCTION INDEX of SCRIPT]
*
*
*
* 84: class tx_templavoila_syntaxhl
*
* SECTION: Markup of Data Structure, <T3DataStructure>
* 156: function highLight_DS($str)
* 183: function highLight_DS_markUpRecursively($struct,$parent='',$app='')
*
* SECTION: Markup of Data Structure, <T3FlexForms>
* 268: function highLight_FF($str)
* 295: function highLight_FF_markUpRecursively($struct,$parent='',$app='')
*
* SECTION: Various
* 376: function getAllTags($str)
* 407: function splitXMLbyTags($tagList,$str)
*
* TOTAL FUNCTIONS: 6
* (This index is automatically created/updated by the extension "extdeveval")
*
*/
/**
* Syntax Highlighting class.
*
* @author Kasper Skårhøj <[email protected]>
* @package TYPO3
* @subpackage t3lib
*/
class tx_templavoila_syntaxhl {
// Internal, dynamic:
var $htmlParse; // Parse object.
// External, static:
var $DS_wrapTags = array(
'T3DataStructure' => array('<span style="font-weight: bold;">', '</span>'),
'type' => array('<span style="font-weight: bold; color: #000080;">', '</span>'),
'section' => array('<span style="font-weight: bold; color: #000080;">', '</span>'),
'el' => array('<span style="font-weight: bold; color: #800000;">', '</span>'),
'meta' => array('<span style="font-weight: bold; color: #800080;">', '</span>'),
'_unknown' => array('<span style="font-style: italic; color: #666666;">', '</span>'),
'_applicationTag' => array('<span style="font-weight: bold; color: #FF6600;">', '</span>'),
'_applicationContents' => array('<span style="font-style: italic; color: #C29336;">', '</span>'),
'sheets' => array('<span style="font-weight: bold; color: #008000;">', '</span>'),
'parent:sheets' => array('<span style="color: #008000;">', '</span>'),
'ROOT' => array('<span style="font-weight: bold; color: #008080;">', '</span>'),
'parent:el' => array('<span style="font-weight: bold; color: #008080;">', '</span>'),
'langDisable' => array('<span style="color: #000080;">', '</span>'),
'langChildren' => array('<span style="color: #000080;">', '</span>'),
);
var $FF_wrapTags = array(
'T3FlexForms' => array('<span style="font-weight: bold;">', '</span>'),
'meta' => array('<span style="font-weight: bold; color: #800080;">', '</span>'),
'data' => array('<span style="font-weight: bold; color: #800080;">', '</span>'),
'el' => array('<span style="font-weight: bold; color: #80a000;">', '</span>'),
'itemType' => array('<span style="font-weight: bold; color: #804000;">', '</span>'),
'section' => array('<span style="font-weight: bold; color: #604080;">', '</span>'),
'numIndex' => array('<span style="color: #333333;">', '</span>'),
'_unknown' => array('<span style="font-style: italic; color: #666666;">', '</span>'),
'sDEF' => array('<span style="font-weight: bold; color: #008000;">', '</span>'),
'level:sheet' => array('<span style="font-weight: bold; color: #008000;">', '</span>'),
'lDEF' => array('<span style="font-weight: bold; color: #000080;">', '</span>'),
'level:language' => array('<span style="font-weight: bold; color: #000080;">', '</span>'),
'level:fieldname' => array('<span style="font-weight: bold; color: #666666;">', '</span>'),
'vDEF' => array('<span style="font-weight: bold; color: #008080;">', '</span>'),
'level:value' => array('<span style="font-weight: bold; color: #008080;">', '</span>'),
'currentSheetId' => array('<span style="color: #000080;">', '</span>'),
'currentLangId' => array('<span style="color: #000080;">', '</span>'),
);
/*************************************
*
* Markup of Data Structure, <T3DataStructure>
*
*************************************/
/**
* Makes syntax highlighting of a Data Structure, <T3DataStructure>
*
* @param string Data Structure XML, must be valid since it's parsed.
* @return string HTML code with highlighted content. Must be wrapped in <PRE> tags
*/
function highLight_DS($str) {
// Parse DS to verify that it is valid:
$DS = t3lib_div::xml2array($str);
if (is_array($DS)) {
$completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
// Highlighting source:
$this->htmlParse = t3lib_div::makeInstance('t3lib_parsehtml'); // Init parser object
$struct = $this->splitXMLbyTags(implode(',', $completeTagList), $str); // Split the XML by the found tags, recursively into LARGE array.
$markUp = $this->highLight_DS_markUpRecursively($struct); // Perform color-markup on the parsed content. Markup preserves the LINE formatting of the XML.
// Return content:
return $markUp;
} else {
$error = 'ERROR: The input content failed XML parsing: ' . $DS;
}
return $error;
}
/**
* Making syntax highlighting of the parsed Data Structure XML.
* Called recursively.
*
* @param array The structure, see splitXMLbyTags()
* @param string Parent tag.
* @param string "Application" - used to denote if we are 'inside' a section
* @return string HTML
*/
function highLight_DS_markUpRecursively($struct, $parent = '', $app = '') {
$output = '';
foreach ($struct as $k => $v) {
if ($k % 2) {
$nextApp = $app;
$wrap = array('', '');
switch ($app) {
case 'TCEforms':
case 'tx_templavoila':
$wrap = $this->DS_wrapTags['_applicationContents'];
break;
case 'el':
default:
if ($parent == 'el') {
$wrap = $this->DS_wrapTags['parent:el'];
$nextApp = 'el';
} elseif ($parent == 'sheets') {
$wrap = $this->DS_wrapTags['parent:sheets'];
} else {
$wrap = $this->DS_wrapTags[$v['tagName']];
$nextApp = '';
}
// If no wrap defined, us "unknown" definition
if (!is_array($wrap)) {
$wrap = $this->DS_wrapTags['_unknown'];
}
// Check for application sections in the XML:
if ($app == 'el' || $parent == 'ROOT') {
switch ($v['tagName']) {
case 'TCEforms':
case 'tx_templavoila':
$nextApp = $v['tagName'];
$wrap = $this->DS_wrapTags['_applicationTag'];
break;
}
}
break;
}
$output .= $wrap[0] . htmlspecialchars($v['tag']) . $wrap[1];
$output .= $this->highLight_DS_markUpRecursively($v['sub'], $v['tagName'], $nextApp);
$output .= $wrap[0] . htmlspecialchars('</' . $v['tagName'] . '>') . $wrap[1];
} else {
$output .= htmlspecialchars($v);
}
}
return $output;
}
/*************************************
*
* Markup of Data Structure, <T3FlexForms>
*
*************************************/
/**
* Makes syntax highlighting of a FlexForm Data, <T3FlexForms>
*
* @param string Data Structure XML, must be valid since it's parsed.
* @return string HTML code with highlighted content. Must be wrapped in <PRE> tags
*/
function highLight_FF($str) {
// Parse DS to verify that it is valid:
$DS = t3lib_div::xml2array($str);
if (is_array($DS)) {
$completeTagList = array_unique($this->getAllTags($str)); // Complete list of tags in DS
// Highlighting source:
$this->htmlParse = t3lib_div::makeInstance('t3lib_parsehtml'); // Init parser object
$struct = $this->splitXMLbyTags(implode(',', $completeTagList), $str); // Split the XML by the found tags, recursively into LARGE array.
$markUp = $this->highLight_FF_markUpRecursively($struct); // Perform color-markup on the parsed content. Markup preserves the LINE formatting of the XML.
// Return content:
return $markUp;
} else {
$error = 'ERROR: The input content failed XML parsing: ' . $DS;
}
return $error;
}
/**
* Making syntax highlighting of the parsed FlexForm XML.
* Called recursively.
*
* @param array The structure, see splitXMLbyTags()
* @param string Parent tag.
* @param string "Application" - used to denote if we are 'inside' a section
* @return string HTML
*/
function highLight_FF_markUpRecursively($struct, $parent = '', $app = '') {
$output = '';
// Setting levels:
if ($parent == 'data') {
$app = 'sheet';
} elseif ($app == 'sheet') {
$app = 'language';
} elseif ($app == 'language') {
$app = 'fieldname';
} elseif ($app == 'fieldname') {
$app = 'value';
} elseif ($app == 'el' || $app == 'numIndex') {
$app = 'fieldname';
}
// Traverse structure:
foreach ($struct as $k => $v) {
if ($k % 2) {
$wrap = array('', '');
if ($v['tagName'] == 'numIndex') {
$app = 'numIndex';
}
// Default wrap:
$wrap = $this->FF_wrapTags[$v['tagName']];
// If no wrap defined, us "unknown" definition
if (!is_array($wrap)) {
switch ($app) {
case 'sheet':
case 'language':
case 'fieldname':
case 'value':
$wrap = $this->FF_wrapTags['level:' . $app];
break;
default:
$wrap = $this->FF_wrapTags['_unknown'];
break;
}
}
if ($v['tagName'] == 'el') {
$app = 'el';
}
$output .= $wrap[0] . htmlspecialchars($v['tag']) . $wrap[1];
$output .= $this->highLight_FF_markUpRecursively($v['sub'], $v['tagName'], $app);
$output .= $wrap[0] . htmlspecialchars('</' . $v['tagName'] . '>') . $wrap[1];
} else {
$output .= htmlspecialchars($v);
}
}
return $output;
}
/*************************************
*
* Various
*
*************************************/
/**
* Returning all tag names found in XML/HTML input string
*
* @param string HTML/XML input
* @return array Array with all found tags (starttags only)
*/
function getAllTags($str) {
// Init:
$tags = array();
$token = md5(microtime());
// Markup all tag names with token.
$markUpStr = preg_replace('/<([[:alnum:]_]+)[^>]*>/', $token . '${1}' . $token, $str);
// Splitting by token:
$parts = explode($token, $markUpStr);
// Traversing parts:
foreach ($parts as $k => $v) {
if ($k % 2) {
$tags[] = $v;
}
}
// Returning tags:
return $tags;
}
/**
* Splitting the input source by the tags listing in $tagList.
* Called recursively.
*
* @param string Commalist of tags to split source by (into blocks, ALL being block-tags!)
* @param string Input string.
* @return array Array with the content arranged hierarchically.
*/
function splitXMLbyTags($tagList, $str) {
$struct = $this->htmlParse->splitIntoBlock($tagList, $str);
// Traverse level:
foreach ($struct as $k => $v) {
if ($k % 2) {
$tag = $this->htmlParse->getFirstTag($v);
$tagName = $this->htmlParse->getFirstTagName($tag, TRUE);
$struct[$k] = array(
'tag' => $tag,
'tagName' => $tagName,
'sub' => $this->splitXMLbyTags($tagList, $this->htmlParse->removeFirstAndLastTag($struct[$k]))
);
}
}
return $struct;
}
}
?>