* @license http://www.gnu.org/copyleft/gpl.html GNU GPL * @copyright (C) 2005 Nigel McNie * @version 1.1.0 * */ $GLOBALS['geshi_dbg_level'] = 0; function geshi_dbg_level ($level) { $GLOBALS['geshi_dbg_level'] = $level; } /** * Handles debugging by printing a message according to current debug level, * mask of context and other things. * * @param string The message to print out * @param int The context in which this message is to be printed out in - see * the GESHI_DBG_* constants * @param boolean Whether to add a newline to the message * @param boolean Whether to return the count of errors or not */ function geshi_dbg ($message, $context, $add_nl = true, $return_counts = false) { if ((GESHI_DBG & $context) || ($GLOBALS['geshi_dbg_level'] & $context)) { // // Message can have the following symbols at start // // @b: bold // @i: italic // @o: ok (green colour) // @w: warn (yellow colour) // @e: err (red colour) $test = substr($message, 0, 2); $start = ''; $end = ''; switch ($test) { case '@b': $start = ''; break; case '@i': $start = ''; break; case '@o': $start = ''; break; case '@w': $start = ''; break; case '@e': $start = ''; break; default: $end = ''; } if(preg_match('#(.*?)::(.*?)\((.*?)\)#si', $message)) { $start = ''; $end = ''; } if (preg_match('#^@[a-z]#', $message)) { $message = substr($message, 2); } echo $start . htmlspecialchars(str_replace("\n", '', $message)) . $end; if ($add_nl) echo "\n"; } } /** * Checks whether a file name is able to be read by GeSHi * * The file must be within the GESHI_ROOT directory * * @param string The absolute pathname of the file to check * @return boolean Whether the file is readable by GeSHi */ function geshi_can_include ($file_name) { return (GESHI_ROOT == substr($file_name, 0, strlen(GESHI_ROOT)) && is_file($file_name) && is_readable($file_name)); } /** * Drop-in replacement for strpos and stripos. Also can handle regular expression * string positions. * * @param string The string in which to search for the $needle * @param string The string to search for. If this string starts with "REGEX" then * a regular expression search is performed. * @param int The offset in the string in which to start searching * @param boolean Whether the search is case sensitive or not * @param boolean Whether the match table is needed (almost never, and it makes things slower) * @return array An array of data: *
 'pos' => position in string of needle,
 * 'len' => length of match
 * 'tab' => a table of the stuff matched in brackets for a regular expression
*/ function geshi_get_position ($haystack, $needle, $offset = 0, $case_sensitive = false, $need_table = false) { //geshi_dbg('Checking haystack: ' . $haystack . ' against needle ' . $needle . ' (' . $offset . ')',GESHI_DBG_PARSE, false); if ('REGEX' != substr($needle, 0, 5)) { if (!$case_sensitive) { return array('pos' => stripos($haystack, $needle, $offset), 'len' => strlen($needle)); } else { return array('pos' => strpos($haystack, $needle, $offset), 'len' => strlen($needle)); } } // geshi_preg_match_pos $regex = substr($needle, 5); $string = $haystack; $foo = microtime(); $foo_len = strlen($foo); $len = strlen($string); $str = preg_replace($regex, $foo, $string, 1); $length = $len - (strlen($str) - $foo_len); // ADD SOME MORE: Return matching table (?) if ($need_table) { $matches = array(); preg_match_all($regex, $string, $matches); //$table = $matches; $i = 0; $table = array(); foreach ( $matches as $match ) { $table[$i++] = (isset($match[0])) ? $match[0] : null; } } else { $table = array(); } return array('pos' => strpos($str, $foo), 'len' => $length, 'tab' => $table); } /** * Returns the regexp for integer numbers, for use with GeSHiCodeContexts * * @param string The prefix to use for the name of this number match * @return array */ function geshi_use_integers ($prefix) { return array( 0 => array( '#([^a-zA-Z_0-9\.]|^)([-]?[0-9]+)(?=[^a-zA-Z_0-9]|$)#' ), 1 => '', 2 => array( 1 => true, // catch banned stuff for highlighting by the code context that it is in 2 => array( 0 => $prefix . '/' . GESHI_NUM_INT, 1 => 'color:#11e;', 2 => false ), 3 => true ) ); } /** * Returns the regexp for double numbers, for use with GeSHiCodeContexts * * @param string The prefix to use for the name of this number match * @param boolean Whether a number is required in front of the decimal point or not. * @return array */ function geshi_use_doubles ($prefix, $require_leading_number = false) { $banned = '[^a-zA-Z_0-9]'; $plus_minus = '[\-\+]?'; $leading_number_symbol = ($require_leading_number) ? '+' : '*'; return array( 0 => array( // double precision with e, e.g. 3.5e7 or -.45e2 "#(^|$banned)?({$plus_minus}[0-9]$leading_number_symbol\.[0-9]+[eE]{$plus_minus}[0-9]+)($banned|\$)?#", // double precision with e and no decimal place, e.g. 5e2 "#(^|$banned)?({$plus_minus}[0-9]+[eE]{$plus_minus}[0-9]+)($banned|\$)?#", // double precision (.123 or 34.342 for example) // There are some cases where the - sign will not be highlighted for various reasons, // but I'm happy that it's done where it can be. Maybe it might be worth looking at // later if there are any real problems, else I'll ignore it "#(^|$banned)?({$plus_minus}[0-9]$leading_number_symbol\.[0-9]+)($banned|\$)?#" ), 1 => '.', //doubles must have a dot 2 => array( 1 => true, // as above, catch for normal stuff 2 => array( 0 => $prefix . '/' . GESHI_NUM_DBL, 1 => 'color:#d3d;', 2 => false // Don't attempt to highlight numbers as code ), 3 => true, //4 => true ) ); } // @todo [blocking 1.1.9] fix this up // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Aidan Lister | // +----------------------------------------------------------------------+ // /** * Replace stripos() * * This function lifted from the PHP_Compat PEAR package, and optimised * * @author Aidan Lister , Nigel McNie * @version 1.1.0 */ if (!function_exists('stripos')) { function stripos ( $haystack, $needle, $offset = null ) { // Manipulate the string if there is an offset $fix = 0; if (!is_null($offset)) { if ($offset > 0) { $haystack = substr($haystack, $offset); $fix = $offset; } } $segments = explode(strtolower($needle), strtolower($haystack), 2); // Check there was a match if (count($segments) == 1) { return false; } return strlen($segments[0]) + $fix; } } ?>