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.23 2005/11/19 02:23:37 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.5');
45 /** For the future (though this may never be realised) */
46 define('GESHI_OUTPUT_HTML', 0);
48 /** Set the correct directory separator */
49 define('GESHI_DIR_SEPARATOR', ('WIN' != substr(PHP_OS
, 0, 3)) ?
'/' : '\\');
51 // Define the root directory for the GeSHi code tree
52 if (!defined('GESHI_ROOT')) {
53 /** The root directory for GeSHi */
54 define('GESHI_ROOT', dirname(__FILE__
) . GESHI_DIR_SEPARATOR
);
56 /** The language file directory for GeSHi
58 define('GESHI_LANG_ROOT', GESHI_ROOT
. 'geshi' . GESHI_DIR_SEPARATOR
);
61 // Line numbers - use with enable_line_numbers()
62 /** Use no line numbers when building the result */
63 define('GESHI_NO_LINE_NUMBERS', 0);
64 /** Use normal line numbers when building the result */
65 define('GESHI_NORMAL_LINE_NUMBERS', 1);
66 /** Use fancy line numbers when building the result */
67 define('GESHI_FANCY_LINE_NUMBERS', 2);
69 // Container HTML type
70 /** Use nothing to surround the source */
71 define('GESHI_HEADER_NONE', 0);
72 /** Use a "div" to surround the source */
73 define('GESHI_HEADER_DIV', 1);
74 /** Use a "pre" to surround the source */
75 define('GESHI_HEADER_PRE', 2);
77 // Capatalisation constants
78 /** Lowercase keywords found */
79 define('GESHI_CAPS_NO_CHANGE', 0);
80 /** Uppercase keywords found */
81 define('GESHI_CAPS_UPPER', 1);
82 /** Leave keywords found as the case that they are */
83 define('GESHI_CAPS_LOWER', 2);
85 // Link style constants
86 /** Links in the source in the :link state */
87 define('GESHI_LINK', 0);
88 /** Links in the source in the :hover state */
89 define('GESHI_HOVER', 1);
90 /** Links in the source in the :active state */
91 define('GESHI_ACTIVE', 2);
92 /** Links in the source in the :visited state */
93 define('GESHI_VISITED', 3);
95 // Important string starter/finisher
96 // Note that if you change these, they should be as-is: i.e., don't
97 // write them as if they had been run through htmlentities()
98 /** The starter for important parts of the source */
99 define('GESHI_START_IMPORTANT', '<BEGIN GeSHi>');
100 /** The ender for important parts of the source */
101 define('GESHI_END_IMPORTANT', '<END GeSHi>');
106 // When strict mode applies for a language
107 /** Strict mode never applies (this is the most common) */
108 define('GESHI_NEVER', 0);
109 /** Strict mode *might* apply, and can be enabled or
110 disabled by {@link GeSHi::enable_strict_mode()} */
111 define('GESHI_MAYBE', 1);
112 /** Strict mode always applies */
113 define('GESHI_ALWAYS', 2);
115 // Advanced regexp handling constants, used in language files
116 /** The key of the regex array defining what to search for */
117 define('GESHI_SEARCH', 0);
118 /** The key of the regex array defining what bracket group in a
119 matched search to use as a replacement */
120 define('GESHI_REPLACE', 1);
121 /** The key of the regex array defining any modifiers to the regular expression */
122 define('GESHI_MODIFIERS', 2);
123 /** The key of the regex array defining what bracket group in a
124 matched search to put before the replacement */
125 define('GESHI_BEFORE', 3);
126 /** The key of the regex array defining what bracket group in a
127 matched search to put after the replacement */
128 define('GESHI_AFTER', 4);
130 /** Used in language files to mark comments */
131 define('GESHI_COMMENTS', 0);
133 // Error detection - use these to analyse faults
134 /** No sourcecode to highlight was specified */
135 define('GESHI_ERROR_NO_INPUT', 1);
136 /** The language specified does not exist */
137 define('GESHI_ERROR_NO_SUCH_LANG', 2);
138 /** GeSHi could not open a file for reading (generally a language file) */
139 define('GESHI_ERROR_FILE_NOT_READABLE', 3);
140 /** The header type passed to {@link GeSHi::set_header_type()} was invalid */
141 define('GESHI_ERROR_INVALID_HEADER_TYPE', 4);
142 /** The line number type passed to {@link GeSHi::enable_line_numbers()} was invalid */
143 define('GESHI_ERROR_INVALID_LINE_NUMBER_TYPE', 5);
150 * Please refer to the documentation for GeSHi 1.0.X that is available
151 * at http://qbnz.com/highlighter/documentation.php for more information
152 * about how to use this class.
155 * @author Nigel McNie <nigel@geshi.org>
156 * @copyright Copyright © 2004, 2005 Nigel McNie
164 * The source code to highlight
170 * The language to use when highlighting
176 * The data for the language used
179 var $language_data = array();
182 * The path to the language files
185 var $language_path = GESHI_LANG_ROOT
;
188 * The error message associated with an error
190 * @todo check err reporting works
195 * Possible error messages
198 var $error_messages = array(
199 GESHI_ERROR_NO_INPUT
=> 'No source code inputted',
200 GESHI_ERROR_NO_SUCH_LANG
=> 'GeSHi could not find the language {LANGUAGE} (using path {PATH})',
201 GESHI_ERROR_FILE_NOT_READABLE
=> 'The file specified for load_from_file was not readable',
202 GESHI_ERROR_INVALID_HEADER_TYPE
=> 'The header type specified is invalid',
203 GESHI_ERROR_INVALID_LINE_NUMBER_TYPE
=> 'The line number type specified is invalid'
207 * Whether highlighting is strict or not
210 var $strict_mode = false
;
213 * Whether to use CSS classes in output
216 var $use_classes = false
;
219 * The type of header to use. Can be one of the following
223 * <li><b>GESHI_HEADER_PRE</b>: Source is outputted in
224 * a <pre> HTML element.</li>
225 * <li><b>GESHI_HEADER_DIV</b>: Source is outputted in
226 * a <div> HTML element.</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';
395 * Unused (planned for future)
398 var $output_format = GESHI_OUTPUT_HTML
;
403 * Creates a new GeSHi object, with source and language
405 * @param string The source code to highlight
406 * @param string The language to highlight the source with
407 * @param string The path to the language file directory. <b>This
408 * is deprecated!</b> I've backported the auto path
409 * detection from the 1.1.X dev branch, so now it
410 * should be automatically set correctly. If you have
411 * renamed the language directory however, you will
412 * still need to set the path using this parameter or
413 * {@link GeSHi::set_language_path()}
416 function GeSHi ($source, $language, $path = '')
418 $this->set_source($source);
419 $this->set_language_path($path);
420 $this->set_language($language);
424 * Returns an error message associated with the last GeSHi operation,
425 * or false if no error has occured
427 * @return string|false An error message if there has been an error, else false
433 $msg = $this->error_messages
[$this->error
];
434 $debug_tpl_vars = array(
435 '{LANGUAGE}' => $this->language
,
436 '{PATH}' => $this->language_path
438 foreach ($debug_tpl_vars as $tpl => $var) {
439 $msg = str_replace($tpl, $var, $msg);
441 return "<br /><strong>GeSHi Error:</strong> $msg (code $this->error)<br />";
447 * Gets a human-readable language name (thanks to Simon Patterson
450 * @return string The name for the current language
453 function get_language_name ()
455 if (GESHI_ERROR_NO_SUCH_LANG
== $this->_error
) {
456 return $this->language_data
['LANG_NAME'] . ' (Unknown Language)';
458 return $this->language_data
['LANG_NAME'];
462 * Sets the source code for this object
464 * @param string The source code to highlight
467 function set_source ($source)
469 if ('' == trim($source)) {
470 $this->error
= GESHI_ERROR_NO_INPUT
;
472 $this->source
= $source;
476 * Sets the language for this object
478 * @param string The name of the language to use
481 function set_language ($language)
483 $this->error
= false
;
484 $this->strict_mode
= GESHI_NEVER
;
486 $language = preg_replace('#[^a-zA-Z0-9\-_]#', '', $language);
487 $this->language
= strtolower($language);
489 $file_name = $this->language_path
. $this->language
. '.php';
490 if (!is_readable($file_name)) {
491 $this->error
= GESHI_ERROR_NO_SUCH_LANG
;
494 // Load the language for parsing
495 $this->load_language($file_name);
499 * Sets the path to the directory containing the language files. Note
500 * that this path is relative to the directory of the script that included
501 * geshi.php, NOT geshi.php itself.
503 * @param string The path to the language directory
505 * @deprecated The path to the language files should now be automatically
506 * detected, so this method should no longer be needed. The
507 * 1.1.X branch handles manual setting of the path differently
508 * so this method will disappear in 1.2.0.
510 function set_language_path ($path)
513 $this->language_path
= ('/' == substr($path, strlen($path) - 1, 1)) ?
$path : $path . '/';
518 * Sets the type of header to be used.
520 * If GESHI_HEADER_DIV is used, the code is surrounded in a "div".This
521 * means more source code but more control over tab width and line-wrapping.
522 * GESHI_HEADER_PRE means that a "pre" is used - less source, but less
523 * control. Default is GESHI_HEADER_PRE.
525 * From 1.0.7.2, you can use GESHI_HEADER_NONE to specify that no header code
526 * should be outputted.
528 * @param int The type of header to be used
531 function set_header_type ($type)
533 if (GESHI_HEADER_DIV
!= $type && GESHI_HEADER_PRE
!= $type && GESHI_HEADER_NONE
!= $type) {
534 $this->error
= GESHI_ERROR_INVALID_HEADER_TYPE
;
537 $this->header_type
= $type;
541 * Sets the styles for the code that will be outputted
542 * when this object is parsed. The style should be a
543 * string of valid stylesheet declarations
545 * @param string The overall style for the outputted code block
546 * @param boolean Whether to merge the styles with the current styles or not
549 function set_overall_style ($style, $preserve_defaults = false
)
551 if (!$preserve_defaults) {
552 $this->overall_style
= $style;
554 $this->overall_style
.= $style;
559 * Sets the overall classname for this block of code. This
560 * class can then be used in a stylesheet to style this object's
563 * @param string The class name to use for this block of code
566 function set_overall_class ($class)
568 $this->overall_class
= $class;
572 * Sets the overall id for this block of code. This id can then
573 * be used in a stylesheet to style this object's output
575 * @param string The ID to use for this block of code
578 function set_overall_id ($id)
580 $this->overall_id
= $id;
584 * Sets whether CSS classes should be used to highlight the source. Default
585 * is off, calling this method with no arguments will turn it on
587 * @param boolean Whether to turn classes on or not
590 function enable_classes ($flag = true
)
592 $this->use_classes
= ($flag) ? true
: false
;
596 * Sets the style for the actual code. This should be a string
597 * containing valid stylesheet declarations. If $preserve_defaults is
598 * true, then styles are merged with the default styles, with the
599 * user defined styles having priority
601 * Note: Use this method to override any style changes you made to
602 * the line numbers if you are using line numbers, else the line of
603 * code will have the same style as the line number! Consult the
604 * GeSHi documentation for more information about this.
606 * @param string The style to use for actual code
607 * @param boolean Whether to merge the current styles with the new styles
609 function set_code_style ($style, $preserve_defaults = false
)
611 if (!$preserve_defaults) {
612 $this->code_style
= $style;
614 $this->code_style
.= $style;
619 * Sets the styles for the line numbers.
621 * @param string The style for the line numbers that are "normal"
622 * @param string|boolean If a string, this is the style of the line
623 * numbers that are "fancy", otherwise if boolean then this
624 * defines whether the normal styles should be merged with the
625 * new normal styles or not
626 * @param boolean If set, is the flag for whether to merge the "fancy"
627 * styles with the current styles or not
630 function set_line_style ($style1, $style2 = '', $preserve_defaults = false
)
632 if (is_bool($style2)) {
633 $preserve_defaults = $style2;
636 if (!$preserve_defaults) {
637 $this->line_style1
= $style1;
638 $this->line_style2
= $style2;
640 $this->line_style1
.= $style1;
641 $this->line_style2
.= $style2;
646 * Sets whether line numbers should be displayed.
648 * Valid values for the first parameter are:
651 * <li><b>GESHI_NO_LINE_NUMBERS</b>: Line numbers will not be displayed</li>
652 * <li><b>GESHI_NORMAL_LINE_NUMBERS</b>: Line numbers will be displayed</li>
653 * <li><b>GESHI_FANCY_LINE_NUMBERS</b>: Fancy line numbers will be displayed</li>
656 * For fancy line numbers, the second parameter is used to signal which lines
657 * are to be fancy. For example, if the value of this parameter is 5 then every
658 * 5th line will be fancy.
660 * @param int How line numbers should be displayed
661 * @param int Defines which lines are fancy
664 function enable_line_numbers ($flag, $nth_row = 5)
666 if (GESHI_NO_LINE_NUMBERS
!= $flag && GESHI_NORMAL_LINE_NUMBERS
!= $flag
667 && GESHI_FANCY_LINE_NUMBERS
!= $flag) {
668 $this->error
= GESHI_ERROR_INVALID_LINE_NUMBER_TYPE
;
670 $this->line_numbers
= $flag;
671 $this->line_nth_row
= $nth_row;
675 * Sets the style for a keyword group. If $preserve_defaults is
676 * true, then styles are merged with the default styles, with the
677 * user defined styles having priority
679 * @param int The key of the keyword group to change the styles of
680 * @param string The style to make the keywords
681 * @param boolean Whether to merge the new styles with the old or just
685 function set_keyword_group_style ($key, $style, $preserve_defaults = false
)
687 if (!$preserve_defaults) {
688 $this->language_data
['STYLES']['KEYWORDS'][$key] = $style;
690 $this->language_data
['STYLES']['KEYWORDS'][$key] .= $style;
695 * Turns highlighting on/off for a keyword group
697 * @param int The key of the keyword group to turn on or off
698 * @param boolean Whether to turn highlighting for that group on or off
701 function set_keyword_group_highlighting ( $key, $flag = true
)
703 $this->lexic_permissions
['KEYWORDS'][$key] = ($flag) ? true
: false
;
707 * Sets the styles for comment groups. If $preserve_defaults is
708 * true, then styles are merged with the default styles, with the
709 * user defined styles having priority
711 * @param int The key of the comment group to change the styles of
712 * @param string The style to make the comments
713 * @param boolean Whether to merge the new styles with the old or just
717 function set_comments_style ($key, $style, $preserve_defaults = false
)
719 if (!$preserve_defaults) {
720 $this->language_data
['STYLES']['COMMENTS'][$key] = $style;
722 $this->language_data
['STYLES']['COMMENTS'][$key] .= $style;
727 * Turns highlighting on/off for comment groups
729 * @param int The key of the comment group to turn on or off
730 * @param boolean Whether to turn highlighting for that group on or off
733 function set_comments_highlighting ($key, $flag = true
)
735 $this->lexic_permissions
['COMMENTS'][$key] = ($flag) ? true
: false
;
739 * Sets the styles for escaped characters. If $preserve_defaults is
740 * true, then styles are merged with the default styles, with the
741 * user defined styles having priority
743 * @param string The style to make the escape characters
744 * @param boolean Whether to merge the new styles with the old or just
748 function set_escape_characters_style ($style, $preserve_defaults = false
)
750 if (!$preserve_defaults) {
751 $this->language_data
['STYLES']['ESCAPE_CHAR'][0] = $style;
753 $this->language_data
['STYLES']['ESCAPE_CHAR'][0] .= $style;
758 * Turns highlighting on/off for escaped characters
760 * @param boolean Whether to turn highlighting for escape characters on or off
763 function set_escape_characters_highlighting ($flag = true
)
765 $this->lexic_permissions
['ESCAPE_CHAR'] = ($flag) ? true
: false
;
769 * Sets the styles for brackets. If $preserve_defaults is
770 * true, then styles are merged with the default styles, with the
771 * user defined styles having priority
773 * This method is DEPRECATED: use set_symbols_style instead.
774 * This method will be removed in 1.2.X
776 * @param string The style to make the brackets
777 * @param boolean Whether to merge the new styles with the old or just
780 * @deprecated In favour of set_symbols_style
782 function set_brackets_style ($style, $preserve_defaults = false
)
784 if (!$preserve_defaults) {
785 $this->language_data
['STYLES']['BRACKETS'][0] = $style;
787 $this->language_data
['STYLES']['BRACKETS'][0] .= $style;
792 * Turns highlighting on/off for brackets
794 * This method is DEPRECATED: use set_symbols_highlighting instead.
795 * This method will be remove in 1.2.X
797 * @param boolean Whether to turn highlighting for brackets on or off
799 * @deprecated In favour of set_symbols_highlighting
801 function set_brackets_highlighting ($flag)
803 $this->lexic_permissions
['BRACKETS'] = ($flag) ? true
: false
;
807 * Sets the styles for symbols. If $preserve_defaults is
808 * true, then styles are merged with the default styles, with the
809 * user defined styles having priority
811 * @param string The style to make the symbols
812 * @param boolean Whether to merge the new styles with the old or just
816 function set_symbols_style ($style, $preserve_defaults = false
)
818 if (!$preserve_defaults) {
819 $this->language_data
['STYLES']['SYMBOLS'][0] = $style;
821 $this->language_data
['STYLES']['SYMBOLS'][0] .= $style;
823 // For backward compatibility
824 $this->set_brackets_style ($style, $preserve_defaults);
828 * Turns highlighting on/off for symbols
830 * @param boolean Whether to turn highlighting for symbols on or off
833 function set_symbols_highlighting ($flag)
835 $this->lexic_permissions
['SYMBOLS'] = ($flag) ? true
: false
;
836 // For backward compatibility
837 $this->set_brackets_highlighting ($flag);
841 * Sets the styles for strings. If $preserve_defaults is
842 * true, then styles are merged with the default styles, with the
843 * user defined styles having priority
845 * @param string The style to make the escape characters
846 * @param boolean Whether to merge the new styles with the old or just
850 function set_strings_style ($style, $preserve_defaults = false
)
852 if (!$preserve_defaults) {
853 $this->language_data
['STYLES']['STRINGS'][0] = $style;
855 $this->language_data
['STYLES']['STRINGS'][0] .= $style;
860 * Turns highlighting on/off for strings
862 * @param boolean Whether to turn highlighting for strings on or off
865 function set_strings_highlighting ($flag)
867 $this->lexic_permissions
['STRINGS'] = ($flag) ? true
: false
;
871 * Sets the styles for numbers. If $preserve_defaults is
872 * true, then styles are merged with the default styles, with the
873 * user defined styles having priority
875 * @param string The style to make the numbers
876 * @param boolean Whether to merge the new styles with the old or just
880 function set_numbers_style ($style, $preserve_defaults = false
)
882 if (!$preserve_defaults) {
883 $this->language_data
['STYLES']['NUMBERS'][0] = $style;
885 $this->language_data
['STYLES']['NUMBERS'][0] .= $style;
890 * Turns highlighting on/off for numbers
892 * @param boolean Whether to turn highlighting for numbers on or off
895 function set_numbers_highlighting ($flag)
897 $this->lexic_permissions
['NUMBERS'] = ($flag) ? true
: false
;
901 * Sets the styles for methods. $key is a number that references the
902 * appropriate "object splitter" - see the language file for the language
903 * you are highlighting to get this number. If $preserve_defaults is
904 * true, then styles are merged with the default styles, with the
905 * user defined styles having priority
907 * @param int The key of the object splitter to change the styles of
908 * @param string The style to make the methods
909 * @param boolean Whether to merge the new styles with the old or just
913 function set_methods_style ($key, $style, $preserve_defaults = false
)
915 if (!$preserve_defaults) {
916 $this->language_data
['STYLES']['METHODS'][$key] = $style;
918 $this->language_data
['STYLES']['METHODS'][$key] .= $style;
923 * Turns highlighting on/off for methods
925 * @param boolean Whether to turn highlighting for methods on or off
928 function set_methods_highlighting ($flag)
930 $this->lexic_permissions
['METHODS'] = ($flag) ? true
: false
;
934 * Sets the styles for regexps. If $preserve_defaults is
935 * true, then styles are merged with the default styles, with the
936 * user defined styles having priority
938 * @param string The style to make the regular expression matches
939 * @param boolean Whether to merge the new styles with the old or just
943 function set_regexps_style ($key, $style, $preserve_defaults = false
)
945 if (!$preserve_defaults) {
946 $this->language_data
['STYLES']['REGEXPS'][$key] = $style;
948 $this->language_data
['STYLES']['REGEXPS'][$key] .= $style;
953 * Turns highlighting on/off for regexps
955 * @param int The key of the regular expression group to turn on or off
956 * @param boolean Whether to turn highlighting for the regular expression group on or off
959 function set_regexps_highlighting ($key, $flag)
961 $this->lexic_permissions
['REGEXPS'][$key] = ($flag) ? true
: false
;
965 * Sets whether a set of keywords are checked for in a case sensitive manner
967 * @param int The key of the keyword group to change the case sensitivity of
968 * @param boolean Whether to check in a case sensitive manner or not
971 function set_case_sensitivity ($key, $case)
973 $this->language_data
['CASE_SENSITIVE'][$key] = ($case) ? true
: false
;
977 * Sets the case that keywords should use when found. Use the constants:
980 * <li><b>GESHI_CAPS_NO_CHANGE</b>: leave keywords as-is</li>
981 * <li><b>GESHI_CAPS_UPPER</b>: convert all keywords to uppercase where found</li>
982 * <li><b>GESHI_CAPS_LOWER</b>: convert all keywords to lowercase where found</li>
985 * @param int A constant specifying what to do with matched keywords
987 * @todo Error check the passed value
989 function set_case_keywords ($case)
991 $this->language_data
['CASE_KEYWORDS'] = $case;
995 * Sets how many spaces a tab is substituted for
997 * Widths below zero are ignored
999 * @param int The tab width
1002 function set_tab_width ($width)
1004 $this->tab_width
= intval($width);
1008 * Enables/disables strict highlighting. Default is off, calling this
1009 * method without parameters will turn it on. See documentation
1010 * for more details on strict mode and where to use it.
1012 * @param boolean Whether to enable strict mode or not
1015 function enable_strict_mode ($mode = true
)
1017 if (GESHI_MAYBE
== $this->language_data
['STRICT_MODE_APPLIES']) {
1018 $this->strict_mode
= ($mode) ? true
: false
;
1023 * Disables all highlighting
1026 * @todo Rewrite with an array traversal
1028 function disable_highlighting ()
1030 foreach ($this->lexic_permissions
as $key => $value) {
1031 if (is_array($value)) {
1032 foreach ($value as $k => $v) {
1033 $this->lexic_permissions
[$key][$k] = false
;
1036 $this->lexic_permissions
[$key] = false
;
1040 $this->enable_important_blocks
= false
;
1044 * Enables all highlighting
1047 * @todo Rewrite with array traversal
1049 function enable_highlighting ()
1051 foreach ($this->lexic_permissions
as $key => $value) {
1052 if (is_array($value)) {
1053 foreach ($value as $k => $v) {
1054 $this->lexic_permissions
[$key][$k] = true
;
1057 $this->lexic_permissions
[$key] = true
;
1061 $this->enable_important_blocks
= true
;
1065 * Given a file extension, this method returns either a valid geshi language
1066 * name, or the empty string if it couldn't be found
1068 * @param string The extension to get a language name for
1069 * @param array A lookup array to use instead of the default
1071 * @todo Re-think about how this method works (maybe make it private and/or make it
1072 * a extension->lang lookup?)
1075 function get_language_name_from_extension ( $extension, $lookup = array() )
1080 'actionscript' => array('as'),
1081 'ada' => array('a', 'ada', 'adb', 'ads'),
1082 'apache' => array('conf'),
1083 'asm' => array('ash', 'asm'),
1084 'asp' => array('asp'),
1085 'bash' => array('sh'),
1087 'c_mac' => array('c'),
1088 'caddcl' => array(),
1089 'cadlisp' => array(),
1090 'cpp' => array('cpp'),
1091 'csharp' => array(),
1092 'css' => array('css'),
1093 'delphi' => array('dpk', 'dpr'),
1094 'html4strict' => array('html', 'htm'),
1095 'java' => array('java'),
1096 'javascript' => array('js'),
1097 'lisp' => array('lisp'),
1098 'lua' => array('lua'),
1103 'oracle8' => array(),
1104 'pascal' => array('pas'),
1105 'perl' => array('pl', 'pm'),
1106 'php' => array('php', 'php5', 'phtml', 'phps'),
1107 'python' => array('py'),
1108 'qbasic' => array('bi'),
1109 'smarty' => array(),
1110 'vb' => array('bas'),
1112 'visualfoxpro' => array(),
1113 'xml' => array('xml')
1117 foreach ($lookup as $lang => $extensions) {
1118 foreach ($extensions as $ext) {
1119 if ($ext == $extension) {
1128 * Given a file name, this method loads its contents in, and attempts
1129 * to set the language automatically. An optional lookup table can be
1130 * passed for looking up the language name. If not specified a default
1133 * The language table is in the form
1135 * 'lang_name' => array('extension', 'extension', ...),
1139 * @todo Complete rethink of this and above method
1142 function load_from_file ($file_name, $lookup = array())
1144 if (is_readable($file_name)) {
1145 $this->set_source(implode('', file($file_name)));
1146 $this->set_language($this->get_language_name_from_extension(substr(strrchr($file_name, '.'), 1), $lookup));
1148 $this->error
= GESHI_ERROR_FILE_NOT_READABLE
;
1153 * Adds a keyword to a keyword group for highlighting
1155 * @param int The key of the keyword group to add the keyword to
1156 * @param string The word to add to the keyword group
1159 function add_keyword ($key, $word)
1161 $this->language_data
['KEYWORDS'][$key][] = $word;
1165 * Removes a keyword from a keyword group
1167 * @param int The key of the keyword group to remove the keyword from
1168 * @param string The word to remove from the keyword group
1171 function remove_keyword ($key, $word)
1173 $this->language_data
['KEYWORDS'][$key] =
1174 array_diff($this->language_data
['KEYWORDS'][$key], array($word));
1178 * Creates a new keyword group
1180 * @param int The key of the keyword group to create
1181 * @param string The styles for the keyword group
1182 * @param boolean Whether the keyword group is case sensitive ornot
1183 * @param array The words to use for the keyword group
1186 function add_keyword_group ( $key, $styles, $case_sensitive = true
, $words = array() )
1188 $words = (array) $words;
1189 $this->language_data
['KEYWORDS'][$key] = $words;
1190 $this->lexic_permissions
['KEYWORDS'][$key] = true
;
1191 $this->language_data
['CASE_SENSITIVE'][$key] = $case_sensitive;
1192 $this->language_data
['STYLES']['KEYWORDS'][$key] = $styles;
1196 * Removes a keyword group
1198 * @param int The key of the keyword group to remove
1201 function remove_keyword_group ($key)
1203 unset($this->language_data
['KEYWORDS'][$key]);
1204 unset($this->lexic_permissions
['KEYWORDS'][$key]);
1205 unset($this->language_data
['CASE_SENSITIVE'][$key]);
1206 unset($this->language_data
['STYLES']['KEYWORDS'][$key]);
1210 * Sets the content of the header block
1212 * @param string The content of the header block
1215 function set_header_content ($content)
1217 $this->header_content
= $content;
1221 * Sets the content of the footer block
1223 * @param string The content of the footer block
1226 function set_footer_content ($content)
1228 $this->footer_content
= $content;
1232 * Sets the style for the header content
1234 * @param string The style for the header content
1237 function set_header_content_style ($style)
1239 $this->header_content_style
= $style;
1243 * Sets the style for the footer content
1245 * @param string The style for the footer content
1248 function set_footer_content_style ($style)
1250 $this->footer_content_style
= $style;
1254 * Sets the base URL to be used for keywords
1256 * @param int The key of the keyword group to set the URL for
1257 * @param string The URL to set for the group. If {FNAME} is in
1258 * the url somewhere, it is replaced by the keyword
1259 * that the URL is being made for
1262 function set_url_for_keyword_group ($group, $url)
1264 $this->language_data
['URLS'][$group] = $url;
1268 * Sets styles for links in code
1270 * @param int A constant that specifies what state the style is being
1271 * set for - e.g. :hover or :visited
1272 * @param string The styles to use for that state
1275 function set_link_styles ($type, $styles)
1277 $this->link_styles
[$type] = $styles;
1281 * Sets the target for links in code
1283 * @param string The target for links in the code, e.g. _blank
1286 function set_link_target ( $target )
1289 $this->link_target
= '';
1291 $this->link_target
= ' target="' . $target . '" ';
1296 * Sets styles for important parts of the code
1298 * @param string The styles to use on important parts of the code
1301 function set_important_styles ($styles)
1303 $this->important_styles
= $styles;
1307 * Sets whether context-important blocks are highlighted
1309 * @todo REMOVE THIS SHIZ FROM GESHI!
1312 function enable_important_blocks ( $flag )
1314 $this->enable_important_blocks
= ( $flag ) ? true
: false
;
1318 * Whether CSS IDs should be added to each line
1320 * @param boolean If true, IDs will be added to each line.
1323 function enable_ids ($flag = true
)
1325 $this->add_ids
= ($flag) ? true
: false
;
1329 * Specifies which lines to highlight extra
1331 * @param mixed An array of line numbers to highlight, or just a line
1332 * number on its own.
1334 * @todo Some data replication here that could be cut down on
1336 function highlight_lines_extra ($lines)
1338 if (is_array($lines)) {
1339 foreach ($lines as $line) {
1340 $this->highlight_extra_lines
[intval($line)] = intval($line);
1343 $this->highlight_extra_lines
[intval($lines)] = intval($lines);
1348 * Sets the style for extra-highlighted lines
1350 * @param string The style for extra-highlighted lines
1353 function set_highlight_lines_extra_style ($styles)
1355 $this->highlight_extra_lines_style
= $styles;
1359 * Sets what number line numbers should start at. Should
1360 * be a positive integer, and will be converted to one.
1362 * <b>Warning:</b> Using this method will add the "start"
1363 * attribute to the <ol> that is used for line numbering.
1364 * This is <b>not</b> valid XHTML strict, so if that's what you
1365 * care about then don't use this method. Firefox is getting
1366 * support for the CSS method of doing this in 1.1 and Opera
1367 * has support for the CSS method, but (of course) IE doesn't
1368 * so it's not worth doing it the CSS way yet.
1370 * @param int The number to start line numbers at
1373 function start_line_numbers_at ($number)
1375 $this->line_numbers_start
= abs(intval($number));
1379 * Sets the encoding used for htmlspecialchars(), for international
1382 * @param string The encoding to use for the source
1385 function set_encoding ($encoding)
1388 $this->encoding
= $encoding;
1393 * Returns the code in $this->source, highlighted and surrounded by the
1396 * This should only be called ONCE, cos it's SLOW! If you want to highlight
1397 * the same source multiple times, you're better off doing a whole lot of
1398 * str_replaces to replace the <span>s
1402 function parse_code ()
1405 $start_time = microtime();
1407 // Firstly, if there is an error, we won't highlight
1409 $result = $this->header();
1410 if ($this->header_type
!= GESHI_HEADER_PRE
) {
1411 $result .= $this->indent(@htmlspecialchars($this->source
, ENT_COMPAT
, $this->encoding
));
1413 $result .= @htmlspecialchars($this->source
, ENT_COMPAT
, $this->encoding
);
1416 $this->set_time($start_time, microtime());
1417 return $result . $this->footer();
1420 // Add spaces for regular expression matching and line numbers
1421 $code = ' ' . $this->source
. ' ';
1422 // Replace all newlines to a common form.
1423 $code = str_replace("\r\n", "\n", $code);
1424 $code = str_replace("\r", "\n", $code);
1426 // Initialise various stuff
1427 $length = strlen($code);
1429 $CLOSE_STRING = false
;
1430 $ESCAPE_CHAR_OPEN = false
;
1431 $COMMENT_MATCHED = false
;
1432 // Turn highlighting on if strict mode doesn't apply to this language
1433 $HIGHLIGHTING_ON = ( !$this->strict_mode
) ? true
: '';
1434 // Whether to highlight inside a block of code
1435 $HIGHLIGHT_INSIDE_STRICT = false
;
1436 $stuff_to_parse = '';
1439 // "Important" selections are handled like multiline comments
1440 // @todo GET RID OF THIS SHIZ
1441 if ($this->enable_important_blocks
) {
1442 $this->language_data
['COMMENT_MULTI'][GESHI_START_IMPORTANT
] = GESHI_END_IMPORTANT
;
1445 if ($this->strict_mode
) {
1446 // Break the source into bits. Each bit will be a portion of the code
1447 // within script delimiters - for example, HTML between < and >
1448 $parts = array(0 => array(0 => ''));
1450 for ($i = 0; $i < $length; $i++
) {
1451 $char = substr($code, $i, 1);
1452 if (!$HIGHLIGHTING_ON) {
1453 foreach ($this->language_data
['SCRIPT_DELIMITERS'] as $key => $delimiters) {
1454 foreach ($delimiters as $open => $close) {
1455 // Get the next little bit for this opening string
1456 $check = substr($code, $i, strlen($open));
1458 if ($check == $open) {
1459 // We start a new block with the highlightable
1461 $HIGHLIGHTING_ON = $open;
1462 $i +
= strlen($open) - 1;
1464 $parts[++
$k][0] = $char;
1466 // No point going around again...
1472 foreach ($this->language_data
['SCRIPT_DELIMITERS'] as $key => $delimiters) {
1473 foreach ($delimiters as $open => $close) {
1474 if ($open == $HIGHLIGHTING_ON) {
1475 // Found the closing tag
1480 // We check code from our current position BACKWARDS. This is so
1481 // the ending string for highlighting can be included in the block
1482 $check = substr($code, $i - strlen($close) +
1, strlen($close));
1483 if ($check == $close) {
1484 $HIGHLIGHTING_ON = '';
1485 // Add the string to the rest of the string for this part
1486 $parts[$k][1] = ( isset($parts[$k][1]) ) ?
$parts[$k][1] . $char : $char;
1487 $parts[++
$k][0] = '';
1491 $parts[$k][1] = ( isset($parts[$k][1]) ) ?
$parts[$k][1] . $char : $char;
1493 $HIGHLIGHTING_ON = '';
1495 // Not strict mode - simply dump the source into
1496 // the array at index 1 (the first highlightable block)
1505 // Now we go through each part. We know that even-indexed parts are
1506 // code that shouldn't be highlighted, and odd-indexed parts should
1508 foreach ($parts as $key => $data) {
1510 // If this block should be highlighted...
1512 if ($this->strict_mode
) {
1513 // Find the class key for this block of code
1514 foreach ($this->language_data
['SCRIPT_DELIMITERS'] as $script_key => $script_data) {
1515 foreach ($script_data as $open => $close) {
1516 if ($data[0] == $open) {
1522 if ($this->language_data
['STYLES']['SCRIPT'][$script_key] != '' &&
1523 $this->lexic_permissions
['SCRIPT']) {
1524 // Add a span element around the source to
1525 // highlight the overall source block
1526 if (!$this->use_classes
&&
1527 $this->language_data
['STYLES']['SCRIPT'][$script_key] != '') {
1528 $attributes = ' style="' . $this->language_data
['STYLES']['SCRIPT'][$script_key] . '"';
1530 $attributes = ' class="sc' . $script_key . '"';
1532 $result .= "<span$attributes>";
1536 if (!$this->strict_mode ||
$this->language_data
['HIGHLIGHT_STRICT_BLOCK'][$script_key]) {
1537 // Now, highlight the code in this block. This code
1538 // is really the engine of GeSHi (along with the method
1539 // parse_non_string_part).
1540 $length = strlen($part);
1541 for ($i = 0; $i < $length; $i++
) {
1542 // Get the next char
1543 $char = substr($part, $i, 1);
1544 // Is this char the newline and line numbers being used?
1545 if (($this->line_numbers
!= GESHI_NO_LINE_NUMBERS
1546 ||
count($this->highlight_extra_lines
) > 0)
1548 // If so, is there a string open? If there is, we should end it before
1549 // the newline and begin it again (so when <li>s are put in the source
1550 // remains XHTML compliant)
1551 // note to self: This opens up possibility of config files specifying
1552 // that languages can/cannot have multiline strings???
1554 if (!$this->use_classes
) {
1555 $attributes = ' style="' . $this->language_data
['STYLES']['STRINGS'][0] . '"';
1557 $attributes = ' class="st0"';
1559 $char = '</span>' . $char . "<span$attributes>";
1561 } elseif ($char == $STRING_OPEN) {
1562 // A match of a string delimiter
1563 if (($this->lexic_permissions
['ESCAPE_CHAR'] && $ESCAPE_CHAR_OPEN) ||
1564 ($this->lexic_permissions
['STRINGS'] && !$ESCAPE_CHAR_OPEN)) {
1567 if (!$ESCAPE_CHAR_OPEN) {
1569 $CLOSE_STRING = true
;
1571 $ESCAPE_CHAR_OPEN = false
;
1572 } elseif (in_array($char, $this->language_data
['QUOTEMARKS']) &&
1573 ($STRING_OPEN == '') && $this->lexic_permissions
['STRINGS']) {
1574 // The start of a new string
1575 $STRING_OPEN = $char;
1576 if (!$this->use_classes
) {
1577 $attributes = ' style="' . $this->language_data
['STYLES']['STRINGS'][0] . '"';
1579 $attributes = ' class="st0"';
1581 $char = "<span$attributes>" . $char;
1583 $result .= $this->parse_non_string_part( $stuff_to_parse );
1584 $stuff_to_parse = '';
1585 } elseif (($char == $this->language_data
['ESCAPE_CHAR']) && ($STRING_OPEN != '')) {
1586 // An escape character
1587 if (!$ESCAPE_CHAR_OPEN) {
1588 $ESCAPE_CHAR_OPEN = true
;
1589 if ($this->lexic_permissions
['ESCAPE_CHAR']) {
1590 if (!$this->use_classes
) {
1591 $attributes = ' style="' . $this->language_data
['STYLES']['ESCAPE_CHAR'][0] . '"';
1593 $attributes = ' class="es0"';
1595 $char = "<span$attributes>" . $char;
1596 if (substr($code, $i +
1, 1) == "\n") {
1597 // escaping a newline, what's the point in putting the span around
1598 // the newline? It only causes hassles when inserting line numbers
1600 $ESCAPE_CHAR_OPEN = false
;
1604 $ESCAPE_CHAR_OPEN = false
;
1605 if ($this->lexic_permissions
['ESCAPE_CHAR']) {
1609 } elseif ($ESCAPE_CHAR_OPEN) {
1610 if ($this->lexic_permissions
['ESCAPE_CHAR']) {
1613 $ESCAPE_CHAR_OPEN = false
;
1615 } elseif ($STRING_OPEN == '') {
1616 // Is this a multiline comment?
1617 foreach ($this->language_data
['COMMENT_MULTI'] as $open => $close) {
1618 $com_len = strlen($open);
1619 $test_str = substr( $part, $i, $com_len );
1620 $test_str_match = $test_str;
1621 if ($open == $test_str) {
1622 $COMMENT_MATCHED = true
;
1623 //@todo If remove important do remove here
1624 if ($this->lexic_permissions
['COMMENTS']['MULTI'] ||
1625 $test_str == GESHI_START_IMPORTANT
) {
1626 if ($test_str != GESHI_START_IMPORTANT
) {
1627 if (!$this->use_classes
) {
1628 $attributes = ' style="' . $this->language_data
['STYLES']['COMMENTS']['MULTI'] . '"';
1630 $attributes = ' class="coMULTI"';
1632 $test_str = "<span$attributes>" . @htmlspecialchars($test_str, ENT_COMPAT
, $this->encoding
);
1634 if (!$this->use_classes
) {
1635 $attributes = ' style="' . $this->important_styles
. '"';
1637 $attributes = ' class="imp"';
1639 // We don't include the start of the comment if it's an
1641 $test_str = "<span$attributes>";
1644 $test_str = @htmlspecialchars($test_str, ENT_COMPAT
, $this->encoding
);
1647 $close_pos = strpos( $part, $close, $i +
strlen($close) );
1649 if ($close_pos === false
) {
1650 $close_pos = strlen($part);
1653 // Short-cut through all the multiline code
1654 $rest_of_comment = @htmlspecialchars(substr($part, $i +
$com_len, $close_pos - $i), ENT_COMPAT
, $this->encoding
);
1655 if (($this->lexic_permissions
['COMMENTS']['MULTI'] ||
1656 $test_str_match == GESHI_START_IMPORTANT
) &&
1657 ($this->line_numbers
!= GESHI_NO_LINE_NUMBERS ||
1658 count($this->highlight_extra_lines
) > 0)) {
1659 // strreplace to put close span and open span around multiline newlines
1660 $test_str .= str_replace("\n", "</span>\n<span$attributes>", $rest_of_comment);
1662 $test_str .= $rest_of_comment;
1665 if ($this->lexic_permissions
['COMMENTS']['MULTI'] ||
1666 $test_str_match == GESHI_START_IMPORTANT
) {
1667 $test_str .= '</span>';
1669 $i = $close_pos +
$com_len - 1;
1671 $result .= $this->parse_non_string_part($stuff_to_parse);
1672 $stuff_to_parse = '';
1676 // If we haven't matched a multiline comment, try single-line comments
1677 if (!$COMMENT_MATCHED) {
1678 foreach ($this->language_data
['COMMENT_SINGLE'] as $comment_key => $comment_mark) {
1679 $com_len = strlen($comment_mark);
1680 $test_str = substr($part, $i, $com_len);
1681 if ($this->language_data
['CASE_SENSITIVE'][GESHI_COMMENTS
]) {
1682 $match = ($comment_mark == $test_str);
1684 $match = (strtolower($comment_mark) == strtolower($test_str));
1687 $COMMENT_MATCHED = true
;
1688 if ($this->lexic_permissions
['COMMENTS'][$comment_key]) {
1689 if (!$this->use_classes
) {
1690 $attributes = ' style="' . $this->language_data
['STYLES']['COMMENTS'][$comment_key] . '"';
1692 $attributes = ' class="co' . $comment_key . '"';
1694 $test_str = "<span$attributes>" . @htmlspecialchars($this->change_case($test_str), ENT_COMPAT
, $this->encoding
);
1696 $test_str = @htmlspecialchars($test_str, ENT_COMPAT
, $this->encoding
);
1698 $close_pos = strpos($part, "\n", $i);
1700 if ($close_pos === false
) {
1701 $close_pos = strlen($part);
1704 $test_str .= @htmlspecialchars(substr($part, $i +
$com_len, $close_pos - $i - $com_len), ENT_COMPAT
, $this->encoding
);
1705 if ($this->lexic_permissions
['COMMENTS'][$comment_key]) {
1706 $test_str .= "</span>";
1708 // Take into account that the comment might be the last in the source
1714 $result .= $this->parse_non_string_part($stuff_to_parse);
1715 $stuff_to_parse = '';
1720 } elseif ($STRING_OPEN != '') {
1721 // Otherwise, convert it to HTML form
1722 if (strtolower($this->encoding
) == 'utf-8') {
1723 //only escape <128 (we don't want to break multibyte chars)
1724 if (ord($char) < 128) {
1725 $char = @htmlspecialchars($char, ENT_COMPAT
, $this->encoding
);
1729 $char = @htmlspecialchars($char, ENT_COMPAT
, $this->encoding
);
1732 // Where are we adding this char?
1733 if (!$COMMENT_MATCHED) {
1734 if (($STRING_OPEN == '') && !$CLOSE_STRING) {
1735 $stuff_to_parse .= $char;
1738 $CLOSE_STRING = false
;
1741 $result .= $test_str;
1742 $COMMENT_MATCHED = false
;
1745 // Parse the last bit
1746 $result .= $this->parse_non_string_part($stuff_to_parse);
1747 $stuff_to_parse = '';
1749 $result .= @htmlspecialchars($part, ENT_COMPAT
, $this->encoding
);
1751 // Close the <span> that surrounds the block
1752 if ($this->strict_mode
&& $this->language_data
['STYLES']['SCRIPT'][$script_key] != '' &&
1753 $this->lexic_permissions
['SCRIPT']) {
1754 $result .= '</span>';
1757 // Else not a block to highlight
1758 $result .= @htmlspecialchars($part, ENT_COMPAT
, $this->encoding
);
1762 // Parse the last stuff (redundant?)
1763 $result .= $this->parse_non_string_part($stuff_to_parse);
1765 // Lop off the very first and last spaces
1766 $result = substr($result, 1, strlen($result) - 1);
1768 // Are we still in a string?
1770 $result .= '</span>';
1773 // We're finished: stop timing
1774 $this->set_time($start_time, microtime());
1776 return $this->finalise($result);
1780 * Swaps out spaces and tabs for HTML indentation. Not needed if
1781 * the code is in a pre block...
1783 * @param string The source to indent
1784 * @return string The source with HTML indenting applied
1788 function indent ($result)
1790 /// Replace tabs with the correct number of spaces
1791 if (false
!== strpos($result, "\t")) {
1792 $lines = explode("\n", $result);
1793 foreach ($lines as $key => $line) {
1794 if (false
=== strpos($line, "\t")) {
1795 $lines[$key] = $line;
1797 }//echo 'checking line ' . $key . '<br />';
1800 $tab_width = $this->tab_width
;
1801 $length = strlen($line);
1804 //echo '<pre>line: ' . htmlspecialchars($line) . '</pre>';
1806 for ($i = 0; $i < $length; $i++
) {
1807 $char = substr($line, $i, 1);
1808 // Simple engine to work out whether we're in a tag.
1809 // If we are we modify $pos. This is so we ignore HTML
1810 // in the line and only workout the tab replacement
1811 // via the actual content of the string
1812 // This test could be improved to include strings in the
1813 // html so that < or > would be allowed in user's styles
1814 // (e.g. quotes: '<' '>'; or similar)
1815 if ($IN_TAG && '>' == $char) {
1817 $result_line .= '>';
1819 } elseif (!$IN_TAG && '<' == $char) {
1821 $result_line .= '<';
1823 } elseif (!$IN_TAG && '&' == $char) {
1824 //echo "matched & in line... ";
1825 $substr = substr($line, $i +
3, 4);
1826 //$substr_5 = substr($line, 5, 1);
1827 $posi = strpos($substr, ';');
1828 if (false
!== $posi) {
1829 //echo "found entity at $posi\n";
1832 $result_line .= '&';
1833 } elseif (!$IN_TAG && "\t" == $char) {
1835 // OPTIMISE - move $strs out. Make an array:
1839 // 3 => ' ' etc etc
1840 // to use instead of building a string every time
1841 $strs = array(0 => ' ', 1 => ' ');
1842 //echo "building (pos=$pos i=$i) (" . ($i - $pos) . ") " . ($tab_width - (($i - $pos) % $tab_width)) . " spaces\n";
1843 for ($k = 0; $k < ($tab_width - (($i - $pos) %
$tab_width)); $k++
) $str .= $strs[$k %
2];
1844 $result_line .= $str;
1847 //$pos -= $tab_width-1;
1849 if (false
=== strpos($line, "\t", $i +
1)) {
1850 //$lines[$key] = $result_line;
1852 $result_line .= substr($line, $i +
1);
1855 } elseif ( $IN_TAG ) {
1857 $result_line .= $char;
1859 $result_line .= $char;
1863 $lines[$key] = $result_line;
1865 $result = implode("\n", $lines);
1868 $result = str_replace(' ', ' ', $result);
1869 $result = str_replace(' ', ' ', $result);
1870 $result = str_replace("\n ", "\n ", $result);
1872 if ($this->line_numbers
== GESHI_NO_LINE_NUMBERS
) {
1873 $result = nl2br($result);
1879 * Changes the case of a keyword for those languages where a change is asked for
1881 * @param string The keyword to change the case of
1882 * @return string The keyword with its case changed
1886 function change_case ($instr)
1888 if ($this->language_data
['CASE_KEYWORDS'] == GESHI_CAPS_UPPER
) {
1889 return strtoupper($instr);
1890 } elseif ($this->language_data
['CASE_KEYWORDS'] == GESHI_CAPS_LOWER
) {
1891 return strtolower($instr);
1897 * Adds a url to a keyword where needed.
1899 * @param string The keyword to add the URL HTML to
1900 * @param int What group the keyword is from
1901 * @param boolean Whether to get the HTML for the start or end
1902 * @return The HTML for either the start or end of the HTML <a> tag
1905 * @todo Get rid of ender
1907 function add_url_to_keyword ($keyword, $group, $start_or_end)
1909 if (isset($this->language_data
['URLS'][$group]) &&
1910 $this->language_data
['URLS'][$group] != '' &&
1911 substr($keyword, 0, 5) != '</') {
1912 // There is a base group for this keyword
1913 if ($start_or_end == 'BEGIN') {
1914 // HTML workaround... not good form (tm) but should work for 1.0.X
1915 $keyword = ( substr($keyword, 0, 4) == '<' ) ?
substr($keyword, 4) : $keyword;
1916 $keyword = ( substr($keyword, -4) == '>' ) ?
substr($keyword, 0, strlen($keyword) - 4) : $keyword;
1917 if ($keyword != '') {
1918 $keyword = ( $this->language_data
['CASE_SENSITIVE'][$group] ) ?
$keyword : strtolower($keyword);
1921 array('{FNAME}', '.'),
1922 array(@htmlspecialchars($keyword, ENT_COMPAT
, $this->encoding
), '<DOT>'),
1923 $this->language_data
['URLS'][$group]
1927 // HTML fix. Again, dirty hackage...
1928 } elseif (!($this->language
== 'html4strict' && '>' == $keyword)) {
1935 * Takes a string that has no strings or comments in it, and highlights
1936 * stuff like keywords, numbers and methods.
1938 * @param string The string to parse for keyword, numbers etc.
1941 * @todo BUGGY! Why? Why not build string and return?
1943 function parse_non_string_part (&$stuff_to_parse)
1945 $stuff_to_parse = ' ' . quotemeta(@htmlspecialchars($stuff_to_parse, ENT_COMPAT
, $this->encoding
));
1946 // These vars will disappear in the future
1947 $func = '$this->change_case';
1948 $func2 = '$this->add_url_to_keyword';
1951 // Regular expressions
1953 foreach ($this->language_data
['REGEXPS'] as $key => $regexp) {
1954 if ($this->lexic_permissions
['REGEXPS'][$key]) {
1955 if (is_array($regexp)) {
1956 $stuff_to_parse = preg_replace(
1958 $regexp[GESHI_SEARCH
] .
1959 "#{$regexp[GESHI_MODIFIERS]}",
1960 "{$regexp[GESHI_BEFORE]}<|!REG3XP$key!>{$regexp[GESHI_REPLACE]}|>{$regexp[GESHI_AFTER]}",
1964 $stuff_to_parse = preg_replace( "#(" . $regexp . ")#", "<|!REG3XP$key!>\\1|>", $stuff_to_parse);
1970 // Highlight numbers. This regexp sucks... anyone with a regexp that WORKS
1971 // here wins a cookie if they send it to me. At the moment there's two doing
1972 // almost exactly the same thing, except the second one prevents a number
1973 // being highlighted twice (eg <span...><span...>5</span></span>)
1974 // Put /NUM!/ in for the styles, which gets replaced at the end.
1976 if ($this->lexic_permissions
['NUMBERS'] && preg_match('#[0-9]#', $stuff_to_parse )) {
1977 $stuff_to_parse = preg_replace('#([^a-zA-Z0-9\#])([0-9]+)([^a-zA-Z0-9])#', "\\1<|/NUM!/>\\2|>\\3", $stuff_to_parse);
1978 $stuff_to_parse = preg_replace('#([^a-zA-Z0-9\#>])([0-9]+)([^a-zA-Z0-9])#', "\\1<|/NUM!/>\\2|>\\3", $stuff_to_parse);
1981 // Highlight keywords
1982 // if there is a couple of alpha symbols there *might* be a keyword
1983 if (preg_match('#[a-zA-Z]{2,}#', $stuff_to_parse)) {
1984 foreach ($this->language_data
['KEYWORDS'] as $k => $keywordset) {
1985 if ($this->lexic_permissions
['KEYWORDS'][$k]) {
1986 foreach ($keywordset as $keyword) {
1987 $keyword = quotemeta($keyword);
1989 // This replacement checks the word is on it's own (except if brackets etc
1990 // are next to it), then highlights it. We don't put the color=" for the span
1991 // in just yet - otherwise languages with the keywords "color" or "or" have
1994 if (false
!== stristr($stuff_to_parse, $keyword )) {
1995 $stuff_to_parse .= ' ';
1996 // Might make a more unique string for putting the number in soon
1997 // Basically, we don't put the styles in yet because then the styles themselves will
1998 // get highlighted if the language has a CSS keyword in it (like CSS, for example ;))
2000 $keyword = quotemeta($keyword);
2001 if ($this->language_data
['CASE_SENSITIVE'][$k]) {
2002 $stuff_to_parse = preg_replace(
2003 "#([^a-zA-Z0-9\$_\|\#;>])($keyword)([^a-zA-Z0-9_<\|%\-&])#e",
2004 "'\\1' . $func2('\\2', '$k', 'BEGIN') . '<|$styles>' . $func('\\2') . '|>' . $func2('\\2', '$k', 'END') . '\\3'",
2008 // Change the case of the word.
2009 $stuff_to_parse = preg_replace(
2010 "#([^a-zA-Z0-9\$_\|\#;>])($keyword)([^a-zA-Z0-9_<\|%\-&])#ie",
2011 "'\\1' . $func2('\\2', '$k', 'BEGIN') . '<|$styles>' . $func('\\2') . '|>' . $func2('\\2', '$k', 'END') . '\\3'",
2015 $stuff_to_parse = substr($stuff_to_parse, 0, strlen($stuff_to_parse) - 1);
2023 // Now that's all done, replace /[number]/ with the correct styles
2025 foreach ($this->language_data
['KEYWORDS'] as $k => $kws) {
2026 if (!$this->use_classes
) {
2027 $attributes = ' style="' . $this->language_data
['STYLES']['KEYWORDS'][$k] . '"';
2029 $attributes = ' class="kw' . $k . '"';
2031 $stuff_to_parse = str_replace("/$k/", $attributes, $stuff_to_parse);
2034 // Put number styles in
2035 if (!$this->use_classes
&& $this->lexic_permissions
['NUMBERS']) {
2036 $attributes = ' style="' . $this->language_data
['STYLES']['NUMBERS'][0] . '"';
2038 $attributes = ' class="nu0"';
2040 $stuff_to_parse = str_replace('/NUM!/', $attributes, $stuff_to_parse);
2043 // Highlight methods and fields in objects
2045 if ($this->lexic_permissions
['METHODS'] && $this->language_data
['OOLANG']) {
2046 foreach ($this->language_data
['OBJECT_SPLITTERS'] as $key => $splitter) {
2047 if (false
!== stristr($stuff_to_parse, $splitter)) {
2048 if (!$this->use_classes
) {
2049 $attributes = ' style="' . $this->language_data
['STYLES']['METHODS'][$key] . '"';
2051 $attributes = ' class="me' . $key . '"';
2053 $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);
2059 // Highlight brackets. Yes, I've tried adding a semi-colon to this list.
2060 // You try it, and see what happens ;)
2061 // TODO: Fix lexic permissions not converting entities if shouldn't
2062 // be highlighting regardless
2064 if ($this->lexic_permissions
['BRACKETS']) {
2065 $code_entities_match = array('[', ']', '(', ')', '{', '}');
2066 if (!$this->use_classes
) {
2067 $code_entities_replace = array(
2068 '<| style="' . $this->language_data
['STYLES']['BRACKETS'][0] . '">[|>',
2069 '<| style="' . $this->language_data
['STYLES']['BRACKETS'][0] . '">]|>',
2070 '<| style="' . $this->language_data
['STYLES']['BRACKETS'][0] . '">(|>',
2071 '<| style="' . $this->language_data
['STYLES']['BRACKETS'][0] . '">)|>',
2072 '<| style="' . $this->language_data
['STYLES']['BRACKETS'][0] . '">{|>',
2073 '<| style="' . $this->language_data
['STYLES']['BRACKETS'][0] . '">}|>',
2076 $code_entities_replace = array(
2077 '<| class="br0">[|>',
2078 '<| class="br0">]|>',
2079 '<| class="br0">(|>',
2080 '<| class="br0">)|>',
2081 '<| class="br0">{|>',
2082 '<| class="br0">}|>',
2085 $stuff_to_parse = str_replace( $code_entities_match, $code_entities_replace, $stuff_to_parse );
2089 // Add class/style for regexps
2091 foreach ($this->language_data
['REGEXPS'] as $key => $regexp) {
2092 if ($this->lexic_permissions
['REGEXPS'][$key]) {
2093 if (!$this->use_classes
) {
2094 $attributes = ' style="' . $this->language_data
['STYLES']['REGEXPS'][$key] . '"';
2096 $attributes = ' class="re' . $key . '"';
2098 $stuff_to_parse = str_replace("!REG3XP$key!", "$attributes", $stuff_to_parse);
2102 // Replace <DOT> with . for urls
2103 $stuff_to_parse = str_replace('<DOT>', '.', $stuff_to_parse);
2104 // Replace <|UR1| with <a href= for urls also
2105 if (isset($this->link_styles
[GESHI_LINK
])) {
2106 if ($this->use_classes
) {
2107 $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target
. ' href=', $stuff_to_parse);
2109 $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target
. ' style="' . $this->link_styles
[GESHI_LINK
] . '" href=', $stuff_to_parse);
2112 $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target
. ' href=', $stuff_to_parse);
2116 // NOW we add the span thingy ;)
2119 $stuff_to_parse = str_replace('<|', '<span', $stuff_to_parse);
2120 $stuff_to_parse = str_replace ( '|>', '</span>', $stuff_to_parse );
2122 return substr(stripslashes($stuff_to_parse), 1);
2126 * Sets the time taken to parse the code
2128 * @param microtime The time when parsing started
2129 * @param microtime The time when parsing ended
2133 function set_time ($start_time, $end_time)
2135 $start = explode(' ', $start_time);
2136 $end = explode(' ', $end_time);
2137 $this->time
= $end[0] +
$end[1] - $start[0] - $start[1];
2141 * Gets the time taken to parse the code
2143 * @return double The time taken to parse the code
2146 function get_time ()
2152 * Gets language information and stores it for later use
2155 * @todo Needs to load keys for lexic permissions for keywords, regexps etc
2157 function load_language ($file_name)
2159 $language_data = array();
2161 // Perhaps some checking might be added here later to check that
2162 // $language data is a valid thing but maybe not
2163 $this->language_data
= $language_data;
2164 // Set strict mode if should be set
2165 if ($this->language_data
['STRICT_MODE_APPLIES'] == GESHI_ALWAYS
) {
2166 $this->strict_mode
= true
;
2168 // Set permissions for all lexics to true
2169 // so they'll be highlighted by default
2170 foreach ($this->language_data
['KEYWORDS'] as $key => $words) {
2171 $this->lexic_permissions
['KEYWORDS'][$key] = true
;
2173 foreach ($this->language_data
['COMMENT_SINGLE'] as $key => $comment) {
2174 $this->lexic_permissions
['COMMENTS'][$key] = true
;
2176 foreach ($this->language_data
['REGEXPS'] as $key => $regexp) {
2177 $this->lexic_permissions
['REGEXPS'][$key] = true
;
2179 $this->enable_highlighting();
2180 // Set default class for CSS
2181 $this->overall_class
= $this->language
;
2185 * Takes the parsed code and various options, and creates the HTML
2186 * surrounding it to make it look nice.
2188 * @param string The code already parsed
2189 * @return string The code nicely finalised
2193 function finalise ($parsed_code)
2195 // Remove end parts of important declarations
2196 // This is BUGGY!! My fault for bad code: fix coming in 1.2
2197 // @todo Remove this crap
2198 if ($this->enable_important_blocks
&&
2199 (strstr($parsed_code, @htmlspecialchars(GESHI_START_IMPORTANT
, ENT_COMPAT
, $this->encoding
)) === false
)) {
2200 $parsed_code = str_replace(@htmlspecialchars(GESHI_END_IMPORTANT
, ENT_COMPAT
, $this->encoding
), '', $parsed_code);
2203 // Add HTML whitespace stuff if we're using the <div> header
2204 if ($this->header_type
!= GESHI_HEADER_PRE
) {
2205 $parsed_code = $this->indent($parsed_code);
2208 // If we're using line numbers, we insert <li>s and appropriate
2209 // markup to style them (otherwise we don't need to do anything)
2210 if ($this->line_numbers
!= GESHI_NO_LINE_NUMBERS
) {
2211 // If we're using the <pre> header, we shouldn't add newlines because
2212 // the <pre> will line-break them (and the <li>s already do this for us)
2213 $ls = ($this->header_type
!= GESHI_HEADER_PRE
) ?
"\n" : '';
2214 // Get code into lines
2215 $code = explode("\n", $parsed_code);
2216 // Set vars to defaults for following loop
2220 foreach ($code as $line) {
2221 $line = ( $line ) ?
$line : ' ';
2222 // If this is a "special line"...
2223 if ($this->line_numbers
== GESHI_FANCY_LINE_NUMBERS
&&
2224 $i %
$this->line_nth_row
== ($this->line_nth_row
- 1)) {
2225 // Set the attributes to style the line
2226 if ($this->use_classes
) {
2227 $attr = ' class="li2"';
2228 $def_attr = ' class="de2"';
2230 $attr = ' style="' . $this->line_style2
. '"';
2231 // This style "covers up" the special styles set for special lines
2232 // so that styles applied to special lines don't apply to the actual
2233 // code on that line
2234 $def_attr = ' style="' . $this->code_style
. '"';
2237 $start = "<div$def_attr>";
2240 if ($this->use_classes
) {
2241 $attr = ' class="li1"';
2242 $def_attr = ' class="de1"';
2244 $attr = ' style="' . $this->line_style1
. '"';
2245 $def_attr = ' style="' . $this->code_style
. '"';
2247 $start = "<div$def_attr>";
2252 // Are we supposed to use ids? If so, add them
2253 if ($this->add_ids
) {
2254 $attr .= " id=\"{$this->overall_id}-{$i}\"";
2256 if ($this->use_classes
&& in_array($i, $this->highlight_extra_lines
)) {
2257 $attr .= " class=\"ln-xtra\"";
2259 if (!$this->use_classes
&& in_array($i, $this->highlight_extra_lines
)) {
2260 $attr .= " style=\"{$this->highlight_extra_lines_style}\"";
2263 // Add in the line surrounded by appropriate list HTML
2264 $parsed_code .= "<li$attr>$start$line$end</li>$ls";
2267 // No line numbers, but still need to handle highlighting lines extra.
2268 // Have to use divs so the full width of the code is highlighted
2269 $code = explode("\n", $parsed_code);
2272 foreach ($code as $line)
2274 // Make lines have at least one space in them if they're empty
2275 $line = ($line) ?
$line : ' ';
2276 if (in_array(++
$i, $this->highlight_extra_lines
)) {
2277 if ($this->use_classes
) {
2278 $parsed_code .= '<div class="ln-xtra">';
2280 $parsed_code .= "<div style=\"{$this->highlight_extra_lines_style}\">";
2282 $parsed_code .= $line . "</div>\n";
2284 $parsed_code .= $line . "\n";
2289 // purge some unnecessary stuff
2290 $parsed_code = preg_replace('#<span[^>]+>(\s*)</span>#', '\\1', $parsed_code);
2291 $parsed_code = preg_replace('#<div[^>]+>(\s*)</div>#', '\\1', $parsed_code);
2293 if ($this->header_type
== GESHI_HEADER_PRE
) {
2294 // enforce line numbers when using pre
2295 $parsed_code = str_replace('<li></li>', '<li> </li>', $parsed_code);
2298 return $this->header() . chop($parsed_code) . $this->footer();
2302 * Creates the header for the code block (with correct attributes)
2304 * @return string The header for the code block
2310 // Get attributes needed
2311 $attributes = $this->get_attributes();
2313 $ol_attributes = '';
2315 if ($this->line_numbers_start
!= 1) {
2316 $ol_attributes .= ' start="' . $this->line_numbers_start
. '"';
2319 // Get the header HTML
2320 $header = $this->format_header_content();
2322 if (GESHI_HEADER_NONE
== $this->header_type
) {
2323 if ($this->line_numbers
!= GESHI_NO_LINE_NUMBERS
) {
2324 return "$header<ol$ol_attributes>";
2329 // Work out what to return and do it
2330 if ($this->line_numbers
!= GESHI_NO_LINE_NUMBERS
) {
2331 if ($this->header_type
== GESHI_HEADER_PRE
) {
2332 return "<pre$attributes>$header<ol$ol_attributes>";
2333 } elseif ($this->header_type
== GESHI_HEADER_DIV
) {
2334 return "<div$attributes>$header<ol$ol_attributes>";
2337 if ($this->header_type
== GESHI_HEADER_PRE
) {
2338 return "<pre$attributes>$header";
2339 } elseif ($this->header_type
== GESHI_HEADER_DIV
) {
2340 return "<div$attributes>$header";
2346 * Returns the header content, formatted for output
2348 * @return string The header content, formatted for output
2352 function format_header_content ()
2354 $header = $this->header_content
;
2356 if ($this->header_type
== GESHI_HEADER_PRE
) {
2357 $header = str_replace("\n", '', $header);
2359 $header = $this->replace_keywords($header);
2361 if ($this->use_classes
) {
2362 $attr = ' class="head"';
2364 $attr = " style=\"{$this->header_content_style}\"";
2366 return "<div$attr>$header</div>";
2371 * Returns the footer for the code block.
2373 * @return string The footer for the code block
2379 $footer_content = $this->format_footer_content();
2381 if (GESHI_HEADER_NONE
== $this->header_type
) {
2382 return ($this->line_numbers
!= GESHI_NO_LINE_NUMBERS
) ?
'</ol>' . $footer_content
2386 if ($this->header_type
== GESHI_HEADER_DIV
) {
2387 if ($this->line_numbers
!= GESHI_NO_LINE_NUMBERS
) {
2388 return "</ol>$footer_content</div>";
2390 return "$footer_content</div>";
2392 if ($this->line_numbers
!= GESHI_NO_LINE_NUMBERS
) {
2393 return "</ol>$footer_content</pre>";
2395 return "$footer_content</pre>";
2400 * Returns the footer content, formatted for output
2402 * @return string The footer content, formatted for output
2406 function format_footer_content ()
2408 $footer = $this->footer_content
;
2410 if ($this->header_type
== GESHI_HEADER_PRE
) {
2411 $footer = str_replace("\n", '', $footer);;
2413 $footer = $this->replace_keywords($footer);
2415 if ($this->use_classes
) {
2416 $attr = ' class="foot"';
2418 $attr = " style=\"{$this->footer_content_style}\"";
2420 return "<div$attr>$footer</div>";
2425 * Replaces certain keywords in the header and footer with
2426 * certain configuration values
2428 * @param string The header or footer content to do replacement on
2429 * @return string The header or footer with replaced keywords
2433 function replace_keywords ($instr)
2435 $keywords = $replacements = array();
2437 $keywords[] = '<TIME>';
2438 $replacements[] = number_format($this->get_time(), 3);
2440 $keywords[] = '<LANGUAGE>';
2441 $replacements[] = $this->language
;
2443 $keywords[] = '<VERSION>';
2444 $replacements[] = GESHI_VERSION
;
2446 return str_replace($keywords, $replacements, $instr);
2450 * Gets the CSS attributes for this code
2452 * @return The CSS attributes for this code
2455 * @todo Document behaviour change - class is outputted regardless of whether we're using classes or not.
2458 function get_attributes ()
2462 if ($this->overall_class
!= '') {
2463 $attributes .= " class=\"{$this->overall_class}\"";
2465 if ($this->overall_id
!= '') {
2466 $attributes .= " id=\"{$this->overall_id}\"";
2468 if ($this->overall_style
!= '') {
2469 $attributes .= ' style="' . $this->overall_style
. '"';
2475 * Returns a stylesheet for the highlighted code. If $economy mode
2476 * is true, we only return the stylesheet declarations that matter for
2477 * this code block instead of the whole thing
2479 * @param boolean Whether to use economy mode or not
2480 * @return string A stylesheet built on the data for the current language
2483 function get_stylesheet ($economy_mode = true
)
2485 // If there's an error, chances are that the language file
2486 // won't have populated the language data file, so we can't
2487 // risk getting a stylesheet...
2491 // First, work out what the selector should be. If there's an ID,
2492 // that should be used, the same for a class. Otherwise, a selector
2493 // of '' means that these styles will be applied anywhere
2494 $selector = ($this->overall_id
!= '') ?
"#{$this->overall_id} " : '';
2495 $selector = ($selector == '' && $this->overall_class
!= '') ?
".{$this->overall_class} " : $selector;
2497 // Header of the stylesheet
2498 if (!$economy_mode) {
2499 $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";
2501 $stylesheet = '/* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */' . "\n";
2504 // Set the <ol> to have no effect at all if there are line numbers
2505 // (<ol>s have margins that should be destroyed so all layout is
2506 // controlled by the set_overall_style method, which works on the
2507 // <pre> or <div> container). Additionally, set default styles for lines
2508 if (!$economy_mode ||
$this->line_numbers
!= GESHI_NO_LINE_NUMBERS
) {
2509 //$stylesheet .= "$selector, {$selector}ol, {$selector}ol li {margin: 0;}\n";
2510 $stylesheet .= "$selector.de1, $selector.de2 {{$this->code_style}}\n";
2513 // Add overall styles
2514 if (!$economy_mode ||
$this->overall_style
!= '') {
2515 $stylesheet .= "$selector {{$this->overall_style}}\n";
2518 // Add styles for links
2519 foreach ($this->link_styles
as $key => $style) {
2520 if (!$economy_mode ||
$key == GESHI_LINK
&& $style != '') {
2521 $stylesheet .= "{$selector}a:link {{$style}}\n";
2523 if (!$economy_mode ||
$key == GESHI_HOVER
&& $style != '') {
2524 $stylesheet .= "{$selector}a:hover {{$style}}\n";
2526 if (!$economy_mode ||
$key == GESHI_ACTIVE
&& $style != '') {
2527 $stylesheet .= "{$selector}a:active {{$style}}\n";
2529 if (!$economy_mode ||
$key == GESHI_VISITED
&& $style != '') {
2530 $stylesheet .= "{$selector}a:visited {{$style}}\n";
2534 // Header and footer
2535 if (!$economy_mode ||
$this->header_content_style
!= '') {
2536 $stylesheet .= "$selector.head {{$this->header_content_style}}\n";
2538 if (!$economy_mode ||
$this->footer_content_style
!= '') {
2539 $stylesheet .= "$selector.foot {{$this->footer_content_style}}\n";
2542 // Styles for important stuff
2543 if (!$economy_mode ||
$this->important_styles
!= '') {
2544 $stylesheet .= "$selector.imp {{$this->important_styles}}\n";
2547 // Styles for lines being highlighted extra
2548 if (!$economy_mode ||
count($this->highlight_extra_lines
)) {
2549 $stylesheet .= "$selector.ln-xtra {{$this->highlight_extra_lines_style}}\n";
2552 // Simple line number styles
2553 if (!$economy_mode ||
($this->line_numbers
!= GESHI_NO_LINE_NUMBERS
&& $this->line_style1
!= '')) {
2554 $stylesheet .= "{$selector}li {{$this->line_style1}}\n";
2557 // If there is a style set for fancy line numbers, echo it out
2558 if (!$economy_mode ||
($this->line_numbers
== GESHI_FANCY_LINE_NUMBERS
&& $this->line_style2
!= '')) {
2559 $stylesheet .= "{$selector}li.li2 {{$this->line_style2}}\n";
2562 foreach ($this->language_data
['STYLES']['KEYWORDS'] as $group => $styles) {
2563 if (!$economy_mode ||
!($economy_mode && (!$this->lexic_permissions
['KEYWORDS'][$group] ||
$styles == ''))) {
2564 $stylesheet .= "$selector.kw$group {{$styles}}\n";
2567 foreach ($this->language_data
['STYLES']['COMMENTS'] as $group => $styles) {
2568 if (!$economy_mode ||
!($economy_mode && $styles == '') &&
2569 !($economy_mode && !$this->lexic_permissions
['COMMENTS'][$group])) {
2570 $stylesheet .= "$selector.co$group {{$styles}}\n";
2573 foreach ($this->language_data
['STYLES']['ESCAPE_CHAR'] as $group => $styles) {
2574 if (!$economy_mode ||
!($economy_mode && $styles == '') && !($economy_mode &&
2575 !$this->lexic_permissions
['ESCAPE_CHAR'])) {
2576 $stylesheet .= "$selector.es$group {{$styles}}\n";
2579 foreach ($this->language_data
['STYLES']['SYMBOLS'] as $group => $styles) {
2580 if (!$economy_mode ||
!($economy_mode && $styles == '') && !($economy_mode &&
2581 !$this->lexic_permissions
['BRACKETS'])) {
2582 $stylesheet .= "$selector.br$group {{$styles}}\n";
2585 foreach ($this->language_data
['STYLES']['STRINGS'] as $group => $styles) {
2586 if (!$economy_mode ||
!($economy_mode && $styles == '') && !($economy_mode &&
2587 !$this->lexic_permissions
['STRINGS'])) {
2588 $stylesheet .= "$selector.st$group {{$styles}}\n";
2591 foreach ($this->language_data
['STYLES']['NUMBERS'] as $group => $styles) {
2592 if (!$economy_mode ||
!($economy_mode && $styles == '') && !($economy_mode &&
2593 !$this->lexic_permissions
['NUMBERS'])) {
2594 $stylesheet .= "$selector.nu$group {{$styles}}\n";
2597 foreach ($this->language_data
['STYLES']['METHODS'] as $group => $styles) {
2598 if (!$economy_mode ||
!($economy_mode && $styles == '') && !($economy_mode &&
2599 !$this->lexic_permissions
['METHODS'])) {
2600 $stylesheet .= "$selector.me$group {{$styles}}\n";
2603 foreach ($this->language_data
['STYLES']['SCRIPT'] as $group => $styles) {
2604 if (!$economy_mode ||
!($economy_mode && $styles == '')) {
2605 $stylesheet .= "$selector.sc$group {{$styles}}\n";
2608 foreach ($this->language_data
['STYLES']['REGEXPS'] as $group => $styles) {
2609 if (!$economy_mode ||
!($economy_mode && $styles == '') && !($economy_mode &&
2610 !$this->lexic_permissions
['REGEXPS'][$group])) {
2611 $stylesheet .= "$selector.re$group {{$styles}}\n";
2618 } // End Class GeSHi
2621 if (!function_exists('geshi_highlight')) {
2623 * Easy way to highlight stuff. Behaves just like highlight_string
2625 * @param string The code to highlight
2626 * @param string The language to highlight the code in
2627 * @param string The path to the language files. You can leave this blank if you need
2628 * as from version 1.0.7 the path should be automatically detected
2629 * @param boolean Whether to return the result or to echo
2630 * @return string The code highlighted (if $return is true)
2633 function geshi_highlight ($string, $language, $path, $return = false
)
2635 $geshi = new GeSHi($string, $language, $path);
2636 $geshi->set_header_type(GESHI_HEADER_NONE
);
2638 return '<code>' . $geshi->parse_code() . '</code>';
2640 echo '<code>' . $geshi->parse_code() . '</code>';
2641 if ($geshi->error()) {