Source for file geshi.php

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

Documentation generated on Tue, 26 Jul 2005 17:10:29 +1200 by phpDocumentor 1.2.3