18 require_once SQ_CORE_PACKAGE_PATH.
'/content_type/content_type.inc';
43 $this->_ser_attrs = TRUE;
44 parent::__construct($assetid);
62 $classes = $GLOBALS[
'SQ_SYSTEM']->getUserPrefs($this->
type(),
'SQ_CODE_EDITOR_HTML_CLASSES');
65 $highlighted = str_replace(
'<!--',
'<!--', $highlighted);
66 $highlighted = str_replace(
'-->',
'-->', $highlighted);
73 preg_match_all(
'/<([A-Za-z]+[A-Za-z0-9_]*)(\s*\/)>/i', $highlighted, $matches);
75 $count = count($matches[0]);
76 for ($i = 0; $i < $count; $i++) {
77 $needle = $matches[0][$i];
78 $replace =
'SPANSTART class="'.$classes[
'tag_brackets'].
'"><ENDSPAN';
79 $replace .=
'SPANSTART class="'.$classes[
'tag_name'].
'">'.$matches[1][$i].
'ENDSPAN';
80 $replace .=
'SPANSTART class="'.$classes[
'tag_brackets'].
'">'.$matches[2][$i].
'>ENDSPAN';
81 $highlighted = str_replace($needle, $replace, $highlighted);
86 preg_match_all(
'/<\/\s*([A-Za-z][A-Za-z0-9_]*)>/i', $highlighted, $matches);
88 $count = count($matches[0]);
89 for ($i = 0; $i < $count; $i++) {
90 $needle = $matches[0][$i];
91 $replace =
'SPANSTART class="'.$classes[
'tag_brackets'].
'"></ENDSPAN';
92 $replace .=
'SPANSTART class="'.$classes[
'tag_name'].
'">'.$matches[1][$i].
'ENDSPAN';
93 $replace .=
'SPANSTART class="'.$classes[
'tag_brackets'].
'">>ENDSPAN';
94 $highlighted = str_replace($needle, $replace, $highlighted);
97 $simple_matches = Array();
101 preg_match_all(
'/<\s*([A-Za-z]+[A-Za-z0-9_]*)>/i', $highlighted, $matches);
103 $count = count($matches[0]);
104 for ($i = 0; $i < $count; $i++) {
105 $needle = $matches[0][$i];
106 $replace =
'SPANSTART class="'.$classes[
'tag_brackets'].
'"><ENDSPAN';
107 $replace .=
'SPANSTART class="'.$classes[
'tag_name'].
'">'.$matches[1][$i].
'ENDSPAN';
108 $replace .=
'SPANSTART class="'.$classes[
'tag_brackets'].
'">>ENDSPAN';
109 $highlighted = str_replace($needle, $replace, $highlighted);
112 foreach ($simple_matches as $needle) {
113 $replace =
'SPANSTART style="color: blue;">'.htmlspecialchars($needle).
'ENDSPAN';
114 $highlighted = str_replace($needle, $replace, $highlighted);
119 preg_match_all(
'/<\\s*([A-Za-z][A-Za-z0-9]*)\\s+([^>]*)\\s*>/i', $highlighted, $matches);
121 $count = count($matches[0]);
122 for ($i = 0; $i < $count; $i++) {
123 $original = $matches[0][$i];
124 $replace_text = $original;
125 $replace_text = str_replace(
'<',
'<', $replace_text);
126 $replace_text = str_replace(
'/>',
'ORIG_END_TAG', $replace_text);
127 $replace_text = str_replace(
'>',
'TAG_END_BRACE', $replace_text);
128 $replace_text = preg_replace(
'/([a-z]+-?_?[a-z]*)\\s*=\\s*\"([^\"]*)\"/i',
'SPANSTART class="'.$classes[
'attribute_names'].
'">$1ENDSPAN=SPANSTART class="'.$classes[
'attribute_value'].
'">"$2"ENDSPAN', $replace_text);
129 $replace_text = preg_replace(
'/<('.trim($matches[1][$i]).
')/i',
'SPANSTART class="'.$classes[
'tag_brackets'].
'"><ENDSPANSPANSTART class="'.$classes[
'tag_name'].
'">'.$matches[1][$i].
'ENDSPAN', $replace_text);
131 $highlighted = str_replace($original, $replace_text, $highlighted);
134 $highlighted = str_replace(
'<',
'<', $highlighted);
136 $highlighted = str_replace(
'SPANSTART',
'<span', $highlighted);
137 $highlighted = str_replace(
'ENDSPAN',
'</span>', $highlighted);
138 $highlighted = str_replace(
'ORIG_END_TAG',
'<span class="'.$classes[
'tag_brackets'].
'">/></span>', $highlighted);
139 $highlighted = str_replace(
'TAG_END_BRACE',
'<span class="'.$classes[
'tag_brackets'].
'">></span>', $highlighted);
158 if (trim($str) ==
'')
return '';
160 $classes = $GLOBALS[
'SQ_SYSTEM']->getUserPrefs($this->
type(),
'SQ_CODE_EDITOR_PHP_CLASSES');
162 $system_colours = Array();
163 $system_colours[strtolower(strval(ini_get(
'highlight.bg')))] = $classes[
'background'];
164 $system_colours[strtolower(strval(ini_get(
'highlight.default')))] = $classes[
'default'];
165 $system_colours[strtolower(strval(ini_get(
'highlight.html')))] = $classes[
'html'];
166 $system_colours[strtolower(strval(ini_get(
'highlight.keyword')))] = $classes[
'keyword'];
167 $system_colours[strtolower(strval(ini_get(
'highlight.comment')))] = $classes[
'comment'];
168 $system_colours[strtolower(strval(ini_get(
'highlight.string')))] = $classes[
'string'];
172 if (substr_count($highlighted,
';') <= 1) {
176 $highlighted = highlight_string($highlighted, TRUE);
178 $highlighted = str_replace(
'<code>',
'', $highlighted);
179 $highlighted = str_replace(
'</code>',
'', $highlighted);
180 $highlighted = str_replace(
' ',
' ', $highlighted);
182 $highlighted = str_replace(Array(
'<font ',
'</font>'), Array(
'<span ',
'</span>'), $highlighted);
183 foreach ($classes as $colour => $class) {
184 $highlighted = str_replace(
'color="'.strtoupper($colour).
'"',
'class="'.$class.
'"', $highlighted);
188 preg_match_all(
'#color="(.*?)"#', $highlighted, $matches);
189 $count = count($matches[0]);
190 for ($i = 0; $i < $count; $i++) {
191 $needle = $matches[0][$i];
192 $replace_class = $system_colours[strtolower($matches[1][$i])];
193 $replace =
'class="'.$replace_class.
'"';
194 $highlighted = str_replace($needle, $replace, $highlighted);
198 if ($one_liner == TRUE) {
199 $highlighted = str_replace(
"\n",
'', $highlighted);
206 $highlighted = str_replace(
'<br />',
"\n", $highlighted);
225 'abstract',
'as',
'boolean',
'break',
'byte',
'case',
'catch',
'char',
'class',
'continue',
'const',
226 'debugger',
'default',
'delete',
'do',
'double',
'else',
'enum',
'export',
'extends',
'FALSE',
'final',
227 'finally',
'float',
'for',
'function',
'goto',
'if',
'implements',
'import',
'in',
'instanceof',
'int',
'interface',
228 'is',
'long',
'namespace',
'native',
'new',
'null',
'package',
'private',
'protected',
'public',
'return',
'short',
'static',
229 'super',
'switch',
'synchronized',
'this',
'throw',
'throws',
'transient',
'TRUE',
'try',
'typeof',
'use',
'var',
'void',
'volatile',
232 $highlighted = str_replace(Array(
'jsender',
'jsstarter'),
'' , $str);
235 $classes = $GLOBALS[
'SQ_SYSTEM']->getUserPrefs($this->
type(),
'SQ_CODE_EDITOR_JS_CLASSES');
237 $highlighted = preg_replace(
'/(\".*\")/i',
'<span class="'.$classes[
'string'].
'">\\1</span>', $highlighted);
238 $highlighted = preg_replace(
'/(\'.*\')/i',
'<span class="'.$classes[
'string'].
'">\\1</span>', $highlighted);
242 foreach ($reserved as $word) {
243 $highlighted = str_replace(
' '.$word.
' ',
'<span class="'.$classes[
'reserved'].
'"> '.$word.
' </span>', $highlighted);
244 $highlighted = str_replace(
' '.$word.
'.',
'<span class="'.$classes[
'reserved'].
'"> '.$word.
'.</span>', $highlighted);
247 $highlighted = preg_replace(
'/([a-zA-Z_]*)\((.*)\)/i',
'<span class="'.$classes[
'function'].
'">\\1</span><span class="'.$classes[
'brackets'].
'">(</span><span class="'.$classes[
'default'].
'">\\2</span><span class="'.$classes[
'brackets'].
'">)</span>', $highlighted);
248 $highlighted = preg_replace(
'/\/\/(.*)\n/',
'<span class="'.$classes[
'comment'].
'">//\\1</span>', $highlighted);
269 $php_start_token =
'PHPSTARTTAG';
270 $php_end_token =
'PHPENDTAG';
273 $str = str_replace(
"\r",
'', $str);
277 preg_match_all(
'/<\?\s*xml.*\?>/i', $str, $matches);
278 foreach ($matches as $text) {
279 $replace = str_replace(Array(
'<?',
'?>'), Array(
'<XMLSTART',
'XMLEND>'), $text);
280 $str = str_replace($text, $replace, $str);
283 $str = str_replace(
'<?php', $php_start_token, $str);
284 $str = str_replace(
'<?PHP', $php_start_token, $str);
285 $str = str_replace(
'<?', $php_start_token.
' ', $str);
286 $str = str_replace(
' ?>',
' '.$php_end_token, $str);
287 $str = str_replace(
'<?', $php_start_token.
' ', $str);
288 $str = str_replace(
'?>', $php_end_token, $str);
292 $str = preg_replace(
'/\"(.*)'.$php_start_token.
'(.*)'.$php_end_token.
'(.*)\"/i',
'"\\1inlinephpstart\\2inlinephpend\\3"',$str);
296 $str = preg_replace(
'/(script .*javascript.*>)/i',
'\\1jsstarter', $str);
297 $str = preg_replace(
'/(<\/script>)/i',
'jsender\\1', $str);
300 while (FALSE !== ($start_pos = strpos($str, $php_start_token))) {
301 $other_string = substr($str, 0, $start_pos);
304 $str = substr($str, $start_pos);
305 $length = strlen($str);
306 if (strpos($str, $php_end_token) === FALSE) {
307 $str .= $php_end_token;
310 $end_pos = strpos($str, $php_end_token);
311 $end_pos += strlen($php_end_token);
312 $php_string = substr($str, 0, $end_pos);
314 $php_string = str_replace($php_start_token,
'<?php', $php_string);
315 $php_string = str_replace($php_end_token,
'?>', $php_string);
320 $php_highlighted = preg_replace(
'/(<span class="php_html">)\n/i',
'\\1', $php_highlighted, 1);
321 $highlighted .= $php_highlighted;
323 $str = substr($str, strpos($str, $php_end_token) + strlen($php_end_token));
330 $start_pos = strpos($highlighted,
'jsstarter');
331 $end_pos = strpos($highlighted,
'jsender');
332 $script = substr($highlighted, $start_pos, $end_pos - $start_pos + 7);
334 if ($end_pos !== FALSE && $start_pos !== FALSE) {
336 $highlighted = str_replace($script, $result, $highlighted);
338 $highlighted = str_replace(Array (
'jsender',
'jsstarter'),
'', $highlighted);
341 $highlighted = str_replace(Array(
'inlinephpstart',
'inlinephpend'), Array(
'<?php',
'?>'), $highlighted);
342 $highlighted = str_replace(
'>jsender<',
'><', $highlighted);
343 $highlighted = str_replace(Array(
'XMLSTART',
'XMLEND'), Array(
'?',
'?'), $highlighted);
345 $line_num_prefs = $GLOBALS[
'SQ_SYSTEM']->getUserPrefs($this->
type(),
'SQ_CODE_EDITOR_LINE_NUMBERS');
346 $current_line_settings = $this->
attr(
'line_numbers');
347 foreach ($current_line_settings as $name => $value) {
348 $line_num_prefs[$name] = $value;
353 $lines = explode(
"\n", $highlighted);
355 for ($i = count($lines) -1; $i > 0; $i--) {
357 if ((trim($lines[$i]) ==
'') || (trim($lines[$i]) ==
'</span>')) {
358 $lines[$i - 1] .= $lines[$i];
364 $highlighted = implode(
"\n", $lines);
365 if ($line_num_prefs[
'use_line_numbers'] == TRUE) {
366 $highlighted = $this->
addLineNumbers($highlighted, $line_num_prefs[
'line_number_left'], $line_num_prefs[
'line_number_style'], $line_num_prefs[
'line_number_class']);
368 return '<pre>'.$highlighted.
'</pre>';
387 $lines = explode(
"\n", $str);
389 if ($left == FALSE) $style=
'span';
390 switch (strtolower($style)) {
395 $replace =
'<span class="'.$class.
'" style="-margin_value-">-line_number-</span>';
396 $replace =
"\n$replace";
400 $str = preg_replace(
'/\n/i', $replace, $str);
402 $str = $replace.$str;
407 preg_match_all(
'/-line_number-/i', $str, $matches);
408 $line_number = count($matches[0]);
413 while (FALSE !== strpos($str,
'-line_number-')) {
414 $filler = str_repeat(
' ', strlen(strval($line_number)) - strlen(strval($current_line)));
415 $str = preg_replace(
'/-line_number-/i', $filler.$current_line.
':', $str, 1);
420 $margin = strlen(strval($line_number)) * 6 + 5;
422 for ($i = 1; $i <= $line_number; $i++) {
423 $replace =
'margin-right: 10px;';
424 $str = preg_replace(
'/-margin_value-/i', $replace, $str, 1);
428 $new_str =
'<table style="width: 100%;" ><tr><td><pre>'.$str.
'</pre></td><td style="width: 100px;" >';
429 $line_numbers = range(1, count($lines));
430 $new_str .=
'<pre>'.implode(
"\n", $line_numbers).
'</pre>';
431 $new_str .=
'</td></tr></table>';
436 $str = implode(
'</pre></li><li class="'.$class.
'"><pre>', $lines);
437 $str =
'<ol><li class="'.$class.
'">'.$str.
'</li></ol>';