3 * GeSHi - Generic Syntax Highlighter
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
9 * For changes, release notes, TODOs etc, see the relevant files in the docs/ directory
11 * This file is part of GeSHi.
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.
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.
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
28 * @author Nigel McNie <nigel@geshi.org>
29 * @copyright Copyright © 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 $
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
42 /** The version of this GeSHi file */
43 define('GESHI_VERSION', '1.0.7.9');
45 /** Set the correct directory separator */
46 define('GESHI_DIR_SEPARATOR', ('WIN' != substr(PHP_OS
, 0, 3)) ?
'/' : '\\');
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
);
53 /** The language file directory for GeSHi
55 define('GESHI_LANG_ROOT', GESHI_ROOT
. 'geshi' . GESHI_DIR_SEPARATOR
);
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);
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);
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);
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);
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>');
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);
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);
127 /** Used in language files to mark comments */
128 define('GESHI_COMMENTS', 0);
130 // Error detection - use these to analyse faults
131 /** No sourcecode to highlight was specified
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);
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.
154 * @author Nigel McNie <nigel@geshi.org>
155 * @copyright Copyright © 2004, 2005 Nigel McNie
163 * The source code to highlight
169 * The language to use when highlighting
175 * The data for the language used
178 var $language_data = array();
181 * The path to the language files
184 var $language_path = GESHI_LANG_ROOT
;
187 * The error message associated with an error
189 * @todo check err reporting works
194 * Possible error messages
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'
206 * Whether highlighting is strict or not
209 var $strict_mode = false
;
212 * Whether to use CSS classes in output
215 var $use_classes = false
;
218 * The type of header to use. Can be one of the following
222 * <li><b>GESHI_HEADER_PRE</b>: Source is outputted in
223 * a <pre> HTML element.</li>
224 * <li><b>GESHI_HEADER_DIV</b>: Source is outputted in
225 * a <div> HTML element.</li>
226 * <li><b>GESHI_HEADER_NONE</b>: No header is outputted.</li>
231 var $header_type = GESHI_HEADER_PRE
;
234 * Array of permissions for which lexics should be highlighted
237 var $lexic_permissions = array(
238 'KEYWORDS' => array(),
239 'COMMENTS' => array('MULTI' => true
),
240 'REGEXPS' => array(),
241 'ESCAPE_CHAR' => true
,
251 * The time it took to parse the code
257 * The content of the header block
260 var $header_content = '';
263 * The content of the footer block
266 var $footer_content = '';
269 * The style of the header block
272 var $header_content_style = '';
275 * The style of the footer block
278 var $footer_content_style = '';
281 * The styles for hyperlinks in the code
284 var $link_styles = array();
287 * Whether important blocks should be recognised or not
290 * @todo REMOVE THIS FUNCTIONALITY!
292 var $enable_important_blocks = false
;
295 * Styles for important parts of the code
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
301 var $important_styles = 'font-weight: bold; color: red;'; // Styles for important parts of the code
304 * Whether CSS IDs should be added to the code
307 var $add_ids = false
;
310 * Lines that should be highlighted extra
313 var $highlight_extra_lines = array();
316 * Styles of extra-highlighted lines
319 var $highlight_extra_lines_style = 'color: #cc0; background-color: #ffc;';
322 * Number at which line numbers should start at
324 * @todo Warning documentation about XHTML compliance
326 var $line_numbers_start = 1;
329 * The overall style for this code block
332 var $overall_style = '';
335 * The style for the actual code
338 var $code_style = 'font-family: \'Courier New\', Courier, monospace; font-weight: normal;';
341 * The overall class for this code block
344 var $overall_class = '';
347 * The overall ID for this code block
350 var $overall_id = '';
356 var $line_style1 = 'font-family: \'Courier New\', Courier, monospace; color: black; font-weight: normal; font-style: normal;';
359 * Line number styles for fancy lines
362 var $line_style2 = 'font-weight: bold;';
365 * Flag for how line nubmers are displayed
368 var $line_numbers = GESHI_NO_LINE_NUMBERS
;
371 * The "nth" value for fancy line highlighting
374 var $line_nth_row = 0;
377 * The size of tab stops
383 * Default target for keyword links
386 var $link_target = '';
389 * The encoding to use for entity encoding
392 var $encoding = 'ISO-8859-1';
397 * Creates a new GeSHi object, with source and language
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()}
410 function GeSHi ($source, $language, $path = '')
412 $this->set_source($source);
413 $this->set_language_path($path);
414 $this->set_language($language);
418 * Returns an error message associated with the last GeSHi operation,
419 * or false if no error has occured
421 * @return string|false An error message if there has been an error, else false
427 $msg = $this->error_messages
[$this->error
];
428 $debug_tpl_vars = array(
429 '{LANGUAGE}' => $this->language
,
430 '{PATH}' => $this->language_path
432 foreach ($debug_tpl_vars as $tpl => $var) {
433 $msg = str_replace($tpl, $var, $msg);
435 return "<br /><strong>GeSHi Error:</strong> $msg (code $this->error)<br />";
441 * Gets a human-readable language name (thanks to Simon Patterson
444 * @return string The name for the current language
447 function get_language_name ()
449 if (GESHI_ERROR_NO_SUCH_LANG
== $this->_error
) {
450 return $this->language_data
['LANG_NAME'] . ' (Unknown Language)';
452 return $this->language_data
['LANG_NAME'];
456 * Sets the source code for this object
458 * @param string The source code to highlight
461 function set_source ($source)
463 $this->source
= $source;
467 * Sets the language for this object
469 * @param string The name of the language to use
472 function set_language ($language)
474 $this->error
= false
;
475 $this->strict_mode
= GESHI_NEVER
;
477 $language = preg_replace('#[^a-zA-Z0-9\-_]#', '', $language);
478 $this->language
= strtolower($language);
480 $file_name = $this->language_path
. $this->language
. '.php';
481 if (!is_readable($file_name)) {
482 $this->error
= GESHI_ERROR_NO_SUCH_LANG
;
485 // Load the language for parsing
486 $this->load_language($file_name);
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.
494 * @param string The path to the language directory
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.
501 function set_language_path ($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
510 * Sets the type of header to be used.
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.
517 * From 1.0.7.2, you can use GESHI_HEADER_NONE to specify that no header code
518 * should be outputted.
520 * @param int The type of header to be used
523 function set_header_type ($type)
525 if (GESHI_HEADER_DIV
!= $type && GESHI_HEADER_PRE
!= $type && GESHI_HEADER_NONE
!= $type) {
526 $this->error
= GESHI_ERROR_INVALID_HEADER_TYPE
;
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;';
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
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
545 function set_overall_style ($style, $preserve_defaults = false
)
547 if (!$preserve_defaults) {
548 $this->overall_style
= $style;
550 $this->overall_style
.= $style;
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
559 * @param string The class name to use for this block of code
562 function set_overall_class ($class)
564 $this->overall_class
= $class;
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
571 * @param string The ID to use for this block of code
574 function set_overall_id ($id)
576 $this->overall_id
= $id;
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
583 * @param boolean Whether to turn classes on or not
586 function enable_classes ($flag = true
)
588 $this->use_classes
= ($flag) ? true
: false
;
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
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.
602 * @param string The style to use for actual code
603 * @param boolean Whether to merge the current styles with the new styles
605 function set_code_style ($style, $preserve_defaults = false
)
607 if (!$preserve_defaults) {
608 $this->code_style
= $style;
610 $this->code_style
.= $style;
615 * Sets the styles for the line numbers.
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
626 function set_line_style ($style1, $style2 = '', $preserve_defaults = false
)
628 if (is_bool($style2)) {
629 $preserve_defaults = $style2;
632 if (!$preserve_defaults) {
633 $this->line_style1
= $style1;
634 $this->line_style2
= $style2;
636 $this->line_style1
.= $style1;
637 $this->line_style2
.= $style2;
642 * Sets whether line numbers should be displayed.
644 * Valid values for the first parameter are:
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>
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.
656 * @param int How line numbers should be displayed
657 * @param int Defines which lines are fancy
660 function enable_line_numbers ($flag, $nth_row = 5)
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
;
666 $this->line_numbers
= $flag;
667 $this->line_nth_row
= $nth_row;
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
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
681 function set_keyword_group_style ($key, $style, $preserve_defaults = false
)
683 if (!$preserve_defaults) {
684 $this->language_data
['STYLES']['KEYWORDS'][$key] = $style;
686 $this->language_data
['STYLES']['KEYWORDS'][$key] .= $style;
691 * Turns highlighting on/off for a keyword group
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
697 function set_keyword_group_highlighting ( $key, $flag = true
)
699 $this->lexic_permissions
['KEYWORDS'][$key] = ($flag) ? true
: false
;
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
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
713 function set_comments_style ($key, $style, $preserve_defaults = false
)
715 if (!$preserve_defaults) {
716 $this->language_data
['STYLES']['COMMENTS'][$key] = $style;
718 $this->language_data
['STYLES']['COMMENTS'][$key] .= $style;
723 * Turns highlighting on/off for comment groups
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
729 function set_comments_highlighting ($key, $flag = true
)
731 $this->lexic_permissions
['COMMENTS'][$key] = ($flag) ? true
: false
;
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
739 * @param string The style to make the escape characters
740 * @param boolean Whether to merge the new styles with the old or just
744 function set_escape_characters_style ($style, $preserve_defaults = false
)
746 if (!$preserve_defaults) {
747 $this->language_data
['STYLES']['ESCAPE_CHAR'][0] = $style;
749 $this->language_data
['STYLES']['ESCAPE_CHAR'][0] .= $style;
754 * Turns highlighting on/off for escaped characters
756 * @param boolean Whether to turn highlighting for escape characters on or off
759 function set_escape_characters_highlighting ($flag = true
)
761 $this->lexic_permissions
['ESCAPE_CHAR'] = ($flag) ? true
: false
;
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
769 * This method is DEPRECATED: use set_symbols_style instead.
770 * This method will be removed in 1.2.X
772 * @param string The style to make the brackets
773 * @param boolean Whether to merge the new styles with the old or just
776 * @deprecated In favour of set_symbols_style
778 function set_brackets_style ($style, $preserve_defaults = false
)
780 if (!$preserve_defaults) {
781 $this->language_data
['STYLES']['BRACKETS'][0] = $style;
783 $this->language_data
['STYLES']['BRACKETS'][0] .= $style;
788 * Turns highlighting on/off for brackets
790 * This method is DEPRECATED: use set_symbols_highlighting instead.
791 * This method will be remove in 1.2.X
793 * @param boolean Whether to turn highlighting for brackets on or off
795 * @deprecated In favour of set_symbols_highlighting
797 function set_brackets_highlighting ($flag)
799 $this->lexic_permissions
['BRACKETS'] = ($flag) ? true
: false
;
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
807 * @param string The style to make the symbols
808 * @param boolean Whether to merge the new styles with the old or just
812 function set_symbols_style ($style, $preserve_defaults = false
)
814 if (!$preserve_defaults) {
815 $this->language_data
['STYLES']['SYMBOLS'][0] = $style;
817 $this->language_data
['STYLES']['SYMBOLS'][0] .= $style;
819 // For backward compatibility
820 $this->set_brackets_style ($style, $preserve_defaults);
824 * Turns highlighting on/off for symbols
826 * @param boolean Whether to turn highlighting for symbols on or off
829 function set_symbols_highlighting ($flag)
831 $this->lexic_permissions
['SYMBOLS'] = ($flag) ? true
: false
;
832 // For backward compatibility
833 $this->set_brackets_highlighting ($flag);
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
841 * @param string The style to make the escape characters
842 * @param boolean Whether to merge the new styles with the old or just
846 function set_strings_style ($style, $preserve_defaults = false
)
848 if (!$preserve_defaults) {
849 $this->language_data
['STYLES']['STRINGS'][0] = $style;
851 $this->language_data
['STYLES']['STRINGS'][0] .= $style;
856 * Turns highlighting on/off for strings
858 * @param boolean Whether to turn highlighting for strings on or off
861 function set_strings_highlighting ($flag)
863 $this->lexic_permissions
['STRINGS'] = ($flag) ? true
: false
;
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
871 * @param string The style to make the numbers
872 * @param boolean Whether to merge the new styles with the old or just
876 function set_numbers_style ($style, $preserve_defaults = false
)
878 if (!$preserve_defaults) {
879 $this->language_data
['STYLES']['NUMBERS'][0] = $style;
881 $this->language_data
['STYLES']['NUMBERS'][0] .= $style;
886 * Turns highlighting on/off for numbers
888 * @param boolean Whether to turn highlighting for numbers on or off
891 function set_numbers_highlighting ($flag)
893 $this->lexic_permissions
['NUMBERS'] = ($flag) ? true
: false
;
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
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
909 function set_methods_style ($key, $style, $preserve_defaults = false
)
911 if (!$preserve_defaults) {
912 $this->language_data
['STYLES']['METHODS'][$key] = $style;
914 $this->language_data
['STYLES']['METHODS'][$key] .= $style;
919 * Turns highlighting on/off for methods
921 * @param boolean Whether to turn highlighting for methods on or off
924 function set_methods_highlighting ($flag)
926 $this->lexic_permissions
['METHODS'] = ($flag) ? true
: false
;
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
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
939 function set_regexps_style ($key, $style, $preserve_defaults = false
)
941 if (!$preserve_defaults) {
942 $this->language_data
['STYLES']['REGEXPS'][$key] = $style;
944 $this->language_data
['STYLES']['REGEXPS'][$key] .= $style;
949 * Turns highlighting on/off for regexps
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
955 function set_regexps_highlighting ($key, $flag)
957 $this->lexic_permissions
['REGEXPS'][$key] = ($flag) ? true
: false
;
961 * Sets whether a set of keywords are checked for in a case sensitive manner
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
967 function set_case_sensitivity ($key, $case)
969 $this->language_data
['CASE_SENSITIVE'][$key] = ($case) ? true
: false
;
973 * Sets the case that keywords should use when found. Use the constants:
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>
981 * @param int A constant specifying what to do with matched keywords
983 * @todo Error check the passed value
985 function set_case_keywords ($case)
987 $this->language_data
['CASE_KEYWORDS'] = $case;
991 * Sets how many spaces a tab is substituted for
993 * Widths below zero are ignored
995 * @param int The tab width
998 function set_tab_width ($width)
1000 $this->tab_width
= intval($width);
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.
1008 * @param boolean Whether to enable strict mode or not
1011 function enable_strict_mode ($mode = true
)
1013 if (GESHI_MAYBE
== $this->language_data
['STRICT_MODE_APPLIES']) {
1014 $this->strict_mode
= ($mode) ? true
: false
;
1019 * Disables all highlighting
1022 * @todo Rewrite with an array traversal
1024 function disable_highlighting ()
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
;
1032 $this->lexic_permissions
[$key] = false
;
1036 $this->enable_important_blocks
= false
;
1040 * Enables all highlighting
1043 * @todo Rewrite with array traversal
1045 function enable_highlighting ()
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
;
1053 $this->lexic_permissions
[$key] = true
;
1057 $this->enable_important_blocks
= true
;
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
1064 * @param string The extension to get a language name for
1065 * @param array A lookup array to use instead of the default
1067 * @todo Re-think about how this method works (maybe make it private and/or make it
1068 * a extension->lang lookup?)
1071 function get_language_name_from_extension ( $extension, $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'),
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'),
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'),
1110 'visualfoxpro' => array(),
1111 'xml' => array('xml')
1115 foreach ($lookup as $lang => $extensions) {
1116 foreach ($extensions as $ext) {
1117 if ($ext == $extension) {
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
1131 * The language table is in the form
1133 * 'lang_name' => array('extension', 'extension', ...),
1137 * @todo Complete rethink of this and above method
1140 function load_from_file ($file_name, $lookup = array())
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));
1146 $this->error
= GESHI_ERROR_FILE_NOT_READABLE
;
1151 * Adds a keyword to a keyword group for highlighting
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
1157 function add_keyword ($key, $word)
1159 $this->language_data
['KEYWORDS'][$key][] = $word;
1163 * Removes a keyword from a keyword group
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
1169 function remove_keyword ($key, $word)
1171 $this->language_data
['KEYWORDS'][$key] =
1172 array_diff($this->language_data
['KEYWORDS'][$key], array($word));
1176 * Creates a new keyword group
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
1184 function add_keyword_group ( $key, $styles, $case_sensitive = true
, $words = array() )
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;
1194 * Removes a keyword group
1196 * @param int The key of the keyword group to remove
1199 function remove_keyword_group ($key)
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]);
1208 * Sets the content of the header block
1210 * @param string The content of the header block
1213 function set_header_content ($content)
1215 $this->header_content
= $content;
1219 * Sets the content of the footer block
1221 * @param string The content of the footer block
1224 function set_footer_content ($content)
1226 $this->footer_content
= $content;
1230 * Sets the style for the header content
1232 * @param string The style for the header content
1235 function set_header_content_style ($style)
1237 $this->header_content_style
= $style;
1241 * Sets the style for the footer content
1243 * @param string The style for the footer content
1246 function set_footer_content_style ($style)
1248 $this->footer_content_style
= $style;
1252 * Sets the base URL to be used for keywords
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
1260 function set_url_for_keyword_group ($group, $url)
1262 $this->language_data
['URLS'][$group] = $url;
1266 * Sets styles for links in code
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
1273 function set_link_styles ($type, $styles)
1275 $this->link_styles
[$type] = $styles;
1279 * Sets the target for links in code
1281 * @param string The target for links in the code, e.g. _blank
1284 function set_link_target ( $target )
1287 $this->link_target
= '';
1289 $this->link_target
= ' target="' . $target . '" ';
1294 * Sets styles for important parts of the code
1296 * @param string The styles to use on important parts of the code
1299 function set_important_styles ($styles)
1301 $this->important_styles
= $styles;
1305 * Sets whether context-important blocks are highlighted
1307 * @todo REMOVE THIS SHIZ FROM GESHI!
1310 function enable_important_blocks ( $flag )
1312 $this->enable_important_blocks
= ( $flag ) ? true
: false
;
1316 * Whether CSS IDs should be added to each line
1318 * @param boolean If true, IDs will be added to each line.
1321 function enable_ids ($flag = true
)
1323 $this->add_ids
= ($flag) ? true
: false
;
1327 * Specifies which lines to highlight extra
1329 * @param mixed An array of line numbers to highlight, or just a line
1330 * number on its own.
1332 * @todo Some data replication here that could be cut down on
1334 function highlight_lines_extra ($lines)
1336 if (is_array($lines)) {
1337 foreach ($lines as $line) {
1338 $this->highlight_extra_lines
[intval($line)] = intval($line);
1341 $this->highlight_extra_lines
[intval($lines)] = intval($lines);
1346 * Sets the style for extra-highlighted lines
1348 * @param string The style for extra-highlighted lines
1351 function set_highlight_lines_extra_style ($styles)
1353 $this->highlight_extra_lines_style
= $styles;
1357 * Sets what number line numbers should start at. Should
1358 * be a positive integer, and will be converted to one.
1360 * <b>Warning:</b> Using this method will add the "start"
1361 * attribute to the <ol> 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.
1368 * @param int The number to start line numbers at
1371 function start_line_numbers_at ($number)
1373 $this->line_numbers_start
= abs(intval($number));
1377 * Sets the encoding used for htmlspecialchars(), for international
1380 * @param string The encoding to use for the source
1383 function set_encoding ($encoding)
1386 $this->encoding
= $encoding;
1391 * Returns the code in $this->source, highlighted and surrounded by the
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 <span>s
1400 function parse_code ()
1403 $start_time = microtime();
1405 // Firstly, if there is an error, we won't highlight
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);
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);
1419 // Initialise various stuff
1420 $length = strlen($code);
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 = '';
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
;
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 => ''));
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));
1452 if ($check == $open) {
1453 // We start a new block with the highlightable
1455 $HIGHLIGHTING_ON = $open;
1456 $i +
= strlen($open) - 1;
1458 $parts[++
$k][0] = $char;
1460 // No point going around again...
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
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] = '';
1485 $parts[$k][1] = ( isset($parts[$k][1]) ) ?
$parts[$k][1] . $char : $char;
1487 $HIGHLIGHTING_ON = '';
1489 // Not strict mode - simply dump the source into
1490 // the array at index 1 (the first highlightable block)
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
1502 foreach ($parts as $key => $data) {
1504 // If this block should be highlighted...
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) {
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] . '"';
1524 $attributes = ' class="sc' . $script_key . '"';
1526 $result .= "<span$attributes>";
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)
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???
1549 if (!$this->use_classes
) {
1550 $attributes = ' style="' . $this->language_data
['STYLES']['STRINGS'][0] . '"';
1552 $attributes = ' class="st0"';
1554 $char = '</span>' . $char . "<span$attributes>";
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>';
1563 if ($HARDQUOTE_OPEN)
1565 if ($ESCAPE_CHAR_OPEN)
1568 foreach ($this->language_data
['HARDESCAPE'] as $hardesc)
1569 if (substr($part, $i, strlen($hardesc)) == $hardesc)
1576 if (!$ESCAPE_CHAR_OPEN) {
1578 $CLOSE_STRING = true
;
1581 $HARDQUOTE_OPEN = false
;
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] . '"';
1591 $attributes = ' class="st0"';
1593 $char = "<span$attributes>" . htmlspecialchars($char, ENT_COMPAT
, $this->encoding
);
1595 $result .= $this->parse_non_string_part( $stuff_to_parse );
1596 $stuff_to_parse = '';
1599 substr($part, $i, strlen($hq)) == $hq &&
1600 ($STRING_OPEN == '') && $this->lexic_permissions
['STRINGS']
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] . '"';
1608 $attributes = ' class="st0"';
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 != '')
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)
1623 if (substr($part, $i, strlen($hard)) == $hard)
1625 $ESCAPE_CHAR_OPEN = true
;
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] . '"';
1633 $attributes = ' class="es0"';
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
1640 $ESCAPE_CHAR_OPEN = false
;
1644 $ESCAPE_CHAR_OPEN = false
;
1645 if ($this->lexic_permissions
['ESCAPE_CHAR']) {
1649 } elseif ($ESCAPE_CHAR_OPEN) {
1650 if ($this->lexic_permissions
['ESCAPE_CHAR']) {
1653 $ESCAPE_CHAR_OPEN = false
;
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'] . '"';
1670 $attributes = ' class="coMULTI"';
1672 $test_str = "<span$attributes>" . @htmlspecialchars($test_str, ENT_COMPAT
, $this->encoding
);
1674 if (!$this->use_classes
) {
1675 $attributes = ' style="' . $this->important_styles
. '"';
1677 $attributes = ' class="imp"';
1679 // We don't include the start of the comment if it's an
1681 $test_str = "<span$attributes>";
1684 $test_str = @htmlspecialchars($test_str, ENT_COMPAT
, $this->encoding
);
1687 $close_pos = strpos( $part, $close, $i +
strlen($close) );
1689 if ($close_pos === false
) {
1690 $close_pos = strlen($part);
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);
1702 $test_str .= $rest_of_comment;
1705 if ($this->lexic_permissions
['COMMENTS']['MULTI'] ||
1706 $test_str_match == GESHI_START_IMPORTANT
) {
1707 $test_str .= '</span>';
1709 $i = $close_pos +
$com_len - 1;
1711 $result .= $this->parse_non_string_part($stuff_to_parse);
1712 $stuff_to_parse = '';
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);
1724 $match = (strtolower($comment_mark) == strtolower($test_str));
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] . '"';
1732 $attributes = ' class="co' . $comment_key . '"';
1734 $test_str = "<span$attributes>" . @htmlspecialchars($this->change_case($test_str), ENT_COMPAT
, $this->encoding
);
1736 $test_str = @htmlspecialchars($test_str, ENT_COMPAT
, $this->encoding
);
1738 $close_pos = strpos($part, "\n", $i);
1740 if ($close_pos === false
) {
1741 $close_pos = strlen($part);
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>";
1748 // Take into account that the comment might be the last in the source
1754 $result .= $this->parse_non_string_part($stuff_to_parse);
1755 $stuff_to_parse = '';
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
);
1769 $char = @htmlspecialchars($char, ENT_COMPAT
, $this->encoding
);
1772 // Where are we adding this char?
1773 if (!$COMMENT_MATCHED) {
1774 if (($STRING_OPEN == '') && !$CLOSE_STRING) {
1775 $stuff_to_parse .= $char;
1778 $CLOSE_STRING = false
;
1781 $result .= $test_str;
1782 $COMMENT_MATCHED = false
;
1785 // Parse the last bit
1786 $result .= $this->parse_non_string_part($stuff_to_parse);
1787 $stuff_to_parse = '';
1789 $result .= @htmlspecialchars($part, ENT_COMPAT
, $this->encoding
);
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>';
1797 // Else not a block to highlight
1798 $result .= @htmlspecialchars($part, ENT_COMPAT
, $this->encoding
);
1802 // Parse the last stuff (redundant?)
1803 $result .= $this->parse_non_string_part($stuff_to_parse);
1805 // Lop off the very first and last spaces
1806 $result = substr($result, 1, strlen($result) - 1);
1808 // Are we still in a string?
1810 $result .= '</span>';
1813 // We're finished: stop timing
1814 $this->set_time($start_time, microtime());
1816 return $this->finalise($result);
1820 * Swaps out spaces and tabs for HTML indentation. Not needed if
1821 * the code is in a pre block...
1823 * @param string The source to indent
1824 * @return string The source with HTML indenting applied
1828 function indent ($result)
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;
1840 $tab_width = $this->tab_width
;
1841 $length = strlen($line);
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) {
1856 $result_line .= '>';
1858 } elseif (!$IN_TAG && '<' == $char) {
1860 $result_line .= '<';
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) {
1868 $result_line .= '&';
1869 } elseif (!$IN_TAG && "\t" == $char) {
1871 // OPTIMISE - move $strs out. Make an array:
1875 // 3 => ' ' etc etc
1876 // to use instead of building a string every time
1877 $strs = array(0 => ' ', 1 => ' ');
1878 for ($k = 0; $k < ($tab_width - (($i - $pos) %
$tab_width)); $k++
) $str .= $strs[$k %
2];
1879 $result_line .= $str;
1882 //$pos -= $tab_width-1;
1884 if (false
=== strpos($line, "\t", $i +
1)) {
1885 //$lines[$key] = $result_line;
1886 $result_line .= substr($line, $i +
1);
1889 } elseif ( $IN_TAG ) {
1891 $result_line .= $char;
1893 $result_line .= $char;
1897 $lines[$key] = $result_line;
1899 $result = implode("\n", $lines);
1902 $result = str_replace(' ', ' ', $result);
1903 $result = str_replace(' ', ' ', $result);
1904 $result = str_replace("\n ", "\n ", $result);
1906 if ($this->line_numbers
== GESHI_NO_LINE_NUMBERS
) {
1907 $result = nl2br($result);
1913 * Changes the case of a keyword for those languages where a change is asked for
1915 * @param string The keyword to change the case of
1916 * @return string The keyword with its case changed
1920 function change_case ($instr)
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);
1931 * Adds a url to a keyword where needed.
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 <a> tag
1939 * @todo Get rid of ender
1941 function add_url_to_keyword ($keyword, $group, $start_or_end)
1943 if (isset($this->language_data
['URLS'][$group]) &&
1944 $this->language_data
['URLS'][$group] != '' &&
1945 substr($keyword, 0, 5) != '</') {
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)) {
1958 $word = ( substr($word, 0, 4) == '<' ) ?
substr($word, 4) : $word;
1959 $word = ( substr($word, -4) == '>' ) ?
substr($word, 0, strlen($word) - 4) : $word;
1960 if (!$word) return '';
1964 array('{FNAME}', '.'),
1965 array(@htmlspecialchars($word, ENT_COMPAT
, $this->encoding
), '<DOT>'),
1966 $this->language_data
['URLS'][$group]
1970 // HTML fix. Again, dirty hackage...
1971 } elseif (!($this->language
== 'html4strict' && '>' == $keyword)) {
1978 * Takes a string that has no strings or comments in it, and highlights
1979 * stuff like keywords, numbers and methods.
1981 * @param string The string to parse for keyword, numbers etc.
1984 * @todo BUGGY! Why? Why not build string and return?
1986 function parse_non_string_part (&$stuff_to_parse)
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';
1994 // Regular expressions
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(
2001 str_replace('/', '\/', $regexp[GESHI_SEARCH
]) .
2002 "/{$regexp[GESHI_MODIFIERS]}",
2003 "{$regexp[GESHI_BEFORE]}<|!REG3XP$key!>{$regexp[GESHI_REPLACE]}|>{$regexp[GESHI_AFTER]}",
2007 $stuff_to_parse = preg_replace( "/(" . str_replace('/', '\/', $regexp) . ")/", "<|!REG3XP$key!>\\1|>", $stuff_to_parse);
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.
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);
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);
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, '/');
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
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 ;))
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')",
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')",
2064 $stuff_to_parse = substr($stuff_to_parse, 0, strlen($stuff_to_parse) - 1);
2072 // Now that's all done, replace /[number]/ with the correct styles
2074 foreach ($this->language_data
['KEYWORDS'] as $k => $kws) {
2075 if (!$this->use_classes
) {
2076 $attributes = ' style="' . $this->language_data
['STYLES']['KEYWORDS'][$k] . '"';
2078 $attributes = ' class="kw' . $k . '"';
2080 $stuff_to_parse = str_replace("/$k/", $attributes, $stuff_to_parse);
2083 // Put number styles in
2084 if (!$this->use_classes
&& $this->lexic_permissions
['NUMBERS']) {
2085 $attributes = ' style="' . $this->language_data
['STYLES']['NUMBERS'][0] . '"';
2087 $attributes = ' class="nu0"';
2089 $stuff_to_parse = str_replace('/NUM!/', $attributes, $stuff_to_parse);
2092 // Highlight methods and fields in objects
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] . '"';
2100 $attributes = ' class="me' . $key . '"';
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);
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
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] . '">[|>',
2118 '<| style="' . $this->language_data
['STYLES']['BRACKETS'][0] . '">]|>',
2119 '<| style="' . $this->language_data
['STYLES']['BRACKETS'][0] . '">(|>',
2120 '<| style="' . $this->language_data
['STYLES']['BRACKETS'][0] . '">)|>',
2121 '<| style="' . $this->language_data
['STYLES']['BRACKETS'][0] . '">{|>',
2122 '<| style="' . $this->language_data
['STYLES']['BRACKETS'][0] . '">}|>',
2125 $code_entities_replace = array(
2126 '<| class="br0">[|>',
2127 '<| class="br0">]|>',
2128 '<| class="br0">(|>',
2129 '<| class="br0">)|>',
2130 '<| class="br0">{|>',
2131 '<| class="br0">}|>',
2134 $stuff_to_parse = str_replace( $code_entities_match, $code_entities_replace, $stuff_to_parse );
2138 // Add class/style for regexps
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] . '"';
2145 $attributes = ' class="re' . $key . '"';
2147 $stuff_to_parse = str_replace("!REG3XP$key!", "$attributes", $stuff_to_parse);
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);
2158 $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target
. ' style="' . $this->link_styles
[GESHI_LINK
] . '" href=', $stuff_to_parse);
2161 $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target
. ' href=', $stuff_to_parse);
2165 // NOW we add the span thingy ;)
2168 $stuff_to_parse = str_replace('<|', '<span', $stuff_to_parse);
2169 $stuff_to_parse = str_replace ( '|>', '</span>', $stuff_to_parse );
2171 return substr($stuff_to_parse, 1);
2175 * Sets the time taken to parse the code
2177 * @param microtime The time when parsing started
2178 * @param microtime The time when parsing ended
2182 function set_time ($start_time, $end_time)
2184 $start = explode(' ', $start_time);
2185 $end = explode(' ', $end_time);
2186 $this->time
= $end[0] +
$end[1] - $start[0] - $start[1];
2190 * Gets the time taken to parse the code
2192 * @return double The time taken to parse the code
2195 function get_time ()
2201 * Gets language information and stores it for later use
2204 * @todo Needs to load keys for lexic permissions for keywords, regexps etc
2206 function load_language ($file_name)
2208 $language_data = array();
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
;
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
;
2222 foreach ($this->language_data
['COMMENT_SINGLE'] as $key => $comment) {
2223 $this->lexic_permissions
['COMMENTS'][$key] = true
;
2225 foreach ($this->language_data
['REGEXPS'] as $key => $regexp) {
2226 $this->lexic_permissions
['REGEXPS'][$key] = true
;
2228 $this->enable_highlighting();
2229 // Set default class for CSS
2230 $this->overall_class
= $this->language
;
2234 * Takes the parsed code and various options, and creates the HTML
2235 * surrounding it to make it look nice.
2237 * @param string The code already parsed
2238 * @return string The code nicely finalised
2242 function finalise ($parsed_code)
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);
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);
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
2271 foreach ($code as $line) {
2272 $line = ( $line ) ?
$line : ' ';
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"';
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
. '"';
2290 $start = "<div$def_attr>";
2293 if ($this->use_classes
) {
2294 //$attr = ' class="li1"';
2295 $attrs['class'][] = 'li1';
2296 $def_attr = ' class="de1"';
2298 //$attr = ' style="' . $this->line_style1 . '"';
2299 $attrs['style'][] = $this->line_style1
;
2300 $def_attr = ' style="' . $this->code_style
. '"';
2302 $start = "<div$def_attr>";
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";
2312 if ($this->use_classes
&& in_array($i, $this->highlight_extra_lines
)) {
2313 //$attr .= " class=\"ln-xtra\"";
2314 $attrs['class'][] = 'ln-xtra';
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
;
2321 // Add in the line surrounded by appropriate list HTML
2323 foreach ($attrs as $key => $attr) {
2324 $attr_string .= $key . '="' . implode(' ', $attr) . '"';
2326 $parsed_code .= "<li$attr_string>$start$line$end</li>$ls";
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);
2335 foreach ($code as $line)
2337 // Make lines have at least one space in them if they're empty
2338 $line = ($line) ?
$line : ' ';
2339 if (in_array(++
$i, $this->highlight_extra_lines
)) {
2340 if ($this->use_classes
) {
2341 $parsed_code .= '<div class="ln-xtra">';
2343 $parsed_code .= "<div style=\"{$this->highlight_extra_lines_style}\">";
2345 // Remove \n because it stuffs up <pre> header
2346 $parsed_code .= $line . "</div>";
2348 $parsed_code .= $line . "\n";
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);
2357 if ($this->header_type
== GESHI_HEADER_PRE
) {
2358 // enforce line numbers when using pre
2359 $parsed_code = str_replace('<li></li>', '<li> </li>', $parsed_code);
2362 return $this->header() . chop($parsed_code) . $this->footer();
2366 * Creates the header for the code block (with correct attributes)
2368 * @return string The header for the code block
2374 // Get attributes needed
2375 $attributes = $this->get_attributes();
2377 $ol_attributes = '';
2379 if ($this->line_numbers_start
!= 1) {
2380 $ol_attributes .= ' start="' . $this->line_numbers_start
. '"';
2383 // Get the header HTML
2384 $header = $this->format_header_content();
2386 if (GESHI_HEADER_NONE
== $this->header_type
) {
2387 if ($this->line_numbers
!= GESHI_NO_LINE_NUMBERS
) {
2388 return "$header<ol$ol_attributes>";
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>";
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";
2410 * Returns the header content, formatted for output
2412 * @return string The header content, formatted for output
2416 function format_header_content ()
2418 $header = $this->header_content
;
2420 if ($this->header_type
== GESHI_HEADER_PRE
) {
2421 $header = str_replace("\n", '', $header);
2423 $header = $this->replace_keywords($header);
2425 if ($this->use_classes
) {
2426 $attr = ' class="head"';
2428 $attr = " style=\"{$this->header_content_style}\"";
2430 return "<div$attr>$header</div>";
2435 * Returns the footer for the code block.
2437 * @return string The footer for the code block
2443 $footer_content = $this->format_footer_content();
2445 if (GESHI_HEADER_NONE
== $this->header_type
) {
2446 return ($this->line_numbers
!= GESHI_NO_LINE_NUMBERS
) ?
'</ol>' . $footer_content
2450 if ($this->header_type
== GESHI_HEADER_DIV
) {
2451 if ($this->line_numbers
!= GESHI_NO_LINE_NUMBERS
) {
2452 return "</ol>$footer_content</div>";
2454 return "$footer_content</div>";
2456 if ($this->line_numbers
!= GESHI_NO_LINE_NUMBERS
) {
2457 return "</ol>$footer_content</pre>";
2459 return "$footer_content</pre>";
2464 * Returns the footer content, formatted for output
2466 * @return string The footer content, formatted for output
2470 function format_footer_content ()
2472 $footer = $this->footer_content
;
2474 if ($this->header_type
== GESHI_HEADER_PRE
) {
2475 $footer = str_replace("\n", '', $footer);;
2477 $footer = $this->replace_keywords($footer);
2479 if ($this->use_classes
) {
2480 $attr = ' class="foot"';
2482 $attr = " style=\"{$this->footer_content_style}\"";
2484 return "<div$attr>$footer</div>";
2489 * Replaces certain keywords in the header and footer with
2490 * certain configuration values
2492 * @param string The header or footer content to do replacement on
2493 * @return string The header or footer with replaced keywords
2497 function replace_keywords ($instr)
2499 $keywords = $replacements = array();
2501 $keywords[] = '<TIME>';
2502 $replacements[] = number_format($this->get_time(), 3);
2504 $keywords[] = '<LANGUAGE>';
2505 $replacements[] = $this->language
;
2507 $keywords[] = '<VERSION>';
2508 $replacements[] = GESHI_VERSION
;
2510 return str_replace($keywords, $replacements, $instr);
2514 * Gets the CSS attributes for this code
2516 * @return The CSS attributes for this code
2519 * @todo Document behaviour change - class is outputted regardless of whether we're using classes or not.
2522 function get_attributes ()
2526 if ($this->overall_class
!= '') {
2527 $attributes .= " class=\"{$this->overall_class}\"";
2529 if ($this->overall_id
!= '') {
2530 $attributes .= " id=\"{$this->overall_id}\"";
2532 if ($this->overall_style
!= '') {
2533 $attributes .= ' style="' . $this->overall_style
. '"';
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
2543 * @param boolean Whether to use economy mode or not
2544 * @return string A stylesheet built on the data for the current language
2547 function get_stylesheet ($economy_mode = true
)
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...
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;
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";
2565 $stylesheet = '/* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */' . "\n";
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";
2577 // Add overall styles
2578 if (!$economy_mode ||
$this->overall_style
!= '') {
2579 $stylesheet .= "$selector {{$this->overall_style}}\n";
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";
2587 if (!$economy_mode ||
$key == GESHI_HOVER
&& $style != '') {
2588 $stylesheet .= "{$selector}a:hover {{$style}}\n";
2590 if (!$economy_mode ||
$key == GESHI_ACTIVE
&& $style != '') {
2591 $stylesheet .= "{$selector}a:active {{$style}}\n";
2593 if (!$economy_mode ||
$key == GESHI_VISITED
&& $style != '') {
2594 $stylesheet .= "{$selector}a:visited {{$style}}\n";
2598 // Header and footer
2599 if (!$economy_mode ||
$this->header_content_style
!= '') {
2600 $stylesheet .= "$selector.head {{$this->header_content_style}}\n";
2602 if (!$economy_mode ||
$this->footer_content_style
!= '') {
2603 $stylesheet .= "$selector.foot {{$this->footer_content_style}}\n";
2606 // Styles for important stuff
2607 if (!$economy_mode ||
$this->important_styles
!= '') {
2608 $stylesheet .= "$selector.imp {{$this->important_styles}}\n";
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";
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";
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";
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";
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";
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";
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";
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";
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";
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";
2667 foreach ($this->language_data
['STYLES']['SCRIPT'] as $group => $styles) {
2668 if (!$economy_mode ||
!($economy_mode && $styles == '')) {
2669 $stylesheet .= "$selector.sc$group {{$styles}}\n";
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";
2682 } // End Class GeSHi
2685 if (!function_exists('geshi_highlight')) {
2687 * Easy way to highlight stuff. Behaves just like highlight_string
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)
2697 function geshi_highlight ($string, $language, $path, $return = false
)
2699 $geshi = new GeSHi($string, $language, $path);
2700 $geshi->set_header_type(GESHI_HEADER_NONE
);
2702 return '<code>' . $geshi->parse_code() . '</code>';
2704 echo '<code>' . $geshi->parse_code() . '</code>';
2705 if ($geshi->error()) {