📄 geshi.php
字号:
<?php/************************************************************************************* * geshi.php * --------- * Author: Nigel McNie (oracle.shinoda@gmail.com) * Copyright: (c) 2004 Nigel McNie * Release Version: 1.0.4 * CVS Revision Version: $Revision: 1.1 $ * Date Started: 2004/05/20 * Last Modified: $Date: 2004/12/17 08:01:42 $ * * The GeSHi class for Generic Syntax Highlighting. Please refer to the documentation * at http://qbnz.com/highlighter/documentation.php for more information about how to * use this class. * * For changes, release notes, TODOs etc, see the relevant files in the docs/ directory * ************************************************************************************* * * This file is part of GeSHi. * * GeSHi is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * GeSHi is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GeSHi; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ************************************************************************************///// GeSHi Constants// You should use these constant names in your programs instead of// their values - you never know when a value may change in a future// version//// For the future (though this may never be realised)define('GESHI_OUTPUT_HTML', 0);// Shouldn't be used by your programdefine('GESHI_COMMENTS', 0);// Error detection - use these to analyse faultsdefine('GESHI_ERROR_NO_INPUT', 1);define('GESHI_ERROR_NO_SUCH_LANG', 2);// Human error messages - added in 1.0.2$_GESHI_ERRORS = array( GESHI_ERROR_NO_INPUT => 'No source code inputted', GESHI_ERROR_NO_SUCH_LANG => 'GeSHi could not find the language {LANGUAGE} (using path {PATH})');// Line numbers - use with enable_line_numbers()define('GESHI_NO_LINE_NUMBERS', 0);define('GESHI_NORMAL_LINE_NUMBERS', 1);define('GESHI_FANCY_LINE_NUMBERS', 2);// Strict mode - shouldn't be used by your scriptsdefine('GESHI_NEVER', 0);define('GESHI_MAYBE', 1);define('GESHI_ALWAYS', 2);// Container HTML type - use these (added in 1.0.1)define('GESHI_HEADER_DIV', 1);define('GESHI_HEADER_PRE', 2);// Capatalisation constants - use these (added in 1.0.1)define('GESHI_CAPS_NO_CHANGE', 0);define('GESHI_CAPS_UPPER', 1);define('GESHI_CAPS_LOWER', 2);// Link style constants - use these (added in 1.0.2)define('GESHI_LINK', 0);define('GESHI_HOVER', 1);define('GESHI_ACTIVE', 2);define('GESHI_VISITED', 3);// Important string starter/finisher - use these (added in 1.0.2).// Note that if you change these, they should be as-is: i.e., don't// write them as if they had been run through htmlentities()define('GESHI_START_IMPORTANT', '<BEGIN GeSHi>');define('GESHI_END_IMPORTANT', '<END GeSHi>');// Advanced regexp handling - don't use these (added in 1.0.2)define('GESHI_SEARCH', 0);define('GESHI_REPLACE', 1);define('GESHI_MODIFIERS', 2);define('GESHI_BEFORE', 3);define('GESHI_AFTER', 4);// Begin Class GeSHiclass GeSHi{ // // Data Fields // // Basic fields var $source = ''; // The source code to highlight var $language = ''; // The language to use when highlighting var $language_data = array(); // The data for the language used var $language_path = 'geshi/'; // The path to the language files var $error = false; // The error message associated with an error var $strict_mode = false; // Whether highlighting is strict or not var $use_classes = false; // Whether to use classes var $header_type = GESHI_HEADER_PRE; // The type of header to use var $lexic_permissions = array(); // Array of permissions for which lexics should be highlighted // Added in 1.0.2 basic fields var $time = 0; // The time it took to parse the code var $header_content = ''; // The content of the header block var $footer_content = ''; // The content of the footer block var $header_content_style = ''; // The style of the header block var $footer_content_style = ''; // The style of the footer block var $link_styles = array(); // The styles for hyperlinks in the code var $enable_important_blocks = true; // Whether important blocks should be recognised or not var $important_styles = 'font-weight: bold; color: red;'; // Styles for important parts of the code var $add_ids = false; // Whether css IDs should be added to the code var $highlight_extra_lines = array(); // Lines that should be highlighted extra var $highlight_extra_lines_style = 'color: #cc0; background-color: #ffc;';// Styles of extra-highlighted lines var $line_numbers_start = 1; // Number at which line numbers should start at // Style fields var $overall_style = ''; // The overall style for this code block // The style for the actual code var $code_style = 'font-family: \'Courier New\', Courier, monospace; font-weight: normal;'; var $overall_class = ''; // The overall class for this code block var $overall_id = ''; // The overall id for this code block // Line number styles var $line_style1 = 'font-family: \'Courier New\', Courier, monospace; color: black; font-weight: normal; font-style: normal;'; var $line_style2 = 'font-weight: bold;'; var $line_numbers = GESHI_NO_LINE_NUMBERS; // Flag for how line numbers are displayed var $line_nth_row = 0; // The "nth" value for fancy line highlighting // Misc var $tab_width = 8; // A value for the size of tab stops. var $max_tabs = 20; // Maximum number of spaces per tab var $min_tabs = 0; // Minimum " " " " " var $link_target = ''; // default target for keyword links var $encoding = ''; // The encoding to use for htmlentities() calls // Deprecated/unused var $output_format = GESHI_OUTPUT_HTML; /** * constructor: GeSHi * ------------------ * Creates a new GeSHi object, with source and language */ function GeSHi ($source, $language, $path = 'geshi/') { $this->source = $source; // Security, just in case :) $language = preg_replace('#[^a-zA-Z0-9\-\_]#', '', $language); $this->language = strtolower($language); $this->language_path = ( substr($path, strlen($path) - 1, 1) == '/' ) ? $path : $path . '/'; $this->load_language(); } // // Error methods // /** * method: error * ------------- * Returns an error message associated with the last GeSHi operation, * or false if no error has occured */ function error() { global $_GESHI_ERRORS; if ( $this->error != 0 ) { $msg = $_GESHI_ERRORS[$this->error]; $debug_tpl_vars = array( '{LANGUAGE}' => $this->language, '{PATH}' => $this->language_path ); foreach ( $debug_tpl_vars as $tpl => $var ) { $msg = str_replace($tpl, $var, $msg); } return "<br /><strong>GeSHi Error:</strong> $msg (code $this->error)<br />"; } return false; } // // Getters // /** * get_language_name() * --------------- * Gets a human-readable language name (thanks to Simon Patterson * for the idea :)) */ function get_language_name() { if ( $this->error == GESHI_ERROR_NO_SUCH_LANG ) { return $this->language_data['LANG_NAME'] . ' (Unknown Language)'; } return $this->language_data['LANG_NAME']; } // // Setters // /** * method: set_source * ------------------ * Sets the source code for this object */ function set_source ( $source ) { $this->source = $source; } /** * method: set_language * -------------------- * Sets the language for this object */ function set_language ( $language ) { $language = preg_replace('#[^a-zA-Z0-9\-_]#', '', $language); $this->language = strtolower($language); // Load the language for parsing $this->load_language(); } /** * method: set_language_path * ------------------------- * Sets the path to the directory containing the language files. NOTE * that this path is relative to the directory of the script that included * geshi.php, NOT geshi.php itself. */ function set_language_path ( $path ) { $this->language_path = ( substr($path, strlen($path) - 1, 1) == '/' ) ? $path : $path . '/'; } /** * method: set_header_type * ----------------------- * Sets the type of header to be used. If GESHI_HEADER_DIV is used, * the code is surrounded in a <div>. This means more source code but * more control over tab width and line-wrapping. GESHI_HEADER_PRE * means that a <pre> is used - less source, but less control. Default * is GESHI_HEADER_PRE */ function set_header_type ( $type ) { $this->header_type = $type; } /** * method: set_overall_style * ------------------------- * Sets the styles for the code that will be outputted * when this object is parsed. The style should be a * string of valid stylesheet declarations */ function set_overall_style ( $style, $preserve_defaults = false ) { if ( $preserve_defaults ) { $this->overall_style .= $style; } else { $this->overall_style = $style; } } /** * method: set_overall_class * ------------------------- * Sets the overall classname for this block of code. This * class can then be used in a stylesheet to style this object's * output */ function set_overall_class ( $class ) { $this->overall_class = $class; } /** * method: set_overall_id * ---------------------- * Sets the overall id for this block of code. This id can then * be used in a stylesheet to style this object's output */ function set_overall_id ( $id ) { $this->overall_id = $id; } /** * method: enable_classes * ---------------------- * Sets whether CSS classes should be used to highlight the source. Default * is off, calling this method with no arguments will turn it on */ function enable_classes ( $flag = true ) { $this->use_classes = ( $flag ) ? true : false; } /** * method: set_code_style * ---------------------- * Sets the style for the actual code. This should be a string * containing valid stylesheet declarations. If $preserve_defaults is * true, then styles are merged with the default styles, with the * user defined styles having priority * * NOTE: Use this method to override any style changes you made to * the line numbers if you are using line numbers, else the line of * code will have the same style as the line number! Consult the * GeSHi documentation for more information about this. */ function set_code_style ( $style, $preserve_defaults ) { if ( $preserve_defaults ) { $this->code_style .= $style; } else { $this->code_style = $style; } } /** * method: set_line_style * ---------------------- * Sets the styles for the line numbers. This should be a string * containing valid stylesheet declarations. If $preserve_defaults is * true, then styles are merged with the default styles, with the * user defined styles having priority */ function set_line_style ( $style1, $style2 = '', $preserve_defaults = false ) { if ( is_bool($style2) ) { $preserve_defaults = $style2; $style2 = ''; } if ( $preserve_defaults ) { $this->line_style1 .= $style1; $this->line_style2 .= $style2; } else { $this->line_style1 = $style1; $this->line_style2 = $style2; } } /** * method: enable_line_numbers * --------------------------- * Sets whether line numbers should be displayed. GESHI_NO_LINE_NUMBERS = not displayed, * GESHI_NORMAL_LINE_NUMBERS = displayed, GESHI_FANCY_LINE_NUMBERS = every nth line a * different class. Default is for no line numbers to be used */ function enable_line_numbers ( $flag, $nth_row = 5 ) { $this->line_numbers = $flag; $this->line_nth_row = $nth_row; } /** * method: set_keyword_group_style * ------------------------------- * Sets the style for a keyword group. If $preserve_defaults is * true, then styles are merged with the default styles, with the * user defined styles having priority */ function set_keyword_group_style ( $key, $style, $preserve_defaults = false ) { if ( $preserve_defaults ) { $this->language_data['STYLES']['KEYWORDS'][$key] .= $style; } else { $this->language_data['STYLES']['KEYWORDS'][$key] = $style; } } /** * method: set_keyword_group_highlighting * -------------------------------------- * Turns highlighting on/off for a keyword group */ function set_keyword_group_highlighting ( $key, $flag = true ) { $this->lexic_permissions['KEYWORDS'][$key] = ( $flag ) ? true : false; } /** * method: set_comments_style * -------------------------- * Sets the styles for comment groups. If $preserve_defaults is * true, then styles are merged with the default styles, with the * user defined styles having priority */ function set_comments_style ( $key, $style, $preserve_defaults = false ) { if ( $preserve_defaults ) { $this->language_data['STYLES']['COMMENTS'][$key] .= $style; } else { $this->language_data['STYLES']['COMMENTS'][$key] = $style; } } /** * method: set_comments_highlighting * --------------------------------- * Turns highlighting on/off for comment groups */ function set_comments_highlighting ( $key, $flag = true ) { $this->lexic_permissions['COMMENTS'][$key] = ( $flag ) ? true : false; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -