Merging updated GeSHi code for 1.0.7.9 updates
[viewsvn.git] / includes / geshi / geshi.php
1 <?php
2 /**
3 * GeSHi - Generic Syntax Highlighter
4 *
5 * The GeSHi class for Generic Syntax Highlighting. Please refer to the documentation
6 * at http://qbnz.com/highlighter/documentation.php for more information about how to
7 * use this class.
8 *
9 * For changes, release notes, TODOs etc, see the relevant files in the docs/ directory
10 *
11 * This file is part of GeSHi.
12 *
13 * GeSHi is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * GeSHi is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with GeSHi; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 * @package core
28 * @author Nigel McNie <nigel@geshi.org>
29 * @copyright Copyright &copy; 2004, 2005, Nigel McNie
30 * @license http://gnu.org/copyleft/gpl.html GNU GPL
31 * @version $Id: geshi.php,v 1.36 2006/04/23 00:15:26 oracleshinoda Exp $
32 *
33 */
34
35 //
36 // GeSHi Constants
37 // You should use these constant names in your programs instead of
38 // their values - you never know when a value may change in a future
39 // version
40 //
41
42 /** The version of this GeSHi file */
43 define('GESHI_VERSION', '1.0.7.9');
44
45 /** Set the correct directory separator */
46 define('GESHI_DIR_SEPARATOR', ('WIN' != substr(PHP_OS, 0, 3)) ? '/' : '\\');
47
48 // Define the root directory for the GeSHi code tree
49 if (!defined('GESHI_ROOT')) {
50 /** The root directory for GeSHi */
51 define('GESHI_ROOT', dirname(__FILE__) . GESHI_DIR_SEPARATOR);
52 }
53 /** The language file directory for GeSHi
54 @access private */
55 define('GESHI_LANG_ROOT', GESHI_ROOT . 'geshi' . GESHI_DIR_SEPARATOR);
56
57
58 // Line numbers - use with enable_line_numbers()
59 /** Use no line numbers when building the result */
60 define('GESHI_NO_LINE_NUMBERS', 0);
61 /** Use normal line numbers when building the result */
62 define('GESHI_NORMAL_LINE_NUMBERS', 1);
63 /** Use fancy line numbers when building the result */
64 define('GESHI_FANCY_LINE_NUMBERS', 2);
65
66 // Container HTML type
67 /** Use nothing to surround the source */
68 define('GESHI_HEADER_NONE', 0);
69 /** Use a "div" to surround the source */
70 define('GESHI_HEADER_DIV', 1);
71 /** Use a "pre" to surround the source */
72 define('GESHI_HEADER_PRE', 2);
73
74 // Capatalisation constants
75 /** Lowercase keywords found */
76 define('GESHI_CAPS_NO_CHANGE', 0);
77 /** Uppercase keywords found */
78 define('GESHI_CAPS_UPPER', 1);
79 /** Leave keywords found as the case that they are */
80 define('GESHI_CAPS_LOWER', 2);
81
82 // Link style constants
83 /** Links in the source in the :link state */
84 define('GESHI_LINK', 0);
85 /** Links in the source in the :hover state */
86 define('GESHI_HOVER', 1);
87 /** Links in the source in the :active state */
88 define('GESHI_ACTIVE', 2);
89 /** Links in the source in the :visited state */
90 define('GESHI_VISITED', 3);
91
92 // Important string starter/finisher
93 // Note that if you change these, they should be as-is: i.e., don't
94 // write them as if they had been run through htmlentities()
95 /** The starter for important parts of the source */
96 define('GESHI_START_IMPORTANT', '<BEGIN GeSHi>');
97 /** The ender for important parts of the source */
98 define('GESHI_END_IMPORTANT', '<END GeSHi>');
99
100 /**#@+
101 * @access private
102 */
103 // When strict mode applies for a language
104 /** Strict mode never applies (this is the most common) */
105 define('GESHI_NEVER', 0);
106 /** Strict mode *might* apply, and can be enabled or
107 disabled by {@link GeSHi::enable_strict_mode()} */
108 define('GESHI_MAYBE', 1);
109 /** Strict mode always applies */
110 define('GESHI_ALWAYS', 2);
111
112 // Advanced regexp handling constants, used in language files
113 /** The key of the regex array defining what to search for */
114 define('GESHI_SEARCH', 0);
115 /** The key of the regex array defining what bracket group in a
116 matched search to use as a replacement */
117 define('GESHI_REPLACE', 1);
118 /** The key of the regex array defining any modifiers to the regular expression */
119 define('GESHI_MODIFIERS', 2);
120 /** The key of the regex array defining what bracket group in a
121 matched search to put before the replacement */
122 define('GESHI_BEFORE', 3);
123 /** The key of the regex array defining what bracket group in a
124 matched search to put after the replacement */
125 define('GESHI_AFTER', 4);
126
127 /** Used in language files to mark comments */
128 define('GESHI_COMMENTS', 0);
129
130 // Error detection - use these to analyse faults
131 /** No sourcecode to highlight was specified
132 * @deprecated
133 */
134 define('GESHI_ERROR_NO_INPUT', 1);
135 /** The language specified does not exist */
136 define('GESHI_ERROR_NO_SUCH_LANG', 2);
137 /** GeSHi could not open a file for reading (generally a language file) */
138 define('GESHI_ERROR_FILE_NOT_READABLE', 3);
139 /** The header type passed to {@link GeSHi::set_header_type()} was invalid */
140 define('GESHI_ERROR_INVALID_HEADER_TYPE', 4);
141 /** The line number type passed to {@link GeSHi::enable_line_numbers()} was invalid */
142 define('GESHI_ERROR_INVALID_LINE_NUMBER_TYPE', 5);
143 /**#@-*/
144
145
146 /**
147 * The GeSHi Class.
148 *
149 * Please refer to the documentation for GeSHi 1.0.X that is available
150 * at http://qbnz.com/highlighter/documentation.php for more information
151 * about how to use this class.
152 *
153 * @package core
154 * @author Nigel McNie <nigel@geshi.org>
155 * @copyright Copyright &copy; 2004, 2005 Nigel McNie
156 */
157 class GeSHi
158 {
159 /**#@+
160 * @access private
161 */
162 /**
163 * The source code to highlight
164 * @var string
165 */
166 var $source = '';
167
168 /**
169 * The language to use when highlighting
170 * @var string
171 */
172 var $language = '';
173
174 /**
175 * The data for the language used
176 * @var array
177 */
178 var $language_data = array();
179
180 /**
181 * The path to the language files
182 * @var string
183 */
184 var $language_path = GESHI_LANG_ROOT;
185
186 /**
187 * The error message associated with an error
188 * @var string
189 * @todo check err reporting works
190 */
191 var $error = false;
192
193 /**
194 * Possible error messages
195 * @var array
196 */
197 var $error_messages = array(
198 //GESHI_ERROR_NO_INPUT => 'No source code inputted',
199 GESHI_ERROR_NO_SUCH_LANG => 'GeSHi could not find the language {LANGUAGE} (using path {PATH})',
200 GESHI_ERROR_FILE_NOT_READABLE => 'The file specified for load_from_file was not readable',
201 GESHI_ERROR_INVALID_HEADER_TYPE => 'The header type specified is invalid',
202 GESHI_ERROR_INVALID_LINE_NUMBER_TYPE => 'The line number type specified is invalid'
203 );
204
205 /**
206 * Whether highlighting is strict or not
207 * @var boolean
208 */
209 var $strict_mode = false;
210
211 /**
212 * Whether to use CSS classes in output
213 * @var boolean
214 */
215 var $use_classes = false;
216
217 /**
218 * The type of header to use. Can be one of the following
219 * values:
220 *
221 * <ul>
222 * <li><b>GESHI_HEADER_PRE</b>: Source is outputted in
223 * a &lt;pre&gt; HTML element.</li>
224 * <li><b>GESHI_HEADER_DIV</b>: Source is outputted in
225 * a &lt;div&gt; HTML element.</li>
226 * <li><b>GESHI_HEADER_NONE</b>: No header is outputted.</li>
227 * </ul>
228 *
229 * @var int
230 */
231 var $header_type = GESHI_HEADER_PRE;
232
233 /**
234 * Array of permissions for which lexics should be highlighted
235 * @var array
236 */
237 var $lexic_permissions = array(
238 'KEYWORDS' => array(),
239 'COMMENTS' => array('MULTI' => true),
240 'REGEXPS' => array(),
241 'ESCAPE_CHAR' => true,
242 'BRACKETS' => true,
243 'SYMBOLS' => true,
244 'STRINGS' => true,
245 'NUMBERS' => true,
246 'METHODS' => true,
247 'SCRIPT' => true
248 );
249
250 /**
251 * The time it took to parse the code
252 * @var double
253 */
254 var $time = 0;
255
256 /**
257 * The content of the header block
258 * @var string
259 */
260 var $header_content = '';
261
262 /**
263 * The content of the footer block
264 * @var string
265 */
266 var $footer_content = '';
267
268 /**
269 * The style of the header block
270 * @var string
271 */
272 var $header_content_style = '';
273
274 /**
275 * The style of the footer block
276 * @var string
277 */
278 var $footer_content_style = '';
279
280 /**
281 * The styles for hyperlinks in the code
282 * @var array
283 */
284 var $link_styles = array();
285
286 /**
287 * Whether important blocks should be recognised or not
288 * @var boolean
289 * @deprecated
290 * @todo REMOVE THIS FUNCTIONALITY!
291 */
292 var $enable_important_blocks = false;
293
294 /**
295 * Styles for important parts of the code
296 * @var string
297 * @deprecated
298 * @todo As above - rethink the whole idea of important blocks as it is buggy and
299 * will be hard to implement in 1.2
300 */
301 var $important_styles = 'font-weight: bold; color: red;'; // Styles for important parts of the code
302
303 /**
304 * Whether CSS IDs should be added to the code
305 * @var boolean
306 */
307 var $add_ids = false;
308
309 /**
310 * Lines that should be highlighted extra
311 * @var array
312 */
313 var $highlight_extra_lines = array();
314
315 /**
316 * Styles of extra-highlighted lines
317 * @var string
318 */
319 var $highlight_extra_lines_style = 'color: #cc0; background-color: #ffc;';
320
321 /**
322 * Number at which line numbers should start at
323 * @var int
324 * @todo Warning documentation about XHTML compliance
325 */
326 var $line_numbers_start = 1;
327
328 /**
329 * The overall style for this code block
330 * @var string
331 */
332 var $overall_style = '';
333
334 /**
335 * The style for the actual code
336 * @var string
337 */
338 var $code_style = 'font-family: \'Courier New\', Courier, monospace; font-weight: normal;';
339
340 /**
341 * The overall class for this code block
342 * @var string
343 */
344 var $overall_class = '';
345
346 /**
347 * The overall ID for this code block
348 * @var string
349 */
350 var $overall_id = '';
351
352 /**
353 * Line number styles
354 * @var string
355 */
356 var $line_style1 = 'font-family: \'Courier New\', Courier, monospace; color: black; font-weight: normal; font-style: normal;';
357
358 /**
359 * Line number styles for fancy lines
360 * @var string
361 */
362 var $line_style2 = 'font-weight: bold;';
363
364 /**
365 * Flag for how line nubmers are displayed
366 * @var boolean
367 */
368 var $line_numbers = GESHI_NO_LINE_NUMBERS;
369
370 /**
371 * The "nth" value for fancy line highlighting
372 * @var int
373 */
374 var $line_nth_row = 0;
375
376 /**
377 * The size of tab stops
378 * @var int
379 */
380 var $tab_width = 8;
381
382 /**
383 * Default target for keyword links
384 * @var string
385 */
386 var $link_target = '';
387
388 /**
389 * The encoding to use for entity encoding
390 * @var string
391 */
392 var $encoding = 'ISO-8859-1';
393
394 /**#@-*/
395
396 /**
397 * Creates a new GeSHi object, with source and language
398 *
399 * @param string The source code to highlight
400 * @param string The language to highlight the source with
401 * @param string The path to the language file directory. <b>This
402 * is deprecated!</b> I've backported the auto path
403 * detection from the 1.1.X dev branch, so now it
404 * should be automatically set correctly. If you have
405 * renamed the language directory however, you will
406 * still need to set the path using this parameter or
407 * {@link GeSHi::set_language_path()}
408 * @since 1.0.0
409 */
410 function GeSHi ($source, $language, $path = '')
411 {
412 $this->set_source($source);
413 $this->set_language_path($path);
414 $this->set_language($language);
415 }
416
417 /**
418 * Returns an error message associated with the last GeSHi operation,
419 * or false if no error has occured
420 *
421 * @return string|false An error message if there has been an error, else false
422 * @since 1.0.0
423 */
424 function error ()
425 {
426 if ($this->error) {
427 $msg = $this->error_messages[$this->error];
428 $debug_tpl_vars = array(
429 '{LANGUAGE}' => $this->language,
430 '{PATH}' => $this->language_path
431 );
432 foreach ($debug_tpl_vars as $tpl => $var) {
433 $msg = str_replace($tpl, $var, $msg);
434 }
435 return "<br /><strong>GeSHi Error:</strong> $msg (code $this->error)<br />";
436 }
437 return false;
438 }
439
440 /**
441 * Gets a human-readable language name (thanks to Simon Patterson
442 * for the idea :))
443 *
444 * @return string The name for the current language
445 * @since 1.0.2
446 */
447 function get_language_name ()
448 {
449 if (GESHI_ERROR_NO_SUCH_LANG == $this->_error) {
450 return $this->language_data['LANG_NAME'] . ' (Unknown Language)';
451 }
452 return $this->language_data['LANG_NAME'];
453 }
454
455 /**
456 * Sets the source code for this object
457 *
458 * @param string The source code to highlight
459 * @since 1.0.0
460 */
461 function set_source ($source)
462 {
463 $this->source = $source;
464 }
465
466 /**
467 * Sets the language for this object
468 *
469 * @param string The name of the language to use
470 * @since 1.0.0
471 */
472 function set_language ($language)
473 {
474 $this->error = false;
475 $this->strict_mode = GESHI_NEVER;
476
477 $language = preg_replace('#[^a-zA-Z0-9\-_]#', '', $language);
478 $this->language = strtolower($language);
479
480 $file_name = $this->language_path . $this->language . '.php';
481 if (!is_readable($file_name)) {
482 $this->error = GESHI_ERROR_NO_SUCH_LANG;
483 return;
484 }
485 // Load the language for parsing
486 $this->load_language($file_name);
487 }
488
489 /**
490 * Sets the path to the directory containing the language files. Note
491 * that this path is relative to the directory of the script that included
492 * geshi.php, NOT geshi.php itself.
493 *
494 * @param string The path to the language directory
495 * @since 1.0.0
496 * @deprecated The path to the language files should now be automatically
497 * detected, so this method should no longer be needed. The
498 * 1.1.X branch handles manual setting of the path differently
499 * so this method will disappear in 1.2.0.
500 */
501 function set_language_path ($path)
502 {
503 if ($path) {
504 $this->language_path = ('/' == substr($path, strlen($path) - 1, 1)) ? $path : $path . '/';
505 $this->set_language($this->language); // otherwise set_language_path has no effect
506 }
507 }
508
509 /**
510 * Sets the type of header to be used.
511 *
512 * If GESHI_HEADER_DIV is used, the code is surrounded in a "div".This
513 * means more source code but more control over tab width and line-wrapping.
514 * GESHI_HEADER_PRE means that a "pre" is used - less source, but less
515 * control. Default is GESHI_HEADER_PRE.
516 *
517 * From 1.0.7.2, you can use GESHI_HEADER_NONE to specify that no header code
518 * should be outputted.
519 *
520 * @param int The type of header to be used
521 * @since 1.0.0
522 */
523 function set_header_type ($type)
524 {
525 if (GESHI_HEADER_DIV != $type && GESHI_HEADER_PRE != $type && GESHI_HEADER_NONE != $type) {
526 $this->error = GESHI_ERROR_INVALID_HEADER_TYPE;
527 return;
528 }
529 $this->header_type = $type;
530 // Set a default overall style if the header is a <div>
531 if (GESHI_HEADER_DIV == $type && !$this->overall_style) {
532 $this->overall_style = 'font-family: monospace;';
533 }
534 }
535
536 /**
537 * Sets the styles for the code that will be outputted
538 * when this object is parsed. The style should be a
539 * string of valid stylesheet declarations
540 *
541 * @param string The overall style for the outputted code block
542 * @param boolean Whether to merge the styles with the current styles or not
543 * @since 1.0.0
544 */
545 function set_overall_style ($style, $preserve_defaults = false)
546 {
547 if (!$preserve_defaults) {
548 $this->overall_style = $style;
549 } else {
550 $this->overall_style .= $style;
551 }
552 }
553
554 /**
555 * Sets the overall classname for this block of code. This
556 * class can then be used in a stylesheet to style this object's
557 * output
558 *
559 * @param string The class name to use for this block of code
560 * @since 1.0.0
561 */
562 function set_overall_class ($class)
563 {
564 $this->overall_class = $class;
565 }
566
567 /**
568 * Sets the overall id for this block of code. This id can then
569 * be used in a stylesheet to style this object's output
570 *
571 * @param string The ID to use for this block of code
572 * @since 1.0.0
573 */
574 function set_overall_id ($id)
575 {
576 $this->overall_id = $id;
577 }
578
579 /**
580 * Sets whether CSS classes should be used to highlight the source. Default
581 * is off, calling this method with no arguments will turn it on
582 *
583 * @param boolean Whether to turn classes on or not
584 * @since 1.0.0
585 */
586 function enable_classes ($flag = true)
587 {
588 $this->use_classes = ($flag) ? true : false;
589 }
590
591 /**
592 * Sets the style for the actual code. This should be a string
593 * containing valid stylesheet declarations. If $preserve_defaults is
594 * true, then styles are merged with the default styles, with the
595 * user defined styles having priority
596 *
597 * Note: Use this method to override any style changes you made to
598 * the line numbers if you are using line numbers, else the line of
599 * code will have the same style as the line number! Consult the
600 * GeSHi documentation for more information about this.
601 *
602 * @param string The style to use for actual code
603 * @param boolean Whether to merge the current styles with the new styles
604 */
605 function set_code_style ($style, $preserve_defaults = false)
606 {
607 if (!$preserve_defaults) {
608 $this->code_style = $style;
609 } else {
610 $this->code_style .= $style;
611 }
612 }
613
614 /**
615 * Sets the styles for the line numbers.
616 *
617 * @param string The style for the line numbers that are "normal"
618 * @param string|boolean If a string, this is the style of the line
619 * numbers that are "fancy", otherwise if boolean then this
620 * defines whether the normal styles should be merged with the
621 * new normal styles or not
622 * @param boolean If set, is the flag for whether to merge the "fancy"
623 * styles with the current styles or not
624 * @since 1.0.2
625 */
626 function set_line_style ($style1, $style2 = '', $preserve_defaults = false)
627 {
628 if (is_bool($style2)) {
629 $preserve_defaults = $style2;
630 $style2 = '';
631 }
632 if (!$preserve_defaults) {
633 $this->line_style1 = $style1;
634 $this->line_style2 = $style2;
635 } else {
636 $this->line_style1 .= $style1;
637 $this->line_style2 .= $style2;
638 }
639 }
640
641 /**
642 * Sets whether line numbers should be displayed.
643 *
644 * Valid values for the first parameter are:
645 *
646 * <ul>
647 * <li><b>GESHI_NO_LINE_NUMBERS</b>: Line numbers will not be displayed</li>
648 * <li><b>GESHI_NORMAL_LINE_NUMBERS</b>: Line numbers will be displayed</li>
649 * <li><b>GESHI_FANCY_LINE_NUMBERS</b>: Fancy line numbers will be displayed</li>
650 * </ul>
651 *
652 * For fancy line numbers, the second parameter is used to signal which lines
653 * are to be fancy. For example, if the value of this parameter is 5 then every
654 * 5th line will be fancy.
655 *
656 * @param int How line numbers should be displayed
657 * @param int Defines which lines are fancy
658 * @since 1.0.0
659 */
660 function enable_line_numbers ($flag, $nth_row = 5)
661 {
662 if (GESHI_NO_LINE_NUMBERS != $flag && GESHI_NORMAL_LINE_NUMBERS != $flag
663 && GESHI_FANCY_LINE_NUMBERS != $flag) {
664 $this->error = GESHI_ERROR_INVALID_LINE_NUMBER_TYPE;
665 }
666 $this->line_numbers = $flag;
667 $this->line_nth_row = $nth_row;
668 }
669
670 /**
671 * Sets the style for a keyword group. If $preserve_defaults is
672 * true, then styles are merged with the default styles, with the
673 * user defined styles having priority
674 *
675 * @param int The key of the keyword group to change the styles of
676 * @param string The style to make the keywords
677 * @param boolean Whether to merge the new styles with the old or just
678 * to overwrite them
679 * @since 1.0.0
680 */
681 function set_keyword_group_style ($key, $style, $preserve_defaults = false)
682 {
683 if (!$preserve_defaults) {
684 $this->language_data['STYLES']['KEYWORDS'][$key] = $style;
685 } else {
686 $this->language_data['STYLES']['KEYWORDS'][$key] .= $style;
687 }
688 }
689
690 /**
691 * Turns highlighting on/off for a keyword group
692 *
693 * @param int The key of the keyword group to turn on or off
694 * @param boolean Whether to turn highlighting for that group on or off
695 * @since 1.0.0
696 */
697 function set_keyword_group_highlighting ( $key, $flag = true )
698 {
699 $this->lexic_permissions['KEYWORDS'][$key] = ($flag) ? true : false;
700 }
701
702 /**
703 * Sets the styles for comment groups. If $preserve_defaults is
704 * true, then styles are merged with the default styles, with the
705 * user defined styles having priority
706 *
707 * @param int The key of the comment group to change the styles of
708 * @param string The style to make the comments
709 * @param boolean Whether to merge the new styles with the old or just
710 * to overwrite them
711 * @since 1.0.0
712 */
713 function set_comments_style ($key, $style, $preserve_defaults = false)
714 {
715 if (!$preserve_defaults) {
716 $this->language_data['STYLES']['COMMENTS'][$key] = $style;
717 } else {
718 $this->language_data['STYLES']['COMMENTS'][$key] .= $style;
719 }
720 }
721
722 /**
723 * Turns highlighting on/off for comment groups
724 *
725 * @param int The key of the comment group to turn on or off
726 * @param boolean Whether to turn highlighting for that group on or off
727 * @since 1.0.0
728 */
729 function set_comments_highlighting ($key, $flag = true)
730 {
731 $this->lexic_permissions['COMMENTS'][$key] = ($flag) ? true : false;
732 }
733
734 /**
735 * Sets the styles for escaped characters. If $preserve_defaults is
736 * true, then styles are merged with the default styles, with the
737 * user defined styles having priority
738 *
739 * @param string The style to make the escape characters
740 * @param boolean Whether to merge the new styles with the old or just
741 * to overwrite them
742 * @since 1.0.0
743 */
744 function set_escape_characters_style ($style, $preserve_defaults = false)
745 {
746 if (!$preserve_defaults) {
747 $this->language_data['STYLES']['ESCAPE_CHAR'][0] = $style;
748 } else {
749 $this->language_data['STYLES']['ESCAPE_CHAR'][0] .= $style;
750 }
751 }
752
753 /**
754 * Turns highlighting on/off for escaped characters
755 *
756 * @param boolean Whether to turn highlighting for escape characters on or off
757 * @since 1.0.0
758 */
759 function set_escape_characters_highlighting ($flag = true)
760 {
761 $this->lexic_permissions['ESCAPE_CHAR'] = ($flag) ? true : false;
762 }
763
764 /**
765 * Sets the styles for brackets. If $preserve_defaults is
766 * true, then styles are merged with the default styles, with the
767 * user defined styles having priority
768 *
769 * This method is DEPRECATED: use set_symbols_style instead.
770 * This method will be removed in 1.2.X
771 *
772 * @param string The style to make the brackets
773 * @param boolean Whether to merge the new styles with the old or just
774 * to overwrite them
775 * @since 1.0.0
776 * @deprecated In favour of set_symbols_style
777 */
778 function set_brackets_style ($style, $preserve_defaults = false)
779 {
780 if (!$preserve_defaults) {
781 $this->language_data['STYLES']['BRACKETS'][0] = $style;
782 } else {
783 $this->language_data['STYLES']['BRACKETS'][0] .= $style;
784 }
785 }
786
787 /**
788 * Turns highlighting on/off for brackets
789 *
790 * This method is DEPRECATED: use set_symbols_highlighting instead.
791 * This method will be remove in 1.2.X
792 *
793 * @param boolean Whether to turn highlighting for brackets on or off
794 * @since 1.0.0
795 * @deprecated In favour of set_symbols_highlighting
796 */
797 function set_brackets_highlighting ($flag)
798 {
799 $this->lexic_permissions['BRACKETS'] = ($flag) ? true : false;
800 }
801
802 /**
803 * Sets the styles for symbols. If $preserve_defaults is
804 * true, then styles are merged with the default styles, with the
805 * user defined styles having priority
806 *
807 * @param string The style to make the symbols
808 * @param boolean Whether to merge the new styles with the old or just
809 * to overwrite them
810 * @since 1.0.1
811 */
812 function set_symbols_style ($style, $preserve_defaults = false)
813 {
814 if (!$preserve_defaults) {
815 $this->language_data['STYLES']['SYMBOLS'][0] = $style;
816 } else {
817 $this->language_data['STYLES']['SYMBOLS'][0] .= $style;
818 }
819 // For backward compatibility
820 $this->set_brackets_style ($style, $preserve_defaults);
821 }
822
823 /**
824 * Turns highlighting on/off for symbols
825 *
826 * @param boolean Whether to turn highlighting for symbols on or off
827 * @since 1.0.0
828 */
829 function set_symbols_highlighting ($flag)
830 {
831 $this->lexic_permissions['SYMBOLS'] = ($flag) ? true : false;
832 // For backward compatibility
833 $this->set_brackets_highlighting ($flag);
834 }
835
836 /**
837 * Sets the styles for strings. If $preserve_defaults is
838 * true, then styles are merged with the default styles, with the
839 * user defined styles having priority
840 *
841 * @param string The style to make the escape characters
842 * @param boolean Whether to merge the new styles with the old or just
843 * to overwrite them
844 * @since 1.0.0
845 */
846 function set_strings_style ($style, $preserve_defaults = false)
847 {
848 if (!$preserve_defaults) {
849 $this->language_data['STYLES']['STRINGS'][0] = $style;
850 } else {
851 $this->language_data['STYLES']['STRINGS'][0] .= $style;
852 }
853 }
854
855 /**
856 * Turns highlighting on/off for strings
857 *
858 * @param boolean Whether to turn highlighting for strings on or off
859 * @since 1.0.0
860 */
861 function set_strings_highlighting ($flag)
862 {
863 $this->lexic_permissions['STRINGS'] = ($flag) ? true : false;
864 }
865
866 /**
867 * Sets the styles for numbers. If $preserve_defaults is
868 * true, then styles are merged with the default styles, with the
869 * user defined styles having priority
870 *
871 * @param string The style to make the numbers
872 * @param boolean Whether to merge the new styles with the old or just
873 * to overwrite them
874 * @since 1.0.0
875 */
876 function set_numbers_style ($style, $preserve_defaults = false)
877 {
878 if (!$preserve_defaults) {
879 $this->language_data['STYLES']['NUMBERS'][0] = $style;
880 } else {
881 $this->language_data['STYLES']['NUMBERS'][0] .= $style;
882 }
883 }
884
885 /**
886 * Turns highlighting on/off for numbers
887 *
888 * @param boolean Whether to turn highlighting for numbers on or off
889 * @since 1.0.0
890 */
891 function set_numbers_highlighting ($flag)
892 {
893 $this->lexic_permissions['NUMBERS'] = ($flag) ? true : false;
894 }
895
896 /**
897 * Sets the styles for methods. $key is a number that references the
898 * appropriate "object splitter" - see the language file for the language
899 * you are highlighting to get this number. If $preserve_defaults is
900 * true, then styles are merged with the default styles, with the
901 * user defined styles having priority
902 *
903 * @param int The key of the object splitter to change the styles of
904 * @param string The style to make the methods
905 * @param boolean Whether to merge the new styles with the old or just
906 * to overwrite them
907 * @since 1.0.0
908 */
909 function set_methods_style ($key, $style, $preserve_defaults = false)
910 {
911 if (!$preserve_defaults) {
912 $this->language_data['STYLES']['METHODS'][$key] = $style;
913 } else {
914 $this->language_data['STYLES']['METHODS'][$key] .= $style;
915 }
916 }
917
918 /**
919 * Turns highlighting on/off for methods
920 *
921 * @param boolean Whether to turn highlighting for methods on or off
922 * @since 1.0.0
923 */
924 function set_methods_highlighting ($flag)
925 {
926 $this->lexic_permissions['METHODS'] = ($flag) ? true : false;
927 }
928
929 /**
930 * Sets the styles for regexps. If $preserve_defaults is
931 * true, then styles are merged with the default styles, with the
932 * user defined styles having priority
933 *
934 * @param string The style to make the regular expression matches
935 * @param boolean Whether to merge the new styles with the old or just
936 * to overwrite them
937 * @since 1.0.0
938 */
939 function set_regexps_style ($key, $style, $preserve_defaults = false)
940 {
941 if (!$preserve_defaults) {
942 $this->language_data['STYLES']['REGEXPS'][$key] = $style;
943 } else {
944 $this->language_data['STYLES']['REGEXPS'][$key] .= $style;
945 }
946 }
947
948 /**
949 * Turns highlighting on/off for regexps
950 *
951 * @param int The key of the regular expression group to turn on or off
952 * @param boolean Whether to turn highlighting for the regular expression group on or off
953 * @since 1.0.0
954 */
955 function set_regexps_highlighting ($key, $flag)
956 {
957 $this->lexic_permissions['REGEXPS'][$key] = ($flag) ? true : false;
958 }
959
960 /**
961 * Sets whether a set of keywords are checked for in a case sensitive manner
962 *
963 * @param int The key of the keyword group to change the case sensitivity of
964 * @param boolean Whether to check in a case sensitive manner or not
965 * @since 1.0.0
966 */
967 function set_case_sensitivity ($key, $case)
968 {
969 $this->language_data['CASE_SENSITIVE'][$key] = ($case) ? true : false;
970 }
971
972 /**
973 * Sets the case that keywords should use when found. Use the constants:
974 *
975 * <ul>
976 * <li><b>GESHI_CAPS_NO_CHANGE</b>: leave keywords as-is</li>
977 * <li><b>GESHI_CAPS_UPPER</b>: convert all keywords to uppercase where found</li>
978 * <li><b>GESHI_CAPS_LOWER</b>: convert all keywords to lowercase where found</li>
979 * </ul>
980 *
981 * @param int A constant specifying what to do with matched keywords
982 * @since 1.0.1
983 * @todo Error check the passed value
984 */
985 function set_case_keywords ($case)
986 {
987 $this->language_data['CASE_KEYWORDS'] = $case;
988 }
989
990 /**
991 * Sets how many spaces a tab is substituted for
992 *
993 * Widths below zero are ignored
994 *
995 * @param int The tab width
996 * @since 1.0.0
997 */
998 function set_tab_width ($width)
999 {
1000 $this->tab_width = intval($width);
1001 }
1002
1003 /**
1004 * Enables/disables strict highlighting. Default is off, calling this
1005 * method without parameters will turn it on. See documentation
1006 * for more details on strict mode and where to use it.
1007 *
1008 * @param boolean Whether to enable strict mode or not
1009 * @since 1.0.0
1010 */
1011 function enable_strict_mode ($mode = true)
1012 {
1013 if (GESHI_MAYBE == $this->language_data['STRICT_MODE_APPLIES']) {
1014 $this->strict_mode = ($mode) ? true : false;
1015 }
1016 }
1017
1018 /**
1019 * Disables all highlighting
1020 *
1021 * @since 1.0.0
1022 * @todo Rewrite with an array traversal
1023 */
1024 function disable_highlighting ()
1025 {
1026 foreach ($this->lexic_permissions as $key => $value) {
1027 if (is_array($value)) {
1028 foreach ($value as $k => $v) {
1029 $this->lexic_permissions[$key][$k] = false;
1030 }
1031 } else {
1032 $this->lexic_permissions[$key] = false;
1033 }
1034 }
1035 // Context blocks
1036 $this->enable_important_blocks = false;
1037 }
1038
1039 /**
1040 * Enables all highlighting
1041 *
1042 * @since 1.0.0
1043 * @todo Rewrite with array traversal
1044 */
1045 function enable_highlighting ()
1046 {
1047 foreach ($this->lexic_permissions as $key => $value) {
1048 if (is_array($value)) {
1049 foreach ($value as $k => $v) {
1050 $this->lexic_permissions[$key][$k] = true;
1051 }
1052 } else {
1053 $this->lexic_permissions[$key] = true;
1054 }
1055 }
1056 // Context blocks
1057 $this->enable_important_blocks = true;
1058 }
1059
1060 /**
1061 * Given a file extension, this method returns either a valid geshi language
1062 * name, or the empty string if it couldn't be found
1063 *
1064 * @param string The extension to get a language name for
1065 * @param array A lookup array to use instead of the default
1066 * @since 1.0.5
1067 * @todo Re-think about how this method works (maybe make it private and/or make it
1068 * a extension->lang lookup?)
1069 * @todo static?
1070 */
1071 function get_language_name_from_extension ( $extension, $lookup = array() )
1072 {
1073 if ( !$lookup )
1074 {
1075 $lookup = array(
1076 'actionscript' => array('as'),
1077 'ada' => array('a', 'ada', 'adb', 'ads'),
1078 'apache' => array('conf'),
1079 'asm' => array('ash', 'asm'),
1080 'asp' => array('asp'),
1081 'bash' => array('sh'),
1082 'c' => array('c'),
1083 'c_mac' => array('c'),
1084 'caddcl' => array(),
1085 'cadlisp' => array(),
1086 'cdfg' => array('cdfg'),
1087 'cpp' => array('cpp'),
1088 'csharp' => array(),
1089 'css' => array('css'),
1090 'delphi' => array('dpk', 'dpr'),
1091 'html4strict' => array('html', 'htm'),
1092 'java' => array('java'),
1093 'javascript' => array('js'),
1094 'lisp' => array('lisp'),
1095 'lua' => array('lua'),
1096 'mpasm' => array(),
1097 'nsis' => array(),
1098 'objc' => array(),
1099 'oobas' => array(),
1100 'oracle8' => array(),
1101 'pascal' => array('pas'),
1102 'perl' => array('pl', 'pm'),
1103 'php' => array('php', 'php5', 'phtml', 'phps'),
1104 'python' => array('py'),
1105 'qbasic' => array('bi'),
1106 'sas' => array('sas'),
1107 'smarty' => array(),
1108 'vb' => array('bas'),
1109 'vbnet' => array(),
1110 'visualfoxpro' => array(),
1111 'xml' => array('xml')
1112 );
1113 }
1114
1115 foreach ($lookup as $lang => $extensions) {
1116 foreach ($extensions as $ext) {
1117 if ($ext == $extension) {
1118 return $lang;
1119 }
1120 }
1121 }
1122 return '';
1123 }
1124
1125 /**
1126 * Given a file name, this method loads its contents in, and attempts
1127 * to set the language automatically. An optional lookup table can be
1128 * passed for looking up the language name. If not specified a default
1129 * table is used
1130 *
1131 * The language table is in the form
1132 * <pre>array(
1133 * 'lang_name' => array('extension', 'extension', ...),
1134 * 'lang_name' ...
1135 * );</pre>
1136 *
1137 * @todo Complete rethink of this and above method
1138 * @since 1.0.5
1139 */
1140 function load_from_file ($file_name, $lookup = array())
1141 {
1142 if (is_readable($file_name)) {
1143 $this->set_source(implode('', file($file_name)));
1144 $this->set_language($this->get_language_name_from_extension(substr(strrchr($file_name, '.'), 1), $lookup));
1145 } else {
1146 $this->error = GESHI_ERROR_FILE_NOT_READABLE;
1147 }
1148 }
1149
1150 /**
1151 * Adds a keyword to a keyword group for highlighting
1152 *
1153 * @param int The key of the keyword group to add the keyword to
1154 * @param string The word to add to the keyword group
1155 * @since 1.0.0
1156 */
1157 function add_keyword ($key, $word)
1158 {
1159 $this->language_data['KEYWORDS'][$key][] = $word;
1160 }
1161
1162 /**
1163 * Removes a keyword from a keyword group
1164 *
1165 * @param int The key of the keyword group to remove the keyword from
1166 * @param string The word to remove from the keyword group
1167 * @since 1.0.0
1168 */
1169 function remove_keyword ($key, $word)
1170 {
1171 $this->language_data['KEYWORDS'][$key] =
1172 array_diff($this->language_data['KEYWORDS'][$key], array($word));
1173 }
1174
1175 /**
1176 * Creates a new keyword group
1177 *
1178 * @param int The key of the keyword group to create
1179 * @param string The styles for the keyword group
1180 * @param boolean Whether the keyword group is case sensitive ornot
1181 * @param array The words to use for the keyword group
1182 * @since 1.0.0
1183 */
1184 function add_keyword_group ( $key, $styles, $case_sensitive = true, $words = array() )
1185 {
1186 $words = (array) $words;
1187 $this->language_data['KEYWORDS'][$key] = $words;
1188 $this->lexic_permissions['KEYWORDS'][$key] = true;
1189 $this->language_data['CASE_SENSITIVE'][$key] = $case_sensitive;
1190 $this->language_data['STYLES']['KEYWORDS'][$key] = $styles;
1191 }
1192
1193 /**
1194 * Removes a keyword group
1195 *
1196 * @param int The key of the keyword group to remove
1197 * @since 1.0.0
1198 */
1199 function remove_keyword_group ($key)
1200 {
1201 unset($this->language_data['KEYWORDS'][$key]);
1202 unset($this->lexic_permissions['KEYWORDS'][$key]);
1203 unset($this->language_data['CASE_SENSITIVE'][$key]);
1204 unset($this->language_data['STYLES']['KEYWORDS'][$key]);
1205 }
1206
1207 /**
1208 * Sets the content of the header block
1209 *
1210 * @param string The content of the header block
1211 * @since 1.0.2
1212 */
1213 function set_header_content ($content)
1214 {
1215 $this->header_content = $content;
1216 }
1217
1218 /**
1219 * Sets the content of the footer block
1220 *
1221 * @param string The content of the footer block
1222 * @since 1.0.2
1223 */
1224 function set_footer_content ($content)
1225 {
1226 $this->footer_content = $content;
1227 }
1228
1229 /**
1230 * Sets the style for the header content
1231 *
1232 * @param string The style for the header content
1233 * @since 1.0.2
1234 */
1235 function set_header_content_style ($style)
1236 {
1237 $this->header_content_style = $style;
1238 }
1239
1240 /**
1241 * Sets the style for the footer content
1242 *
1243 * @param string The style for the footer content
1244 * @since 1.0.2
1245 */
1246 function set_footer_content_style ($style)
1247 {
1248 $this->footer_content_style = $style;
1249 }
1250
1251 /**
1252 * Sets the base URL to be used for keywords
1253 *
1254 * @param int The key of the keyword group to set the URL for
1255 * @param string The URL to set for the group. If {FNAME} is in
1256 * the url somewhere, it is replaced by the keyword
1257 * that the URL is being made for
1258 * @since 1.0.2
1259 */
1260 function set_url_for_keyword_group ($group, $url)
1261 {
1262 $this->language_data['URLS'][$group] = $url;
1263 }
1264
1265 /**
1266 * Sets styles for links in code
1267 *
1268 * @param int A constant that specifies what state the style is being
1269 * set for - e.g. :hover or :visited
1270 * @param string The styles to use for that state
1271 * @since 1.0.2
1272 */
1273 function set_link_styles ($type, $styles)
1274 {
1275 $this->link_styles[$type] = $styles;
1276 }
1277
1278 /**
1279 * Sets the target for links in code
1280 *
1281 * @param string The target for links in the code, e.g. _blank
1282 * @since 1.0.3
1283 */
1284 function set_link_target ( $target )
1285 {
1286 if (!$target) {
1287 $this->link_target = '';
1288 } else {
1289 $this->link_target = ' target="' . $target . '" ';
1290 }
1291 }
1292
1293 /**
1294 * Sets styles for important parts of the code
1295 *
1296 * @param string The styles to use on important parts of the code
1297 * @since 1.0.2
1298 */
1299 function set_important_styles ($styles)
1300 {
1301 $this->important_styles = $styles;
1302 }
1303
1304 /**
1305 * Sets whether context-important blocks are highlighted
1306 *
1307 * @todo REMOVE THIS SHIZ FROM GESHI!
1308 * @deprecated
1309 */
1310 function enable_important_blocks ( $flag )
1311 {
1312 $this->enable_important_blocks = ( $flag ) ? true : false;
1313 }
1314
1315 /**
1316 * Whether CSS IDs should be added to each line
1317 *
1318 * @param boolean If true, IDs will be added to each line.
1319 * @since 1.0.2
1320 */
1321 function enable_ids ($flag = true)
1322 {
1323 $this->add_ids = ($flag) ? true : false;
1324 }
1325
1326 /**
1327 * Specifies which lines to highlight extra
1328 *
1329 * @param mixed An array of line numbers to highlight, or just a line
1330 * number on its own.
1331 * @since 1.0.2
1332 * @todo Some data replication here that could be cut down on
1333 */
1334 function highlight_lines_extra ($lines)
1335 {
1336 if (is_array($lines)) {
1337 foreach ($lines as $line) {
1338 $this->highlight_extra_lines[intval($line)] = intval($line);
1339 }
1340 } else {
1341 $this->highlight_extra_lines[intval($lines)] = intval($lines);
1342 }
1343 }
1344
1345 /**
1346 * Sets the style for extra-highlighted lines
1347 *
1348 * @param string The style for extra-highlighted lines
1349 * @since 1.0.2
1350 */
1351 function set_highlight_lines_extra_style ($styles)
1352 {
1353 $this->highlight_extra_lines_style = $styles;
1354 }
1355
1356 /**
1357 * Sets what number line numbers should start at. Should
1358 * be a positive integer, and will be converted to one.
1359 *
1360 * <b>Warning:</b> Using this method will add the "start"
1361 * attribute to the &lt;ol&gt; that is used for line numbering.
1362 * This is <b>not</b> valid XHTML strict, so if that's what you
1363 * care about then don't use this method. Firefox is getting
1364 * support for the CSS method of doing this in 1.1 and Opera
1365 * has support for the CSS method, but (of course) IE doesn't
1366 * so it's not worth doing it the CSS way yet.
1367 *
1368 * @param int The number to start line numbers at
1369 * @since 1.0.2
1370 */
1371 function start_line_numbers_at ($number)
1372 {
1373 $this->line_numbers_start = abs(intval($number));
1374 }
1375
1376 /**
1377 * Sets the encoding used for htmlspecialchars(), for international
1378 * support.
1379 *
1380 * @param string The encoding to use for the source
1381 * @since 1.0.3
1382 */
1383 function set_encoding ($encoding)
1384 {
1385 if ($encoding) {
1386 $this->encoding = $encoding;
1387 }
1388 }
1389
1390 /**
1391 * Returns the code in $this->source, highlighted and surrounded by the
1392 * nessecary HTML.
1393 *
1394 * This should only be called ONCE, cos it's SLOW! If you want to highlight
1395 * the same source multiple times, you're better off doing a whole lot of
1396 * str_replaces to replace the &lt;span&gt;s
1397 *
1398 * @since 1.0.0
1399 */
1400 function parse_code ()
1401 {
1402 // Start the timer
1403 $start_time = microtime();
1404
1405 // Firstly, if there is an error, we won't highlight
1406 if ($this->error) {
1407 $result = @htmlspecialchars($this->source, ENT_COMPAT, $this->encoding);
1408 // Timing is irrelevant
1409 $this->set_time($start_time, $start_time);
1410 return $this->finalise($result);
1411 }
1412
1413 // Add spaces for regular expression matching and line numbers
1414 $code = ' ' . $this->source . ' ';
1415 // Replace all newlines to a common form.
1416 $code = str_replace("\r\n", "\n", $code);
1417 $code = str_replace("\r", "\n", $code);
1418
1419 // Initialise various stuff
1420 $length = strlen($code);
1421 $STRING_OPEN = '';
1422 $CLOSE_STRING = false;
1423 $ESCAPE_CHAR_OPEN = false;
1424 $COMMENT_MATCHED = false;
1425 // Turn highlighting on if strict mode doesn't apply to this language
1426 $HIGHLIGHTING_ON = ( !$this->strict_mode ) ? true : '';
1427 // Whether to highlight inside a block of code
1428 $HIGHLIGHT_INSIDE_STRICT = false;
1429 $HARDQUOTE_OPEN = false;
1430 $stuff_to_parse = '';
1431 $result = '';
1432
1433 // "Important" selections are handled like multiline comments
1434 // @todo GET RID OF THIS SHIZ
1435 if ($this->enable_important_blocks) {
1436 $this->language_data['COMMENT_MULTI'][GESHI_START_IMPORTANT] = GESHI_END_IMPORTANT;
1437 }
1438
1439 if ($this->strict_mode) {
1440 // Break the source into bits. Each bit will be a portion of the code
1441 // within script delimiters - for example, HTML between < and >
1442 $parts = array(0 => array(0 => ''));
1443 $k = 0;
1444 for ($i = 0; $i < $length; $i++) {
1445 $char = substr($code, $i, 1);
1446 if (!$HIGHLIGHTING_ON) {
1447 foreach ($this->language_data['SCRIPT_DELIMITERS'] as $key => $delimiters) {
1448 foreach ($delimiters as $open => $close) {
1449 // Get the next little bit for this opening string
1450 $check = substr($code, $i, strlen($open));
1451 // If it matches...
1452 if ($check == $open) {
1453 // We start a new block with the highlightable
1454 // code in it
1455 $HIGHLIGHTING_ON = $open;
1456 $i += strlen($open) - 1;
1457 $char = $open;
1458 $parts[++$k][0] = $char;
1459
1460 // No point going around again...
1461 break(2);
1462 }
1463 }
1464 }
1465 } else {
1466 foreach ($this->language_data['SCRIPT_DELIMITERS'] as $key => $delimiters) {
1467 foreach ($delimiters as $open => $close) {
1468 if ($open == $HIGHLIGHTING_ON) {
1469 // Found the closing tag
1470 break(2);
1471 }
1472 }
1473 }
1474 // We check code from our current position BACKWARDS. This is so
1475 // the ending string for highlighting can be included in the block
1476 $check = substr($code, $i - strlen($close) + 1, strlen($close));
1477 if ($check == $close) {
1478 $HIGHLIGHTING_ON = '';
1479 // Add the string to the rest of the string for this part
1480 $parts[$k][1] = ( isset($parts[$k][1]) ) ? $parts[$k][1] . $char : $char;
1481 $parts[++$k][0] = '';
1482 $char = '';
1483 }
1484 }
1485 $parts[$k][1] = ( isset($parts[$k][1]) ) ? $parts[$k][1] . $char : $char;
1486 }
1487 $HIGHLIGHTING_ON = '';
1488 } else {
1489 // Not strict mode - simply dump the source into
1490 // the array at index 1 (the first highlightable block)
1491 $parts = array(
1492 1 => array(
1493 0 => '',
1494 1 => $code
1495 )
1496 );
1497 }
1498
1499 // Now we go through each part. We know that even-indexed parts are
1500 // code that shouldn't be highlighted, and odd-indexed parts should
1501 // be highlighted
1502 foreach ($parts as $key => $data) {
1503 $part = $data[1];
1504 // If this block should be highlighted...
1505 if ($key % 2) {
1506 if ($this->strict_mode) {
1507 // Find the class key for this block of code
1508 foreach ($this->language_data['SCRIPT_DELIMITERS'] as $script_key => $script_data) {
1509 foreach ($script_data as $open => $close) {
1510 if ($data[0] == $open) {
1511 break(2);
1512 }
1513 }
1514 }
1515
1516 if ($this->language_data['STYLES']['SCRIPT'][$script_key] != '' &&
1517 $this->lexic_permissions['SCRIPT']) {
1518 // Add a span element around the source to
1519 // highlight the overall source block
1520 if (!$this->use_classes &&
1521 $this->language_data['STYLES']['SCRIPT'][$script_key] != '') {
1522 $attributes = ' style="' . $this->language_data['STYLES']['SCRIPT'][$script_key] . '"';
1523 } else {
1524 $attributes = ' class="sc' . $script_key . '"';
1525 }
1526 $result .= "<span$attributes>";
1527 }
1528 }
1529
1530 if (!$this->strict_mode || $this->language_data['HIGHLIGHT_STRICT_BLOCK'][$script_key]) {
1531 // Now, highlight the code in this block. This code
1532 // is really the engine of GeSHi (along with the method
1533 // parse_non_string_part).
1534 $length = strlen($part);
1535 for ($i = 0; $i < $length; $i++) {
1536 // Get the next char
1537 $char = substr($part, $i, 1);
1538 $hq = isset($this->language_data['HARDQUOTE']) ? $this->language_data['HARDQUOTE'][0] : false;
1539 // Is this char the newline and line numbers being used?
1540 if (($this->line_numbers != GESHI_NO_LINE_NUMBERS
1541 || count($this->highlight_extra_lines) > 0)
1542 && $char == "\n") {
1543 // If so, is there a string open? If there is, we should end it before
1544 // the newline and begin it again (so when <li>s are put in the source
1545 // remains XHTML compliant)
1546 // note to self: This opens up possibility of config files specifying
1547 // that languages can/cannot have multiline strings???
1548 if ($STRING_OPEN) {
1549 if (!$this->use_classes) {
1550 $attributes = ' style="' . $this->language_data['STYLES']['STRINGS'][0] . '"';
1551 } else {
1552 $attributes = ' class="st0"';
1553 }
1554 $char = '</span>' . $char . "<span$attributes>";
1555 }
1556 } elseif ($char == $STRING_OPEN) {
1557 // A match of a string delimiter
1558 if (($this->lexic_permissions['ESCAPE_CHAR'] && $ESCAPE_CHAR_OPEN) ||
1559 ($this->lexic_permissions['STRINGS'] && !$ESCAPE_CHAR_OPEN)) {
1560 $char = htmlspecialchars($char, ENT_COMPAT, $this->encoding) . '</span>';
1561 }
1562 $escape_me = false;
1563 if ($HARDQUOTE_OPEN)
1564 {
1565 if ($ESCAPE_CHAR_OPEN)
1566 $escape_me = true;
1567 else {
1568 foreach ($this->language_data['HARDESCAPE'] as $hardesc)
1569 if (substr($part, $i, strlen($hardesc)) == $hardesc)
1570 {
1571 $escape_me = true;
1572 break;
1573 }
1574 }
1575 }
1576 if (!$ESCAPE_CHAR_OPEN) {
1577 $STRING_OPEN = '';
1578 $CLOSE_STRING = true;
1579 }
1580 if (!$escape_me) {
1581 $HARDQUOTE_OPEN = false;
1582 }
1583 $ESCAPE_CHAR_OPEN = false;
1584 } elseif (in_array($char, $this->language_data['QUOTEMARKS']) &&
1585 ($STRING_OPEN == '') && $this->lexic_permissions['STRINGS']) {
1586 // The start of a new string
1587 $STRING_OPEN = $char;
1588 if (!$this->use_classes) {
1589 $attributes = ' style="' . $this->language_data['STYLES']['STRINGS'][0] . '"';
1590 } else {
1591 $attributes = ' class="st0"';
1592 }
1593 $char = "<span$attributes>" . htmlspecialchars($char, ENT_COMPAT, $this->encoding);
1594
1595 $result .= $this->parse_non_string_part( $stuff_to_parse );
1596 $stuff_to_parse = '';
1597 } elseif (
1598 $hq &&
1599 substr($part, $i, strlen($hq)) == $hq &&
1600 ($STRING_OPEN == '') && $this->lexic_permissions['STRINGS']
1601 )
1602 {
1603 // The start of a hard quoted string
1604 $STRING_OPEN = $this->language_data['HARDQUOTE'][1];
1605 if (!$this->use_classes) {
1606 $attributes = ' style="' . $this->language_data['STYLES']['STRINGS'][0] . '"';
1607 } else {
1608 $attributes = ' class="st0"';
1609 }
1610 $char = "<span$attributes>" . $hq;
1611 $i += strlen($hq) - 1;
1612 $HARDQUOTE_OPEN = true;
1613 $result .= $this->parse_non_string_part( $stuff_to_parse );
1614 $stuff_to_parse = '';
1615 } elseif ($char == $this->language_data['ESCAPE_CHAR'] && $STRING_OPEN != '')
1616 {
1617 // An escape character
1618 if (!$ESCAPE_CHAR_OPEN) {
1619 $ESCAPE_CHAR_OPEN = !$HARDQUOTE_OPEN; // true unless $HARDQUOTE_OPEN
1620 if ($HARDQUOTE_OPEN)
1621 foreach ($this->language_data['HARDESCAPE'] as $hard)
1622 {
1623 if (substr($part, $i, strlen($hard)) == $hard)
1624 {
1625 $ESCAPE_CHAR_OPEN = true;
1626 break;
1627 }
1628 }
1629 if ($ESCAPE_CHAR_OPEN && $this->lexic_permissions['ESCAPE_CHAR']) {
1630 if (!$this->use_classes) {
1631 $attributes = ' style="' . $this->language_data['STYLES']['ESCAPE_CHAR'][0] . '"';
1632 } else {
1633 $attributes = ' class="es0"';
1634 }
1635 $char = "<span$attributes>" . $char;
1636 if (substr($code, $i + 1, 1) == "\n") {
1637 // escaping a newline, what's the point in putting the span around
1638 // the newline? It only causes hassles when inserting line numbers
1639 $char .= '</span>';
1640 $ESCAPE_CHAR_OPEN = false;
1641 }
1642 }
1643 } else {
1644 $ESCAPE_CHAR_OPEN = false;
1645 if ($this->lexic_permissions['ESCAPE_CHAR']) {
1646 $char .= '</span>';
1647 }
1648 }
1649 } elseif ($ESCAPE_CHAR_OPEN) {
1650 if ($this->lexic_permissions['ESCAPE_CHAR']) {
1651 $char .= '</span>';
1652 }
1653 $ESCAPE_CHAR_OPEN = false;
1654 $test_str = $char;
1655 } elseif ($STRING_OPEN == '') {
1656 // Is this a multiline comment?
1657 foreach ($this->language_data['COMMENT_MULTI'] as $open => $close) {
1658 $com_len = strlen($open);
1659 $test_str = substr( $part, $i, $com_len );
1660 $test_str_match = $test_str;
1661 if ($open == $test_str) {
1662 $COMMENT_MATCHED = true;
1663 //@todo If remove important do remove here
1664 if ($this->lexic_permissions['COMMENTS']['MULTI'] ||
1665 $test_str == GESHI_START_IMPORTANT) {
1666 if ($test_str != GESHI_START_IMPORTANT) {
1667 if (!$this->use_classes) {
1668 $attributes = ' style="' . $this->language_data['STYLES']['COMMENTS']['MULTI'] . '"';
1669 } else {
1670 $attributes = ' class="coMULTI"';
1671 }
1672 $test_str = "<span$attributes>" . @htmlspecialchars($test_str, ENT_COMPAT, $this->encoding);
1673 } else {
1674 if (!$this->use_classes) {
1675 $attributes = ' style="' . $this->important_styles . '"';
1676 } else {
1677 $attributes = ' class="imp"';
1678 }
1679 // We don't include the start of the comment if it's an
1680 // "important" part
1681 $test_str = "<span$attributes>";
1682 }
1683 } else {
1684 $test_str = @htmlspecialchars($test_str, ENT_COMPAT, $this->encoding);
1685 }
1686
1687 $close_pos = strpos( $part, $close, $i + strlen($close) );
1688
1689 if ($close_pos === false) {
1690 $close_pos = strlen($part);
1691 }
1692
1693 // Short-cut through all the multiline code
1694 $rest_of_comment = @htmlspecialchars(substr($part, $i + $com_len, $close_pos - $i), ENT_COMPAT, $this->encoding);
1695 if (($this->lexic_permissions['COMMENTS']['MULTI'] ||
1696 $test_str_match == GESHI_START_IMPORTANT) &&
1697 ($this->line_numbers != GESHI_NO_LINE_NUMBERS ||
1698 count($this->highlight_extra_lines) > 0)) {
1699 // strreplace to put close span and open span around multiline newlines
1700 $test_str .= str_replace("\n", "</span>\n<span$attributes>", $rest_of_comment);
1701 } else {
1702 $test_str .= $rest_of_comment;
1703 }
1704
1705 if ($this->lexic_permissions['COMMENTS']['MULTI'] ||
1706 $test_str_match == GESHI_START_IMPORTANT) {
1707 $test_str .= '</span>';
1708 }
1709 $i = $close_pos + $com_len - 1;
1710 // parse the rest
1711 $result .= $this->parse_non_string_part($stuff_to_parse);
1712 $stuff_to_parse = '';
1713 break;
1714 }
1715 }
1716 // If we haven't matched a multiline comment, try single-line comments
1717 if (!$COMMENT_MATCHED) {
1718 foreach ($this->language_data['COMMENT_SINGLE'] as $comment_key => $comment_mark) {
1719 $com_len = strlen($comment_mark);
1720 $test_str = substr($part, $i, $com_len);
1721 if ($this->language_data['CASE_SENSITIVE'][GESHI_COMMENTS]) {
1722 $match = ($comment_mark == $test_str);
1723 } else {
1724 $match = (strtolower($comment_mark) == strtolower($test_str));
1725 }
1726 if ($match) {
1727 $COMMENT_MATCHED = true;
1728 if ($this->lexic_permissions['COMMENTS'][$comment_key]) {
1729 if (!$this->use_classes) {
1730 $attributes = ' style="' . $this->language_data['STYLES']['COMMENTS'][$comment_key] . '"';
1731 } else {
1732 $attributes = ' class="co' . $comment_key . '"';
1733 }
1734 $test_str = "<span$attributes>" . @htmlspecialchars($this->change_case($test_str), ENT_COMPAT, $this->encoding);
1735 } else {
1736 $test_str = @htmlspecialchars($test_str, ENT_COMPAT, $this->encoding);
1737 }
1738 $close_pos = strpos($part, "\n", $i);
1739 $oops = false;
1740 if ($close_pos === false) {
1741 $close_pos = strlen($part);
1742 $oops = true;
1743 }
1744 $test_str .= @htmlspecialchars(substr($part, $i + $com_len, $close_pos - $i - $com_len), ENT_COMPAT, $this->encoding);
1745 if ($this->lexic_permissions['COMMENTS'][$comment_key]) {
1746 $test_str .= "</span>";
1747 }
1748 // Take into account that the comment might be the last in the source
1749 if (!$oops) {
1750 $test_str .= "\n";
1751 }
1752 $i = $close_pos;
1753 // parse the rest
1754 $result .= $this->parse_non_string_part($stuff_to_parse);
1755 $stuff_to_parse = '';
1756 break;
1757 }
1758 }
1759 }
1760 } elseif ($STRING_OPEN != '') {
1761 // Otherwise, convert it to HTML form
1762 if (strtolower($this->encoding) == 'utf-8') {
1763 //only escape <128 (we don't want to break multibyte chars)
1764 if (ord($char) < 128) {
1765 $char = @htmlspecialchars($char, ENT_COMPAT, $this->encoding);
1766 }
1767 } else {
1768 //encode everthing
1769 $char = @htmlspecialchars($char, ENT_COMPAT, $this->encoding);
1770 }
1771 }
1772 // Where are we adding this char?
1773 if (!$COMMENT_MATCHED) {
1774 if (($STRING_OPEN == '') && !$CLOSE_STRING) {
1775 $stuff_to_parse .= $char;
1776 } else {
1777 $result .= $char;
1778 $CLOSE_STRING = false;
1779 }
1780 } else {
1781 $result .= $test_str;
1782 $COMMENT_MATCHED = false;
1783 }
1784 }
1785 // Parse the last bit
1786 $result .= $this->parse_non_string_part($stuff_to_parse);
1787 $stuff_to_parse = '';
1788 } else {
1789 $result .= @htmlspecialchars($part, ENT_COMPAT, $this->encoding);
1790 }
1791 // Close the <span> that surrounds the block
1792 if ($this->strict_mode && $this->language_data['STYLES']['SCRIPT'][$script_key] != '' &&
1793 $this->lexic_permissions['SCRIPT']) {
1794 $result .= '</span>';
1795 }
1796 } else {
1797 // Else not a block to highlight
1798 $result .= @htmlspecialchars($part, ENT_COMPAT, $this->encoding);
1799 }
1800 }
1801
1802 // Parse the last stuff (redundant?)
1803 $result .= $this->parse_non_string_part($stuff_to_parse);
1804
1805 // Lop off the very first and last spaces
1806 $result = substr($result, 1, strlen($result) - 1);
1807
1808 // Are we still in a string?
1809 if ($STRING_OPEN) {
1810 $result .= '</span>';
1811 }
1812
1813 // We're finished: stop timing
1814 $this->set_time($start_time, microtime());
1815
1816 return $this->finalise($result);
1817 }
1818
1819 /**
1820 * Swaps out spaces and tabs for HTML indentation. Not needed if
1821 * the code is in a pre block...
1822 *
1823 * @param string The source to indent
1824 * @return string The source with HTML indenting applied
1825 * @since 1.0.0
1826 * @access private
1827 */
1828 function indent ($result)
1829 {
1830 /// Replace tabs with the correct number of spaces
1831 if (false !== strpos($result, "\t")) {
1832 $lines = explode("\n", $result);
1833 foreach ($lines as $key => $line) {
1834 if (false === strpos($line, "\t")) {
1835 $lines[$key] = $line;
1836 continue;
1837 }
1838
1839 $pos = 0;
1840 $tab_width = $this->tab_width;
1841 $length = strlen($line);
1842 $result_line = '';
1843
1844 $IN_TAG = false;
1845 for ($i = 0; $i < $length; $i++) {
1846 $char = substr($line, $i, 1);
1847 // Simple engine to work out whether we're in a tag.
1848 // If we are we modify $pos. This is so we ignore HTML
1849 // in the line and only workout the tab replacement
1850 // via the actual content of the string
1851 // This test could be improved to include strings in the
1852 // html so that < or > would be allowed in user's styles
1853 // (e.g. quotes: '<' '>'; or similar)
1854 if ($IN_TAG && '>' == $char) {
1855 $IN_TAG = false;
1856 $result_line .= '>';
1857 ++$pos;
1858 } elseif (!$IN_TAG && '<' == $char) {
1859 $IN_TAG = true;
1860 $result_line .= '<';
1861 ++$pos;
1862 } elseif (!$IN_TAG && '&' == $char) { $substr = substr($line, $i + 3, 4);
1863 //$substr_5 = substr($line, 5, 1);
1864 $posi = strpos($substr, ';');
1865 if (false !== $posi) {
1866 $pos += $posi + 3;
1867 }
1868 $result_line .= '&';
1869 } elseif (!$IN_TAG && "\t" == $char) {
1870 $str = '';
1871 // OPTIMISE - move $strs out. Make an array:
1872 // $tabs = array(
1873 // 1 => '&nbsp;',
1874 // 2 => '&nbsp; ',
1875 // 3 => '&nbsp; &nbsp;' etc etc
1876 // to use instead of building a string every time
1877 $strs = array(0 => '&nbsp;', 1 => ' ');
1878 for ($k = 0; $k < ($tab_width - (($i - $pos) % $tab_width)); $k++) $str .= $strs[$k % 2];
1879 $result_line .= $str;
1880 //$pos--;
1881 $pos++;
1882 //$pos -= $tab_width-1;
1883
1884 if (false === strpos($line, "\t", $i + 1)) {
1885 //$lines[$key] = $result_line;
1886 $result_line .= substr($line, $i + 1);
1887 break;
1888 }
1889 } elseif ( $IN_TAG ) {
1890 ++$pos;
1891 $result_line .= $char;
1892 } else {
1893 $result_line .= $char;
1894 //++$pos;
1895 }
1896 }
1897 $lines[$key] = $result_line;
1898 }
1899 $result = implode("\n", $lines);
1900 }
1901 // Other whitespace
1902 $result = str_replace(' ', '&nbsp; ', $result);
1903 $result = str_replace(' ', ' &nbsp;', $result);
1904 $result = str_replace("\n ", "\n&nbsp;", $result);
1905
1906 if ($this->line_numbers == GESHI_NO_LINE_NUMBERS) {
1907 $result = nl2br($result);
1908 }
1909 return $result;
1910 }
1911
1912 /**
1913 * Changes the case of a keyword for those languages where a change is asked for
1914 *
1915 * @param string The keyword to change the case of
1916 * @return string The keyword with its case changed
1917 * @since 1.0.0
1918 * @access private
1919 */
1920 function change_case ($instr)
1921 {
1922 if ($this->language_data['CASE_KEYWORDS'] == GESHI_CAPS_UPPER) {
1923 return strtoupper($instr);
1924 } elseif ($this->language_data['CASE_KEYWORDS'] == GESHI_CAPS_LOWER) {
1925 return strtolower($instr);
1926 }
1927 return $instr;
1928 }
1929
1930 /**
1931 * Adds a url to a keyword where needed.
1932 *
1933 * @param string The keyword to add the URL HTML to
1934 * @param int What group the keyword is from
1935 * @param boolean Whether to get the HTML for the start or end
1936 * @return The HTML for either the start or end of the HTML &lt;a&gt; tag
1937 * @since 1.0.2
1938 * @access private
1939 * @todo Get rid of ender
1940 */
1941 function add_url_to_keyword ($keyword, $group, $start_or_end)
1942 {
1943 if (isset($this->language_data['URLS'][$group]) &&
1944 $this->language_data['URLS'][$group] != '' &&
1945 substr($keyword, 0, 5) != '&lt;/') {
1946 // There is a base group for this keyword
1947 if ($start_or_end == 'BEGIN') {
1948 // HTML workaround... not good form (tm) but should work for 1.0.X
1949 if ($keyword != '') {
1950 // Old system: strtolower
1951 //$keyword = ( $this->language_data['CASE_SENSITIVE'][$group] ) ? $keyword : strtolower($keyword);
1952 // New system: get keyword from language file to get correct case
1953 foreach ($this->language_data['KEYWORDS'][$group] as $word) {
1954 if (strtolower($word) == strtolower($keyword)) {
1955 break;
1956 }
1957 }
1958 $word = ( substr($word, 0, 4) == '&lt;' ) ? substr($word, 4) : $word;
1959 $word = ( substr($word, -4) == '&gt;' ) ? substr($word, 0, strlen($word) - 4) : $word;
1960 if (!$word) return '';
1961
1962 return '<|UR1|"' .
1963 str_replace(
1964 array('{FNAME}', '.'),
1965 array(@htmlspecialchars($word, ENT_COMPAT, $this->encoding), '<DOT>'),
1966 $this->language_data['URLS'][$group]
1967 ) . '">';
1968 }
1969 return '';
1970 // HTML fix. Again, dirty hackage...
1971 } elseif (!($this->language == 'html4strict' && '&gt;' == $keyword)) {
1972 return '</a>';
1973 }
1974 }
1975 }
1976
1977 /**
1978 * Takes a string that has no strings or comments in it, and highlights
1979 * stuff like keywords, numbers and methods.
1980 *
1981 * @param string The string to parse for keyword, numbers etc.
1982 * @since 1.0.0
1983 * @access private
1984 * @todo BUGGY! Why? Why not build string and return?
1985 */
1986 function parse_non_string_part (&$stuff_to_parse)
1987 {
1988 $stuff_to_parse = ' ' . @htmlspecialchars($stuff_to_parse, ENT_COMPAT, $this->encoding);
1989 $stuff_to_parse_pregquote = preg_quote($stuff_to_parse, '/');
1990 $func = '$this->change_case';
1991 $func2 = '$this->add_url_to_keyword';
1992
1993 //
1994 // Regular expressions
1995 //
1996 foreach ($this->language_data['REGEXPS'] as $key => $regexp) {
1997 if ($this->lexic_permissions['REGEXPS'][$key]) {
1998 if (is_array($regexp)) {
1999 $stuff_to_parse = preg_replace(
2000 "/" .
2001 str_replace('/', '\/', $regexp[GESHI_SEARCH]) .
2002 "/{$regexp[GESHI_MODIFIERS]}",
2003 "{$regexp[GESHI_BEFORE]}<|!REG3XP$key!>{$regexp[GESHI_REPLACE]}|>{$regexp[GESHI_AFTER]}",
2004 $stuff_to_parse
2005 );
2006 } else {
2007 $stuff_to_parse = preg_replace( "/(" . str_replace('/', '\/', $regexp) . ")/", "<|!REG3XP$key!>\\1|>", $stuff_to_parse);
2008 }
2009 }
2010 }
2011
2012 //
2013 // Highlight numbers. This regexp sucks... anyone with a regexp that WORKS
2014 // here wins a cookie if they send it to me. At the moment there's two doing
2015 // almost exactly the same thing, except the second one prevents a number
2016 // being highlighted twice (eg <span...><span...>5</span></span>)
2017 // Put /NUM!/ in for the styles, which gets replaced at the end.
2018 //
2019 // NEW ONE: Brice Bernard
2020 // $stuff_to_parse = preg_replace('/([^(\\w|#|\\\|"|\')])(\\d+)/', '\\1<|/NUM!/>\\2|>', $stuff_to_parse);
2021 //$stuff_to_parse = preg_replace('/([-+]?\\b(?:[0-9]*\\.)?[0-9]+\\b)/', '<|/NUM!/>\\1|>', $stuff_to_parse);
2022 //
2023 if ($this->lexic_permissions['NUMBERS'] && preg_match('#[0-9]#', $stuff_to_parse )) {
2024 //$stuff_to_parse = preg_replace('#([^a-zA-Z0-9_\#])([0-9]+)([^a-zA-Z0-9])#', "\\1<|/NUM!/>\\2|>\\3", $stuff_to_parse);
2025 //$stuff_to_parse = preg_replace('#([^a-zA-Z0-9_\#>])([0-9]+)([^a-zA-Z0-9])#', "\\1<|/NUM!/>\\2|>\\3", $stuff_to_parse);
2026 $stuff_to_parse = preg_replace('/([-+]?\\b(?:[0-9]*\\.)?[0-9]+\\b)/', '<|/NUM!/>\\1|>', $stuff_to_parse);
2027 }
2028
2029 // Highlight keywords
2030 // if there is a couple of alpha symbols there *might* be a keyword
2031 if (preg_match('#[a-zA-Z]{2,}#', $stuff_to_parse)) {
2032 foreach ($this->language_data['KEYWORDS'] as $k => $keywordset) {
2033 if ($this->lexic_permissions['KEYWORDS'][$k]) {
2034 foreach ($keywordset as $keyword) {
2035 $keyword = preg_quote($keyword, '/');
2036 //
2037 // This replacement checks the word is on it's own (except if brackets etc
2038 // are next to it), then highlights it. We don't put the color=" for the span
2039 // in just yet - otherwise languages with the keywords "color" or "or" have
2040 // a fit.
2041 //
2042 if (false !== stristr($stuff_to_parse_pregquote, $keyword )) {
2043 $stuff_to_parse .= ' ';
2044 // Might make a more unique string for putting the number in soon
2045 // Basically, we don't put the styles in yet because then the styles themselves will
2046 // get highlighted if the language has a CSS keyword in it (like CSS, for example ;))
2047 $styles = "/$k/";
2048 if ($this->language_data['CASE_SENSITIVE'][$k]) {
2049 $stuff_to_parse = preg_replace(
2050 "/([^a-zA-Z0-9\$_\|\#;>|^])($keyword)(?=[^a-zA-Z0-9_<\|%\-&])/e",
2051 "'\\1' . $func2('\\2', '$k', 'BEGIN') . '<|$styles>' . $func('\\2') . '|>' . $func2('\\2', '$k', 'END')",
2052 $stuff_to_parse
2053 );
2054 } else {
2055 // Change the case of the word.
2056 // hackage again... must... release... 1.2...
2057 if ('smarty' == $this->language) { $hackage = '\/'; } else { $hackage = ''; }
2058 $stuff_to_parse = preg_replace(
2059 "/([^a-zA-Z0-9\$_\|\#;>$hackage|^])($keyword)(?=[^a-zA-Z0-9_<\|%\-&])/ie",
2060 "'\\1' . $func2('\\2', '$k', 'BEGIN') . '<|$styles>' . $func('\\2') . '|>' . $func2('\\2', '$k', 'END')",
2061 $stuff_to_parse
2062 );
2063 }
2064 $stuff_to_parse = substr($stuff_to_parse, 0, strlen($stuff_to_parse) - 1);
2065 }
2066 }
2067 }
2068 }
2069 }
2070
2071 //
2072 // Now that's all done, replace /[number]/ with the correct styles
2073 //
2074 foreach ($this->language_data['KEYWORDS'] as $k => $kws) {
2075 if (!$this->use_classes) {
2076 $attributes = ' style="' . $this->language_data['STYLES']['KEYWORDS'][$k] . '"';
2077 } else {
2078 $attributes = ' class="kw' . $k . '"';
2079 }
2080 $stuff_to_parse = str_replace("/$k/", $attributes, $stuff_to_parse);
2081 }
2082
2083 // Put number styles in
2084 if (!$this->use_classes && $this->lexic_permissions['NUMBERS']) {
2085 $attributes = ' style="' . $this->language_data['STYLES']['NUMBERS'][0] . '"';
2086 } else {
2087 $attributes = ' class="nu0"';
2088 }
2089 $stuff_to_parse = str_replace('/NUM!/', $attributes, $stuff_to_parse);
2090
2091 //
2092 // Highlight methods and fields in objects
2093 //
2094 if ($this->lexic_permissions['METHODS'] && $this->language_data['OOLANG']) {
2095 foreach ($this->language_data['OBJECT_SPLITTERS'] as $key => $splitter) {
2096 if (false !== stristr($stuff_to_parse, $splitter)) {
2097 if (!$this->use_classes) {
2098 $attributes = ' style="' . $this->language_data['STYLES']['METHODS'][$key] . '"';
2099 } else {
2100 $attributes = ' class="me' . $key . '"';
2101 }
2102 $stuff_to_parse = preg_replace("/(" . preg_quote($this->language_data['OBJECT_SPLITTERS'][$key], 1) . "[\s]*)([a-zA-Z\*\(][a-zA-Z0-9_\*]*)/", "\\1<|$attributes>\\2|>", $stuff_to_parse);
2103 }
2104 }
2105 }
2106
2107 //
2108 // Highlight brackets. Yes, I've tried adding a semi-colon to this list.
2109 // You try it, and see what happens ;)
2110 // TODO: Fix lexic permissions not converting entities if shouldn't
2111 // be highlighting regardless
2112 //
2113 if ($this->lexic_permissions['BRACKETS']) {
2114 $code_entities_match = array('[', ']', '(', ')', '{', '}');
2115 if (!$this->use_classes) {
2116 $code_entities_replace = array(
2117 '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#91;|>',
2118 '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#93;|>',
2119 '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#40;|>',
2120 '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#41;|>',
2121 '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#123;|>',
2122 '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#125;|>',
2123 );
2124 } else {
2125 $code_entities_replace = array(
2126 '<| class="br0">&#91;|>',
2127 '<| class="br0">&#93;|>',
2128 '<| class="br0">&#40;|>',
2129 '<| class="br0">&#41;|>',
2130 '<| class="br0">&#123;|>',
2131 '<| class="br0">&#125;|>',
2132 );
2133 }
2134 $stuff_to_parse = str_replace( $code_entities_match, $code_entities_replace, $stuff_to_parse );
2135 }
2136
2137 //
2138 // Add class/style for regexps
2139 //
2140 foreach ($this->language_data['REGEXPS'] as $key => $regexp) {
2141 if ($this->lexic_permissions['REGEXPS'][$key]) {
2142 if (!$this->use_classes) {
2143 $attributes = ' style="' . $this->language_data['STYLES']['REGEXPS'][$key] . '"';
2144 } else {
2145 $attributes = ' class="re' . $key . '"';
2146 }
2147 $stuff_to_parse = str_replace("!REG3XP$key!", "$attributes", $stuff_to_parse);
2148 }
2149 }
2150
2151 // Replace <DOT> with . for urls
2152 $stuff_to_parse = str_replace('<DOT>', '.', $stuff_to_parse);
2153 // Replace <|UR1| with <a href= for urls also
2154 if (isset($this->link_styles[GESHI_LINK])) {
2155 if ($this->use_classes) {
2156 $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target . ' href=', $stuff_to_parse);
2157 } else {
2158 $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target . ' style="' . $this->link_styles[GESHI_LINK] . '" href=', $stuff_to_parse);
2159 }
2160 } else {
2161 $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target . ' href=', $stuff_to_parse);
2162 }
2163
2164 //
2165 // NOW we add the span thingy ;)
2166 //
2167
2168 $stuff_to_parse = str_replace('<|', '<span', $stuff_to_parse);
2169 $stuff_to_parse = str_replace ( '|>', '</span>', $stuff_to_parse );
2170
2171 return substr($stuff_to_parse, 1);
2172 }
2173
2174 /**
2175 * Sets the time taken to parse the code
2176 *
2177 * @param microtime The time when parsing started
2178 * @param microtime The time when parsing ended
2179 * @since 1.0.2
2180 * @access private
2181 */
2182 function set_time ($start_time, $end_time)
2183 {
2184 $start = explode(' ', $start_time);
2185 $end = explode(' ', $end_time);
2186 $this->time = $end[0] + $end[1] - $start[0] - $start[1];
2187 }
2188
2189 /**
2190 * Gets the time taken to parse the code
2191 *
2192 * @return double The time taken to parse the code
2193 * @since 1.0.2
2194 */
2195 function get_time ()
2196 {
2197 return $this->time;
2198 }
2199
2200 /**
2201 * Gets language information and stores it for later use
2202 *
2203 * @access private
2204 * @todo Needs to load keys for lexic permissions for keywords, regexps etc
2205 */
2206 function load_language ($file_name)
2207 {
2208 $language_data = array();
2209 require $file_name;
2210 // Perhaps some checking might be added here later to check that
2211 // $language data is a valid thing but maybe not
2212 $this->language_data = $language_data;
2213 // Set strict mode if should be set
2214 if ($this->language_data['STRICT_MODE_APPLIES'] == GESHI_ALWAYS) {
2215 $this->strict_mode = true;
2216 }
2217 // Set permissions for all lexics to true
2218 // so they'll be highlighted by default
2219 foreach ($this->language_data['KEYWORDS'] as $key => $words) {
2220 $this->lexic_permissions['KEYWORDS'][$key] = true;
2221 }
2222 foreach ($this->language_data['COMMENT_SINGLE'] as $key => $comment) {
2223 $this->lexic_permissions['COMMENTS'][$key] = true;
2224 }
2225 foreach ($this->language_data['REGEXPS'] as $key => $regexp) {
2226 $this->lexic_permissions['REGEXPS'][$key] = true;
2227 }
2228 $this->enable_highlighting();
2229 // Set default class for CSS
2230 $this->overall_class = $this->language;
2231 }
2232
2233 /**
2234 * Takes the parsed code and various options, and creates the HTML
2235 * surrounding it to make it look nice.
2236 *
2237 * @param string The code already parsed
2238 * @return string The code nicely finalised
2239 * @since 1.0.0
2240 * @access private
2241 */
2242 function finalise ($parsed_code)
2243 {
2244 // Remove end parts of important declarations
2245 // This is BUGGY!! My fault for bad code: fix coming in 1.2
2246 // @todo Remove this crap
2247 if ($this->enable_important_blocks &&
2248 (strstr($parsed_code, @htmlspecialchars(GESHI_START_IMPORTANT, ENT_COMPAT, $this->encoding)) === false)) {
2249 $parsed_code = str_replace(@htmlspecialchars(GESHI_END_IMPORTANT, ENT_COMPAT, $this->encoding), '', $parsed_code);
2250 }
2251
2252 // Add HTML whitespace stuff if we're using the <div> header
2253 if ($this->header_type != GESHI_HEADER_PRE) {
2254 $parsed_code = $this->indent($parsed_code);
2255 }
2256
2257 // If we're using line numbers, we insert <li>s and appropriate
2258 // markup to style them (otherwise we don't need to do anything)
2259 if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
2260 // If we're using the <pre> header, we shouldn't add newlines because
2261 // the <pre> will line-break them (and the <li>s already do this for us)
2262 $ls = ($this->header_type != GESHI_HEADER_PRE) ? "\n" : '';
2263 // Get code into lines
2264 $code = explode("\n", $parsed_code);
2265 // Set vars to defaults for following loop
2266 $parsed_code = '';
2267 $i = 0;
2268 $attrs = array();
2269
2270 // Foreach line...
2271 foreach ($code as $line) {
2272 $line = ( $line ) ? $line : '&nbsp;';
2273 // If this is a "special line"...
2274 if ($this->line_numbers == GESHI_FANCY_LINE_NUMBERS &&
2275 $i % $this->line_nth_row == ($this->line_nth_row - 1)) {
2276 // Set the attributes to style the line
2277 if ($this->use_classes) {
2278 //$attr = ' class="li2"';
2279 $attrs['class'][] = 'li2';
2280 $def_attr = ' class="de2"';
2281 } else {
2282 //$attr = ' style="' . $this->line_style2 . '"';
2283 $attrs['style'][] = $this->line_style2;
2284 // This style "covers up" the special styles set for special lines
2285 // so that styles applied to special lines don't apply to the actual
2286 // code on that line
2287 $def_attr = ' style="' . $this->code_style . '"';
2288 }
2289 // Span or div?
2290 $start = "<div$def_attr>";
2291 $end = '</div>';
2292 } else {
2293 if ($this->use_classes) {
2294 //$attr = ' class="li1"';
2295 $attrs['class'][] = 'li1';
2296 $def_attr = ' class="de1"';
2297 } else {
2298 //$attr = ' style="' . $this->line_style1 . '"';
2299 $attrs['style'][] = $this->line_style1;
2300 $def_attr = ' style="' . $this->code_style . '"';
2301 }
2302 $start = "<div$def_attr>";
2303 $end = '</div>';
2304 }
2305
2306 ++$i;
2307 // Are we supposed to use ids? If so, add them
2308 if ($this->add_ids) {
2309 //$attr .= " id=\"{$this->overall_id}-{$i}\"";
2310 $attrs['id'][] = "$this->overall_id-$i";
2311 }
2312 if ($this->use_classes && in_array($i, $this->highlight_extra_lines)) {
2313 //$attr .= " class=\"ln-xtra\"";
2314 $attrs['class'][] = 'ln-xtra';
2315 }
2316 if (!$this->use_classes && in_array($i, $this->highlight_extra_lines)) {
2317 //$attr .= " style=\"{$this->highlight_extra_lines_style}\"";
2318 $attrs['style'][] = $this->highlight_extra_lines_style;
2319 }
2320
2321 // Add in the line surrounded by appropriate list HTML
2322 $attr_string = ' ';
2323 foreach ($attrs as $key => $attr) {
2324 $attr_string .= $key . '="' . implode(' ', $attr) . '"';
2325 }
2326 $parsed_code .= "<li$attr_string>$start$line$end</li>$ls";
2327 $attrs = array();
2328 }
2329 } else {
2330 // No line numbers, but still need to handle highlighting lines extra.
2331 // Have to use divs so the full width of the code is highlighted
2332 $code = explode("\n", $parsed_code);
2333 $parsed_code = '';
2334 $i = 0;
2335 foreach ($code as $line)
2336 {
2337 // Make lines have at least one space in them if they're empty
2338 $line = ($line) ? $line : '&nbsp;';
2339 if (in_array(++$i, $this->highlight_extra_lines)) {
2340 if ($this->use_classes) {
2341 $parsed_code .= '<div class="ln-xtra">';
2342 } else {
2343 $parsed_code .= "<div style=\"{$this->highlight_extra_lines_style}\">";
2344 }
2345 // Remove \n because it stuffs up <pre> header
2346 $parsed_code .= $line . "</div>";
2347 } else {
2348 $parsed_code .= $line . "\n";
2349 }
2350 }
2351 }
2352
2353 // purge some unnecessary stuff
2354 $parsed_code = preg_replace('#<span[^>]+>(\s*)</span>#', '\\1', $parsed_code);
2355 $parsed_code = preg_replace('#<div[^>]+>(\s*)</div>#', '\\1', $parsed_code);
2356
2357 if ($this->header_type == GESHI_HEADER_PRE) {
2358 // enforce line numbers when using pre
2359 $parsed_code = str_replace('<li></li>', '<li>&nbsp;</li>', $parsed_code);
2360 }
2361
2362 return $this->header() . chop($parsed_code) . $this->footer();
2363 }
2364
2365 /**
2366 * Creates the header for the code block (with correct attributes)
2367 *
2368 * @return string The header for the code block
2369 * @since 1.0.0
2370 * @access private
2371 */
2372 function header ()
2373 {
2374 // Get attributes needed
2375 $attributes = $this->get_attributes();
2376
2377 $ol_attributes = '';
2378
2379 if ($this->line_numbers_start != 1) {
2380 $ol_attributes .= ' start="' . $this->line_numbers_start . '"';
2381 }
2382
2383 // Get the header HTML
2384 $header = $this->format_header_content();
2385
2386 if (GESHI_HEADER_NONE == $this->header_type) {
2387 if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
2388 return "$header<ol$ol_attributes>";
2389 }
2390 return $header;
2391 }
2392
2393 // Work out what to return and do it
2394 if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
2395 if ($this->header_type == GESHI_HEADER_PRE) {
2396 return "<pre$attributes>$header<ol$ol_attributes>";
2397 } elseif ($this->header_type == GESHI_HEADER_DIV) {
2398 return "<div$attributes>$header<ol$ol_attributes>";
2399 }
2400 } else {
2401 if ($this->header_type == GESHI_HEADER_PRE) {
2402 return "<pre$attributes>$header";
2403 } elseif ($this->header_type == GESHI_HEADER_DIV) {
2404 return "<div$attributes>$header";
2405 }
2406 }
2407 }
2408
2409 /**
2410 * Returns the header content, formatted for output
2411 *
2412 * @return string The header content, formatted for output
2413 * @since 1.0.2
2414 * @access private
2415 */
2416 function format_header_content ()
2417 {
2418 $header = $this->header_content;
2419 if ($header) {
2420 if ($this->header_type == GESHI_HEADER_PRE) {
2421 $header = str_replace("\n", '', $header);
2422 }
2423 $header = $this->replace_keywords($header);
2424
2425 if ($this->use_classes) {
2426 $attr = ' class="head"';
2427 } else {
2428 $attr = " style=\"{$this->header_content_style}\"";
2429 }
2430 return "<div$attr>$header</div>";
2431 }
2432 }
2433
2434 /**
2435 * Returns the footer for the code block.
2436 *
2437 * @return string The footer for the code block
2438 * @since 1.0.0
2439 * @access private
2440 */
2441 function footer ()
2442 {
2443 $footer_content = $this->format_footer_content();
2444
2445 if (GESHI_HEADER_NONE == $this->header_type) {
2446 return ($this->line_numbers != GESHI_NO_LINE_NUMBERS) ? '</ol>' . $footer_content
2447 : $footer_content;
2448 }
2449
2450 if ($this->header_type == GESHI_HEADER_DIV) {
2451 if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
2452 return "</ol>$footer_content</div>";
2453 }
2454 return "$footer_content</div>";
2455 } else {
2456 if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
2457 return "</ol>$footer_content</pre>";
2458 }
2459 return "$footer_content</pre>";
2460 }
2461 }
2462
2463 /**
2464 * Returns the footer content, formatted for output
2465 *
2466 * @return string The footer content, formatted for output
2467 * @since 1.0.2
2468 * @access private
2469 */
2470 function format_footer_content ()
2471 {
2472 $footer = $this->footer_content;
2473 if ($footer) {
2474 if ($this->header_type == GESHI_HEADER_PRE) {
2475 $footer = str_replace("\n", '', $footer);;
2476 }
2477 $footer = $this->replace_keywords($footer);
2478
2479 if ($this->use_classes) {
2480 $attr = ' class="foot"';
2481 } else {
2482 $attr = " style=\"{$this->footer_content_style}\"";
2483 }
2484 return "<div$attr>$footer</div>";
2485 }
2486 }
2487
2488 /**
2489 * Replaces certain keywords in the header and footer with
2490 * certain configuration values
2491 *
2492 * @param string The header or footer content to do replacement on
2493 * @return string The header or footer with replaced keywords
2494 * @since 1.0.2
2495 * @access private
2496 */
2497 function replace_keywords ($instr)
2498 {
2499 $keywords = $replacements = array();
2500
2501 $keywords[] = '<TIME>';
2502 $replacements[] = number_format($this->get_time(), 3);
2503
2504 $keywords[] = '<LANGUAGE>';
2505 $replacements[] = $this->language;
2506
2507 $keywords[] = '<VERSION>';
2508 $replacements[] = GESHI_VERSION;
2509
2510 return str_replace($keywords, $replacements, $instr);
2511 }
2512
2513 /**
2514 * Gets the CSS attributes for this code
2515 *
2516 * @return The CSS attributes for this code
2517 * @since 1.0.0
2518 * @access private
2519 * @todo Document behaviour change - class is outputted regardless of whether we're using classes or not.
2520 * Same with style
2521 */
2522 function get_attributes ()
2523 {
2524 $attributes = '';
2525
2526 if ($this->overall_class != '') {
2527 $attributes .= " class=\"{$this->overall_class}\"";
2528 }
2529 if ($this->overall_id != '') {
2530 $attributes .= " id=\"{$this->overall_id}\"";
2531 }
2532 if ($this->overall_style != '') {
2533 $attributes .= ' style="' . $this->overall_style . '"';
2534 }
2535 return $attributes;
2536 }
2537
2538 /**
2539 * Returns a stylesheet for the highlighted code. If $economy mode
2540 * is true, we only return the stylesheet declarations that matter for
2541 * this code block instead of the whole thing
2542 *
2543 * @param boolean Whether to use economy mode or not
2544 * @return string A stylesheet built on the data for the current language
2545 * @since 1.0.0
2546 */
2547 function get_stylesheet ($economy_mode = true)
2548 {
2549 // If there's an error, chances are that the language file
2550 // won't have populated the language data file, so we can't
2551 // risk getting a stylesheet...
2552 if ($this->error) {
2553 return '';
2554 }
2555 // First, work out what the selector should be. If there's an ID,
2556 // that should be used, the same for a class. Otherwise, a selector
2557 // of '' means that these styles will be applied anywhere
2558 $selector = ($this->overall_id != '') ? "#{$this->overall_id} " : '';
2559 $selector = ($selector == '' && $this->overall_class != '') ? ".{$this->overall_class} " : $selector;
2560
2561 // Header of the stylesheet
2562 if (!$economy_mode) {
2563 $stylesheet = "/**\n * GeSHi Dynamically Generated Stylesheet\n * --------------------------------------\n * Dynamically generated stylesheet for {$this->language}\n * CSS class: {$this->overall_class}, CSS id: {$this->overall_id}\n * GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter)\n */\n";
2564 } else {
2565 $stylesheet = '/* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */' . "\n";
2566 }
2567
2568 // Set the <ol> to have no effect at all if there are line numbers
2569 // (<ol>s have margins that should be destroyed so all layout is
2570 // controlled by the set_overall_style method, which works on the
2571 // <pre> or <div> container). Additionally, set default styles for lines
2572 if (!$economy_mode || $this->line_numbers != GESHI_NO_LINE_NUMBERS) {
2573 //$stylesheet .= "$selector, {$selector}ol, {$selector}ol li {margin: 0;}\n";
2574 $stylesheet .= "$selector.de1, $selector.de2 {{$this->code_style}}\n";
2575 }
2576
2577 // Add overall styles
2578 if (!$economy_mode || $this->overall_style != '') {
2579 $stylesheet .= "$selector {{$this->overall_style}}\n";
2580 }
2581
2582 // Add styles for links
2583 foreach ($this->link_styles as $key => $style) {
2584 if (!$economy_mode || $key == GESHI_LINK && $style != '') {
2585 $stylesheet .= "{$selector}a:link {{$style}}\n";
2586 }
2587 if (!$economy_mode || $key == GESHI_HOVER && $style != '') {
2588 $stylesheet .= "{$selector}a:hover {{$style}}\n";
2589 }
2590 if (!$economy_mode || $key == GESHI_ACTIVE && $style != '') {
2591 $stylesheet .= "{$selector}a:active {{$style}}\n";
2592 }
2593 if (!$economy_mode || $key == GESHI_VISITED && $style != '') {
2594 $stylesheet .= "{$selector}a:visited {{$style}}\n";
2595 }
2596 }
2597
2598 // Header and footer
2599 if (!$economy_mode || $this->header_content_style != '') {
2600 $stylesheet .= "$selector.head {{$this->header_content_style}}\n";
2601 }
2602 if (!$economy_mode || $this->footer_content_style != '') {
2603 $stylesheet .= "$selector.foot {{$this->footer_content_style}}\n";
2604 }
2605
2606 // Styles for important stuff
2607 if (!$economy_mode || $this->important_styles != '') {
2608 $stylesheet .= "$selector.imp {{$this->important_styles}}\n";
2609 }
2610
2611 // Styles for lines being highlighted extra
2612 if (!$economy_mode || count($this->highlight_extra_lines)) {
2613 $stylesheet .= "$selector.ln-xtra {{$this->highlight_extra_lines_style}}\n";
2614 }
2615
2616 // Simple line number styles
2617 if (!$economy_mode || ($this->line_numbers != GESHI_NO_LINE_NUMBERS && $this->line_style1 != '')) {
2618 $stylesheet .= "{$selector}li {{$this->line_style1}}\n";
2619 }
2620
2621 // If there is a style set for fancy line numbers, echo it out
2622 if (!$economy_mode || ($this->line_numbers == GESHI_FANCY_LINE_NUMBERS && $this->line_style2 != '')) {
2623 $stylesheet .= "{$selector}li.li2 {{$this->line_style2}}\n";
2624 }
2625
2626 foreach ($this->language_data['STYLES']['KEYWORDS'] as $group => $styles) {
2627 if (!$economy_mode || !($economy_mode && (!$this->lexic_permissions['KEYWORDS'][$group] || $styles == ''))) {
2628 $stylesheet .= "$selector.kw$group {{$styles}}\n";
2629 }
2630 }
2631 foreach ($this->language_data['STYLES']['COMMENTS'] as $group => $styles) {
2632 if (!$economy_mode || !($economy_mode && $styles == '') &&
2633 !($economy_mode && !$this->lexic_permissions['COMMENTS'][$group])) {
2634 $stylesheet .= "$selector.co$group {{$styles}}\n";
2635 }
2636 }
2637 foreach ($this->language_data['STYLES']['ESCAPE_CHAR'] as $group => $styles) {
2638 if (!$economy_mode || !($economy_mode && $styles == '') && !($economy_mode &&
2639 !$this->lexic_permissions['ESCAPE_CHAR'])) {
2640 $stylesheet .= "$selector.es$group {{$styles}}\n";
2641 }
2642 }
2643 foreach ($this->language_data['STYLES']['SYMBOLS'] as $group => $styles) {
2644 if (!$economy_mode || !($economy_mode && $styles == '') && !($economy_mode &&
2645 !$this->lexic_permissions['BRACKETS'])) {
2646 $stylesheet .= "$selector.br$group {{$styles}}\n";
2647 }
2648 }
2649 foreach ($this->language_data['STYLES']['STRINGS'] as $group => $styles) {
2650 if (!$economy_mode || !($economy_mode && $styles == '') && !($economy_mode &&
2651 !$this->lexic_permissions['STRINGS'])) {
2652 $stylesheet .= "$selector.st$group {{$styles}}\n";
2653 }
2654 }
2655 foreach ($this->language_data['STYLES']['NUMBERS'] as $group => $styles) {
2656 if (!$economy_mode || !($economy_mode && $styles == '') && !($economy_mode &&
2657 !$this->lexic_permissions['NUMBERS'])) {
2658 $stylesheet .= "$selector.nu$group {{$styles}}\n";
2659 }
2660 }
2661 foreach ($this->language_data['STYLES']['METHODS'] as $group => $styles) {
2662 if (!$economy_mode || !($economy_mode && $styles == '') && !($economy_mode &&
2663 !$this->lexic_permissions['METHODS'])) {
2664 $stylesheet .= "$selector.me$group {{$styles}}\n";
2665 }
2666 }
2667 foreach ($this->language_data['STYLES']['SCRIPT'] as $group => $styles) {
2668 if (!$economy_mode || !($economy_mode && $styles == '')) {
2669 $stylesheet .= "$selector.sc$group {{$styles}}\n";
2670 }
2671 }
2672 foreach ($this->language_data['STYLES']['REGEXPS'] as $group => $styles) {
2673 if (!$economy_mode || !($economy_mode && $styles == '') && !($economy_mode &&
2674 !$this->lexic_permissions['REGEXPS'][$group])) {
2675 $stylesheet .= "$selector.re$group {{$styles}}\n";
2676 }
2677 }
2678
2679 return $stylesheet;
2680 }
2681
2682 } // End Class GeSHi
2683
2684
2685 if (!function_exists('geshi_highlight')) {
2686 /**
2687 * Easy way to highlight stuff. Behaves just like highlight_string
2688 *
2689 * @param string The code to highlight
2690 * @param string The language to highlight the code in
2691 * @param string The path to the language files. You can leave this blank if you need
2692 * as from version 1.0.7 the path should be automatically detected
2693 * @param boolean Whether to return the result or to echo
2694 * @return string The code highlighted (if $return is true)
2695 * @since 1.0.2
2696 */
2697 function geshi_highlight ($string, $language, $path, $return = false)
2698 {
2699 $geshi = new GeSHi($string, $language, $path);
2700 $geshi->set_header_type(GESHI_HEADER_NONE);
2701 if ($return) {
2702 return '<code>' . $geshi->parse_code() . '</code>';
2703 }
2704 echo '<code>' . $geshi->parse_code() . '</code>';
2705 if ($geshi->error()) {
2706 return false;
2707 }
2708 return true;
2709 }
2710 }
2711
2712 ?>