Source for file geshi.php

Documentation is available at geshi.php

  1. <?php
  2. /**
  3.  * GeSHi - Generic Syntax Highlighter
  4.  *
  5.  * The GeSHi class for Generic Syntax Highlighting. Please refer to the
  6.  * documentation at http://qbnz.com/highlighter/documentation.php for more
  7.  * information about how to use this class.
  8.  *
  9.  * For changes, release notes, TODOs etc, see the relevant files in the docs/
  10.  * directory.
  11.  *
  12.  *   This file is part of GeSHi.
  13.  *
  14.  *  GeSHi is free software; you can redistribute it and/or modify
  15.  *  it under the terms of the GNU General Public License as published by
  16.  *  the Free Software Foundation; either version 2 of the License, or
  17.  *  (at your option) any later version.
  18.  *
  19.  *  GeSHi is distributed in the hope that it will be useful,
  20.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.  *  GNU General Public License for more details.
  23.  *
  24.  *  You should have received a copy of the GNU General Public License
  25.  *  along with GeSHi; if not, write to the Free Software
  26.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  27.  *
  28.  * @package    geshi
  29.  * @subpackage core
  30.  * @author     Nigel McNie <nigel@geshi.org>, Benny Baumann <BenBE@omorphia.de>
  31.  * @copyright  (C) 2004 - 2007 Nigel McNie, (C) 2007 - 2008 Benny Baumann
  32.  * @license    http://gnu.org/copyleft/gpl.html GNU GPL
  33.  *
  34.  */
  35.  
  36. //
  37. // GeSHi Constants
  38. // You should use these constant names in your programs instead of
  39. // their values - you never know when a value may change in a future
  40. // version
  41. //
  42.  
  43. /** The version of this GeSHi file */
  44. define('GESHI_VERSION''1.0.8.2',);
  45.  
  46. // Define the root directory for the GeSHi code tree
  47. if (!defined('GESHI_ROOT')) {
  48.     /** The root directory for GeSHi */
  49.     define('GESHI_ROOT'dirname(__FILE__DIRECTORY_SEPARATOR);
  50. }
  51. /** The language file directory for GeSHi
  52.     @access private */
  53. define('GESHI_LANG_ROOT'GESHI_ROOT 'geshi' DIRECTORY_SEPARATOR);
  54.  
  55. // Define if GeSHi should be paranoid about security
  56. if (!defined('GESHI_SECURITY_PARANOID')) {
  57.     /** Tells GeSHi to be paranoid about security settings */
  58.     define('GESHI_SECURITY_PARANOID'false);
  59. }
  60.  
  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);
  68.  
  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);
  76. /** Use a pre to wrap lines when line numbers are enabled or to wrap the whole code. */
  77. define('GESHI_HEADER_PRE_VALID'3);
  78. /**
  79.  * Use a "table" to surround the source:
  80.  *
  81.  *  <table>
  82.  *    <thead><tr><td colspan="2">$header</td></tr></thead>
  83.  *    <tbody><tr><td><pre>$linenumbers</pre></td><td><pre>$code></pre></td></tr></tbody>
  84.  *    <tfooter><tr><td colspan="2">$footer</td></tr></tfoot>
  85.  *  </table>
  86.  *
  87.  * this is essentially only a workaround for Firefox, see sf#1651996 or take a look at
  88.  * https://bugzilla.mozilla.org/show_bug.cgi?id=365805
  89.  * @note when linenumbers are disabled this is essentially the same as GESHI_HEADER_PRE
  90.  */
  91. define('GESHI_HEADER_PRE_TABLE'4);
  92.  
  93. // Capatalisation constants
  94. /** Lowercase keywords found */
  95. define('GESHI_CAPS_NO_CHANGE'0);
  96. /** Uppercase keywords found */
  97. define('GESHI_CAPS_UPPER'1);
  98. /** Leave keywords found as the case that they are */
  99. define('GESHI_CAPS_LOWER'2);
  100.  
  101. // Link style constants
  102. /** Links in the source in the :link state */
  103. define('GESHI_LINK'0);
  104. /** Links in the source in the :hover state */
  105. define('GESHI_HOVER'1);
  106. /** Links in the source in the :active state */
  107. define('GESHI_ACTIVE'2);
  108. /** Links in the source in the :visited state */
  109. define('GESHI_VISITED'3);
  110.  
  111. // Important string starter/finisher
  112. // Note that if you change these, they should be as-is: i.e., don't
  113. // write them as if they had been run through htmlentities()
  114. /** The starter for important parts of the source */
  115. define('GESHI_START_IMPORTANT''<BEGIN GeSHi>');
  116. /** The ender for important parts of the source */
  117. define('GESHI_END_IMPORTANT''<END GeSHi>');
  118.  
  119. /**#@+
  120.  *  @access private
  121.  */
  122. // When strict mode applies for a language
  123. /** Strict mode never applies (this is the most common) */
  124. define('GESHI_NEVER'0);
  125. /** Strict mode *might* apply, and can be enabled or
  126.     disabled by {@link GeSHi->enable_strict_mode()} */
  127. define('GESHI_MAYBE'1);
  128. /** Strict mode always applies */
  129. define('GESHI_ALWAYS'2);
  130.  
  131. // Advanced regexp handling constants, used in language files
  132. /** The key of the regex array defining what to search for */
  133. define('GESHI_SEARCH'0);
  134. /** The key of the regex array defining what bracket group in a
  135.     matched search to use as a replacement */
  136. define('GESHI_REPLACE'1);
  137. /** The key of the regex array defining any modifiers to the regular expression */
  138. define('GESHI_MODIFIERS'2);
  139. /** The key of the regex array defining what bracket group in a
  140.     matched search to put before the replacement */
  141. define('GESHI_BEFORE'3);
  142. /** The key of the regex array defining what bracket group in a
  143.     matched search to put after the replacement */
  144. define('GESHI_AFTER'4);
  145. /** The key of the regex array defining a custom keyword to use
  146.     for this regexp's html tag class */
  147. define('GESHI_CLASS'5);
  148.  
  149. /** Used in language files to mark comments */
  150. define('GESHI_COMMENTS'0);
  151.  
  152. /** Used to work around missing PHP features **/
  153. define('GESHI_PHP_PRE_433'!(version_compare(PHP_VERSION'4.3.3'=== 1));
  154.  
  155. /** make sure we can call stripos **/
  156. if (!function_exists('stripos')) {
  157.     // the offset param of preg_match is not supported below PHP 4.3.3
  158.     if (GESHI_PHP_PRE_433{
  159.         /**
  160.          * @ignore
  161.          */
  162.         function stripos($haystack$needle$offset null{
  163.             if (!is_null($offset)) {
  164.                 $haystack substr($haystack$offset);
  165.             }
  166.             if (preg_match('/'preg_quote($needle'/''/'$haystack$matchPREG_OFFSET_CAPTURE)) {
  167.                 return $match[0][1];
  168.             }
  169.             return false;
  170.         }
  171.     }
  172.     else {
  173.         /**
  174.          * @ignore
  175.          */
  176.         function stripos($haystack$needle$offset null{
  177.             if (preg_match('/'preg_quote($needle'/''/'$haystack$matchPREG_OFFSET_CAPTURE$offset)) {
  178.                 return $match[0][1];
  179.             }
  180.             return false;
  181.         }
  182.     }
  183. }
  184.  
  185. /** some old PHP / PCRE subpatterns only support up to xxx subpatterns in
  186.     regular expressions. Set this to false if your PCRE lib is up to date
  187.     @see GeSHi->optimize_regexp_list()
  188.     **/
  189.  
  190. define('GESHI_MAX_PCRE_SUBPATTERNS'500);
  191. /** it's also important not to generate too long regular expressions
  192.     be generous here... but keep in mind, that when reaching this limit we
  193.     still have to close open patterns. 12k should do just fine on a 16k limit.
  194.     @see GeSHi->optimize_regexp_list()
  195.     **/
  196.  
  197. define('GESHI_MAX_PCRE_LENGTH'12288);
  198.  
  199. //Number format specification
  200. /** Basic number format for integers */
  201. define('GESHI_NUMBER_INT_BASIC'1);        //Default integers \d+
  202. /** Enhanced number format for integers like seen in C */
  203. define('GESHI_NUMBER_INT_CSTYLE'2);       //Default C-Style \d+[lL]?
  204. /** Number format to highlight binary numbers with a suffix "b" */
  205. define('GESHI_NUMBER_BIN_SUFFIX'16);           //[01]+[bB]
  206. /** Number format to highlight binary numbers with a prefix % */
  207. define('GESHI_NUMBER_BIN_PREFIX_PERCENT'32);   //%[01]+
  208. /** Number format to highlight binary numbers with a prefix 0b (C) */
  209. define('GESHI_NUMBER_BIN_PREFIX_0B'64);        //0b[01]+
  210. /** Number format to highlight octal numbers with a leading zero */
  211. define('GESHI_NUMBER_OCT_PREFIX'256);           //0[0-7]+
  212. /** Number format to highlight octal numbers with a suffix of o */
  213. define('GESHI_NUMBER_OCT_SUFFIX'512);           //[0-7]+[oO]
  214. /** Number format to highlight hex numbers with a prefix 0x */
  215. define('GESHI_NUMBER_HEX_PREFIX'4096);           //0x[0-9a-fA-F]+
  216. /** Number format to highlight hex numbers with a suffix of h */
  217. define('GESHI_NUMBER_HEX_SUFFIX'8192);           //[0-9][0-9a-fA-F]*h
  218. /** Number format to highlight floating-point numbers without support for scientific notation */
  219. define('GESHI_NUMBER_FLT_NONSCI'65536);          //\d+\.\d+
  220. /** Number format to highlight floating-point numbers without support for scientific notation */
  221. define('GESHI_NUMBER_FLT_NONSCI_F'131072);       //\d+(\.\d+)?f
  222. /** Number format to highlight floating-point numbers with support for scientific notation (E) and optional leading zero */
  223. define('GESHI_NUMBER_FLT_SCI_SHORT'262144);      //\.\d+e\d+
  224. /** Number format to highlight floating-point numbers with support for scientific notation (E) and required leading digit */
  225. define('GESHI_NUMBER_FLT_SCI_ZERO'524288);       //\d+(\.\d+)?e\d+
  226. //Custom formats are passed by RX array
  227.  
  228. // Error detection - use these to analyse faults
  229. /** No sourcecode to highlight was specified
  230.  * @deprecated
  231.  */
  232. define('GESHI_ERROR_NO_INPUT'1);
  233. /** The language specified does not exist */
  234. define('GESHI_ERROR_NO_SUCH_LANG'2);
  235. /** GeSHi could not open a file for reading (generally a language file) */
  236. define('GESHI_ERROR_FILE_NOT_READABLE'3);
  237. /** The header type passed to {@link GeSHi->set_header_type()} was invalid */
  238. define('GESHI_ERROR_INVALID_HEADER_TYPE'4);
  239. /** The line number type passed to {@link GeSHi->enable_line_numbers()} was invalid */
  240. define('GESHI_ERROR_INVALID_LINE_NUMBER_TYPE'5);
  241. /**#@-*/
  242.  
  243.  
  244. /**
  245.  * The GeSHi Class.
  246.  *
  247.  * Please refer to the documentation for GeSHi 1.0.X that is available
  248.  * at http://qbnz.com/highlighter/documentation.php for more information
  249.  * about how to use this class.
  250.  *
  251.  * @package   geshi
  252.  * @author    Nigel McNie <nigel@geshi.org>, Benny Baumann <BenBE@omorphia.de>
  253.  * @copyright (C) 2004 - 2007 Nigel McNie, (C) 2007 - 2008 Benny Baumann
  254.  */
  255. class GeSHi {
  256.     /**#@+
  257.      * @access private
  258.      */
  259.     /**
  260.      * The source code to highlight
  261.      * @var string 
  262.      */
  263.     var $source '';
  264.  
  265.     /**
  266.      * The language to use when highlighting
  267.      * @var string 
  268.      */
  269.     var $language '';
  270.  
  271.     /**
  272.      * The data for the language used
  273.      * @var array 
  274.      */
  275.     var $language_data array();
  276.  
  277.     /**
  278.      * The path to the language files
  279.      * @var string 
  280.      */
  281.     var $language_path GESHI_LANG_ROOT;
  282.  
  283.     /**
  284.      * The error message associated with an error
  285.      * @var string 
  286.      * @todo check err reporting works
  287.      */
  288.     var $error false;
  289.  
  290.     /**
  291.      * Possible error messages
  292.      * @var array 
  293.      */
  294.     var $error_messages array(
  295.         GESHI_ERROR_NO_SUCH_LANG => 'GeSHi could not find the language {LANGUAGE} (using path {PATH})',
  296.         GESHI_ERROR_FILE_NOT_READABLE => 'The file specified for load_from_file was not readable',
  297.         GESHI_ERROR_INVALID_HEADER_TYPE => 'The header type specified is invalid',
  298.         GESHI_ERROR_INVALID_LINE_NUMBER_TYPE => 'The line number type specified is invalid'
  299.     );
  300.  
  301.     /**
  302.      * Whether highlighting is strict or not
  303.      * @var boolean 
  304.      */
  305.     var $strict_mode false;
  306.  
  307.     /**
  308.      * Whether to use CSS classes in output
  309.      * @var boolean 
  310.      */
  311.     var $use_classes false;
  312.  
  313.     /**
  314.      * The type of header to use. Can be one of the following
  315.      * values:
  316.      *
  317.      * - GESHI_HEADER_PRE: Source is outputted in a "pre" HTML element.
  318.      * - GESHI_HEADER_DIV: Source is outputted in a "div" HTML element.
  319.      * - GESHI_HEADER_NONE: No header is outputted.
  320.      *
  321.      * @var int 
  322.      */
  323.     var $header_type GESHI_HEADER_PRE;
  324.  
  325.     /**
  326.      * Array of permissions for which lexics should be highlighted
  327.      * @var array 
  328.      */
  329.     var $lexic_permissions array(
  330.         'KEYWORDS' =>    array(),
  331.         'COMMENTS' =>    array('MULTI' => true),
  332.         'REGEXPS' =>     array(),
  333.         'ESCAPE_CHAR' => true,
  334.         'BRACKETS' =>    true,
  335.         'SYMBOLS' =>     false,
  336.         'STRINGS' =>     true,
  337.         'NUMBERS' =>     true,
  338.         'METHODS' =>     true,
  339.         'SCRIPT' =>      true
  340.     );
  341.  
  342.     /**
  343.      * The time it took to parse the code
  344.      * @var double 
  345.      */
  346.     var $time 0;
  347.  
  348.     /**
  349.      * The content of the header block
  350.      * @var string 
  351.      */
  352.     var $header_content '';
  353.  
  354.     /**
  355.      * The content of the footer block
  356.      * @var string 
  357.      */
  358.     var $footer_content '';
  359.  
  360.     /**
  361.      * The style of the header block
  362.      * @var string 
  363.      */
  364.     var $header_content_style '';
  365.  
  366.     /**
  367.      * The style of the footer block
  368.      * @var string 
  369.      */
  370.     var $footer_content_style '';
  371.  
  372.     /**
  373.      * Tells if a block around the highlighted source should be forced
  374.      * if not using line numbering
  375.      * @var boolean 
  376.      */
  377.     var $force_code_block false;
  378.  
  379.     /**
  380.      * The styles for hyperlinks in the code
  381.      * @var array 
  382.      */
  383.     var $link_styles array();
  384.  
  385.     /**
  386.      * Whether important blocks should be recognised or not
  387.      * @var boolean 
  388.      * @deprecated
  389.      * @todo REMOVE THIS FUNCTIONALITY!
  390.      */
  391.     var $enable_important_blocks false;
  392.  
  393.     /**
  394.      * Styles for important parts of the code
  395.      * @var string 
  396.      * @deprecated
  397.      * @todo As above - rethink the whole idea of important blocks as it is buggy and
  398.      *  will be hard to implement in 1.2
  399.      */
  400.     var $important_styles 'font-weight: bold; color: red;'// Styles for important parts of the code
  401.  
  402.     /**
  403.      * Whether CSS IDs should be added to the code
  404.      * @var boolean 
  405.      */
  406.     var $add_ids false;
  407.  
  408.     /**
  409.      * Lines that should be highlighted extra
  410.      * @var array 
  411.      */
  412.     var $highlight_extra_lines array();
  413.  
  414.     /**
  415.      * Styles of lines that should be highlighted extra
  416.      * @var array 
  417.      */
  418.     var $highlight_extra_lines_styles array();
  419.  
  420.     /**
  421.      * Styles of extra-highlighted lines
  422.      * @var string 
  423.      */
  424.     var $highlight_extra_lines_style 'background-color: #ffc;';
  425.  
  426.     /**
  427.      * The line ending
  428.      * If null, nl2br() will be used on the result string.
  429.      * Otherwise, all instances of \n will be replaced with $line_ending
  430.      * @var string 
  431.      */
  432.     var $line_ending null;
  433.  
  434.     /**
  435.      * Number at which line numbers should start at
  436.      * @var int 
  437.      */
  438.     var $line_numbers_start 1;
  439.  
  440.     /**
  441.      * The overall style for this code block
  442.      * @var string 
  443.      */
  444.     var $overall_style 'font-family:monospace;';
  445.  
  446.     /**
  447.      *  The style for the actual code
  448.      * @var string 
  449.      */
  450.     var $code_style 'font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;';
  451.  
  452.     /**
  453.      * The overall class for this code block
  454.      * @var string 
  455.      */
  456.     var $overall_class '';
  457.  
  458.     /**
  459.      * The overall ID for this code block
  460.      * @var string 
  461.      */
  462.     var $overall_id '';
  463.  
  464.     /**
  465.      * Line number styles
  466.      * @var string 
  467.      */
  468.     var $line_style1 'font-weight: normal; vertical-align:top;';
  469.  
  470.     /**
  471.      * Line number styles for fancy lines
  472.      * @var string 
  473.      */
  474.     var $line_style2 'font-weight: bold; vertical-align:top;';
  475.  
  476.     /**
  477.      * Style for line numbers when GESHI_HEADER_PRE_TABLE is chosen
  478.      * @var string 
  479.      */
  480.     var $table_linenumber_style 'width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;';
  481.  
  482.     /**
  483.      * Flag for how line numbers are displayed
  484.      * @var boolean 
  485.      */
  486.     var $line_numbers GESHI_NO_LINE_NUMBERS;
  487.  
  488.     /**
  489.      * Flag to decide if multi line spans are allowed. Set it to false to make sure
  490.      * each tag is closed before and reopened after each linefeed.
  491.      * @var boolean 
  492.      */
  493.     var $allow_multiline_span true;
  494.  
  495.     /**
  496.      * The "nth" value for fancy line highlighting
  497.      * @var int 
  498.      */
  499.     var $line_nth_row 0;
  500.  
  501.     /**
  502.      * The size of tab stops
  503.      * @var int 
  504.      */
  505.     var $tab_width 8;
  506.  
  507.     /**
  508.      * Should we use language-defined tab stop widths?
  509.      * @var int 
  510.      */
  511.     var $use_language_tab_width false;
  512.  
  513.     /**
  514.      * Default target for keyword links
  515.      * @var string 
  516.      */
  517.     var $link_target '';
  518.  
  519.     /**
  520.      * The encoding to use for entity encoding
  521.      * NOTE: Used with Escape Char Sequences to fix UTF-8 handling (cf. SF#2037598)
  522.      * @var string 
  523.      */
  524.     var $encoding 'utf-8';
  525.  
  526.     /**
  527.      * Should keywords be linked?
  528.      * @var boolean 
  529.      */
  530.     var $keyword_links true;
  531.  
  532.     /**
  533.      * Currently loaded language file
  534.      * @var string 
  535.      * @since 1.0.7.22
  536.      */
  537.     var $loaded_language '';
  538.  
  539.     /**
  540.      * Wether the caches needed for parsing are built or not
  541.      *
  542.      * @var bool 
  543.      * @since 1.0.8
  544.      */
  545.     var $parse_cache_built false;
  546.  
  547.     /**
  548.      * Work around for Suhosin Patch with disabled /e modifier
  549.      *
  550.      * Note from suhosins author in config file:
  551.      * <blockquote>
  552.      *   The /e modifier inside <code>preg_replace()</code> allows code execution.
  553.      *   Often it is the cause for remote code execution exploits. It is wise to
  554.      *   deactivate this feature and test where in the application it is used.
  555.      *   The developer using the /e modifier should be made aware that he should
  556.      *   use <code>preg_replace_callback()</code> instead
  557.      * </blockquote>
  558.      *
  559.      * @var array 
  560.      * @since 1.0.8
  561.      */
  562.     var $_kw_replace_group 0;
  563.     var $_rx_key 0;
  564.  
  565.     /**
  566.      * some "callback parameters" for handle_multiline_regexps
  567.      *
  568.      * @since 1.0.8
  569.      * @access private
  570.      * @var string 
  571.      */
  572.     var $_hmr_before '';
  573.     var $_hmr_replace '';
  574.     var $_hmr_after '';
  575.     var $_hmr_key 0;
  576.  
  577.     /**#@-*/
  578.  
  579.     /**
  580.      * Creates a new GeSHi object, with source and language
  581.      *
  582.      * @param string The source code to highlight
  583.      * @param string The language to highlight the source with
  584.      * @param string The path to the language file directory. <b>This
  585.      *                is deprecated!</b> I've backported the auto path
  586.      *                detection from the 1.1.X dev branch, so now it
  587.      *                should be automatically set correctly. If you have
  588.      *                renamed the language directory however, you will
  589.      *                still need to set the path using this parameter or
  590.      *                {@link GeSHi->set_language_path()}
  591.      * @since 1.0.0
  592.      */
  593.     function GeSHi($source ''$language ''$path ''{
  594.         if (!empty($source)) {
  595.             $this->set_source($source);
  596.         }
  597.         if (!empty($language)) {
  598.             $this->set_language($language);
  599.         }
  600.         $this->set_language_path($path);
  601.     }
  602.  
  603.     /**
  604.      * Returns an error message associated with the last GeSHi operation,
  605.      * or false if no error has occured
  606.      *
  607.      * @return string|falseAn error message if there has been an error, else false
  608.      * @since  1.0.0
  609.      */
  610.     function error({
  611.         if ($this->error{
  612.             //Put some template variables for debugging here ...
  613.             $debug_tpl_vars array(
  614.                 '{LANGUAGE}' => $this->language,
  615.                 '{PATH}' => $this->language_path
  616.             );
  617.             $msg str_replace(
  618.                 array_keys($debug_tpl_vars),
  619.                 array_values($debug_tpl_vars),
  620.                 $this->error_messages[$this->error]);
  621.  
  622.             return "<br /><strong>GeSHi Error:</strong> $msg (code {$this->error})<br />";
  623.         }
  624.         return false;
  625.     }
  626.  
  627.     /**
  628.      * Gets a human-readable language name (thanks to Simon Patterson
  629.      * for the idea :))
  630.      *
  631.      * @return string The name for the current language
  632.      * @since  1.0.2
  633.      */
  634.     function get_language_name() {
  635.         if (GESHI_ERROR_NO_SUCH_LANG == $this->error) {
  636.             return $this->language_data['LANG_NAME'] . ' (Unknown Language)';
  637.         }
  638.         return $this->language_data['LANG_NAME'];
  639.     }
  640.  
  641. /**    
  642.      * Sets the source code for this object
  643.      *
  644.      * @param string The source code to highlight
  645.      * @since 1.0.0
  646.      */
  647.     function set_source($source) {
  648.         $this->source = $source;
  649.         $this->highlight_extra_lines = array();
  650.     }
  651.  
  652. /**    
  653.      * Sets the language for this object
  654.      *
  655.      * @note since 1.0.8 this function won't reset language-settings by default anymore!
  656.      *        if you need this set $force_reset = true
  657.      *
  658.      * @param string The name of the language to use
  659.      * @since 1.0.0
  660.      */
  661.     function set_language($language, $force_reset = false) {
  662.         if ($force_reset) {
  663.             $this->loaded_language = false;
  664.         }
  665.  
  666.         //Clean up the language name to prevent malicious code injection
  667.         $language = preg_replace('#[^a-zA-Z0-9\-_]#', '', $language);
  668.  
  669.         $language = strtolower($language);
  670.  
  671.         //Retreive the full filename
  672.         $file_name = $this->language_path . $language . '.php';
  673.         if ($file_name == $this->loaded_language) {
  674.             // this language is already loaded!
  675.             return;
  676.         }
  677.  
  678.         $this->language = $language;
  679.  
  680.         $this->error = false;
  681.         $this->strict_mode = GESHI_NEVER;
  682.  
  683.         //Check if we can read the desired file
  684.         if (!is_readable($file_name)) {
  685.             $this->error = GESHI_ERROR_NO_SUCH_LANG;
  686.             return;
  687.         }
  688.  
  689.         // Load the language for parsing
  690.         $this->load_language($file_name);
  691.     }
  692.  
  693. /**    
  694.      * Sets the path to the directory containing the language files. Note
  695.      * that this path is relative to the directory of the script that included
  696.      * geshi.php, NOT geshi.php itself.
  697.      *
  698.      * @param string The path to the language directory
  699.      * @since 1.0.0
  700.      * @deprecated The path to the language files should now be automatically
  701.      *              detected, so this method should no longer be needed. The
  702.      *              1.1.X branch handles manual setting of the path differently
  703.      *              so this method will disappear in 1.2.0.
  704.      */
  705.     function set_language_path($path) {
  706.         if(strpos($path,':')) {
  707.             //Security Fix to prevent external directories using fopen wrappers.
  708.             if(DIRECTORY_SEPARATOR == "\\") {
  709.                 if(!preg_match('#^[a-zA-Z]:#', $path) || false !== strpos($path, ':', 2)) {
  710.                     return;
  711.                 }
  712.             } else {
  713.                 return;
  714.             }
  715.         }
  716.         if(preg_match('#[^/a-zA-Z0-9_\.\-\\\s:]#', $path)) {
  717.             //Security Fix to prevent external directories using fopen wrappers.
  718.             return;
  719.         }
  720.         if(<a href="../geshi/core/_geshi.php.html#defineGESHI_SECURITY_PARANOID">GESHI_SECURITY_PARANOID</a> && false !== strpos($path, '/.')) {
  721.             //Security Fix to prevent external directories using fopen wrappers.
  722.             return;
  723.         }
  724.         if(<a href="../geshi/core/_geshi.php.html#defineGESHI_SECURITY_PARANOID">GESHI_SECURITY_PARANOID</a> && false !== strpos($path, '..')) {
  725.             //Security Fix to prevent external directories using fopen wrappers.
  726.             return;
  727.         }
  728.         if ($path) {
  729.             $this->language_path = ('/' == $path[strlen($path) - 1]) ? $path : $path . '/';
  730.             $this->set_language($this->language); // otherwise set_language_path has no effect
  731.         }
  732.     }
  733.  
  734. /**    
  735.      * Sets the type of header to be used.
  736.      *
  737.      * If GESHI_HEADER_DIV is used, the code is surrounded in a "div".This
  738.      * means more source code but more control over tab width and line-wrapping.
  739.      * GESHI_HEADER_PRE means that a "pre" is used - less source, but less
  740.      * control. Default is GESHI_HEADER_PRE.
  741.      *
  742.      * From 1.0.7.2, you can use GESHI_HEADER_NONE to specify that no header code
  743.      * should be outputted.
  744.      *
  745.      * @param int The type of header to be used
  746.      * @since 1.0.0
  747.      */
  748.     function set_header_type($type) {
  749.         //Check if we got a valid header type
  750.         if (!in_array($type, array(<a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_NONE">GESHI_HEADER_NONE</a>, <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_DIV">GESHI_HEADER_DIV</a>,
  751.             <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE">GESHI_HEADER_PRE</a>, <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_VALID">GESHI_HEADER_PRE_VALID</a>, <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_TABLE">GESHI_HEADER_PRE_TABLE</a>))) {
  752.             $this->error = GESHI_ERROR_INVALID_HEADER_TYPE;
  753.             return;
  754.         }
  755.  
  756.         //Set that new header type
  757.         $this->header_type = $type;
  758.     }
  759.  
  760. /**    
  761.      * Sets the styles for the code that will be outputted
  762.      * when this object is parsed. The style should be a
  763.      * string of valid stylesheet declarations
  764.      *
  765.      * @param string  The overall style for the outputted code block
  766.      * @param boolean Whether to merge the styles with the current styles or not
  767.      * @since 1.0.0
  768.      */
  769.     function set_overall_style($style, $preserve_defaults = false) {
  770.         if (!$preserve_defaults) {
  771.             $this->overall_style = $style;
  772.         } else {
  773.             $this->overall_style .= $style;
  774.         }
  775.     }
  776.  
  777. /**    
  778.      * Sets the overall classname for this block of code. This
  779.      * class can then be used in a stylesheet to style this object's
  780.      * output
  781.      *
  782.      * @param string The class name to use for this block of code
  783.      * @since 1.0.0
  784.      */
  785.     function set_overall_class($class) {
  786.         $this->overall_class = $class;
  787.     }
  788.  
  789. /**    
  790.      * Sets the overall id for this block of code. This id can then
  791.      * be used in a stylesheet to style this object's output
  792.      *
  793.      * @param string The ID to use for this block of code
  794.      * @since 1.0.0
  795.      */
  796.     function set_overall_id($id) {
  797.         $this->overall_id = $id;
  798.     }
  799.  
  800. /**    
  801.      * Sets whether CSS classes should be used to highlight the source. Default
  802.      * is off, calling this method with no arguments will turn it on
  803.      *
  804.      * @param boolean Whether to turn classes on or not
  805.      * @since 1.0.0
  806.      */
  807.     function enable_classes($flag = true) {
  808.         $this->use_classes = ($flag) ? true : false;
  809.     }
  810.  
  811. /**    
  812.      * Sets the style for the actual code. This should be a string
  813.      * containing valid stylesheet declarations. If $preserve_defaults is
  814.      * true, then styles are merged with the default styles, with the
  815.      * user defined styles having priority
  816.      *
  817.      * Note: Use this method to override any style changes you made to
  818.      * the line numbers if you are using line numbers, else the line of
  819.      * code will have the same style as the line number! Consult the
  820.      * GeSHi documentation for more information about this.
  821.      *
  822.      * @param string  The style to use for actual code
  823.      * @param boolean Whether to merge the current styles with the new styles
  824.      * @since 1.0.2
  825.      */
  826.     function set_code_style($style, $preserve_defaults = false) {
  827.         if (!$preserve_defaults) {
  828.             $this->code_style = $style;
  829.         } else {
  830.             $this->code_style .= $style;
  831.         }
  832.     }
  833.  
  834. /**    
  835.      * Sets the styles for the line numbers.
  836.      *
  837.      * @param string The style for the line numbers that are "normal"
  838.      * @param string|booleanIf a string, this is the style of the line
  839.      *         numbers that are "fancy", otherwise if boolean then this
  840.      *         defines whether the normal styles should be merged with the
  841.      *         new normal styles or not
  842.      * @param boolean If set, is the flag for whether to merge the "fancy"
  843.      *         styles with the current styles or not
  844.      * @since 1.0.2
  845.      */
  846.     function set_line_style($style1, $style2 = '', $preserve_defaults = false) {
  847.         //Check if we got 2 or three parameters
  848.         if (is_bool($style2)) {
  849.             $preserve_defaults = $style2;
  850.             $style2 = '';
  851.         }
  852.  
  853.         //Actually set the new styles
  854.         if (!$preserve_defaults) {
  855.             $this->line_style1 = $style1;
  856.             $this->line_style2 = $style2;
  857.         } else {
  858.             $this->line_style1 .= $style1;
  859.             $this->line_style2 .= $style2;
  860.         }
  861.     }
  862.  
  863. /**    
  864.      * Sets whether line numbers should be displayed.
  865.      *
  866.      * Valid values for the first parameter are:
  867.      *
  868.      *  - GESHI_NO_LINE_NUMBERS: Line numbers will not be displayed
  869.      *  - GESHI_NORMAL_LINE_NUMBERS: Line numbers will be displayed
  870.      *  - GESHI_FANCY_LINE_NUMBERS: Fancy line numbers will be displayed
  871.      *
  872.      * For fancy line numbers, the second parameter is used to signal which lines
  873.      * are to be fancy. For example, if the value of this parameter is 5 then every
  874.      * 5th line will be fancy.
  875.      *
  876.      * @param int How line numbers should be displayed
  877.      * @param int Defines which lines are fancy
  878.      * @since 1.0.0
  879.      */
  880.     function enable_line_numbers($flag, $nth_row = 5) {
  881.         if (<a href="../geshi/core/_geshi.php.html#defineGESHI_NO_LINE_NUMBERS">GESHI_NO_LINE_NUMBERS</a> != $flag && <a href="../geshi/core/_geshi.php.html#defineGESHI_NORMAL_LINE_NUMBERS">GESHI_NORMAL_LINE_NUMBERS</a> != $flag
  882.             && <a href="../geshi/core/_geshi.php.html#defineGESHI_FANCY_LINE_NUMBERS">GESHI_FANCY_LINE_NUMBERS</a> != $flag) {
  883.             $this->error = GESHI_ERROR_INVALID_LINE_NUMBER_TYPE;
  884.         }
  885.         $this->line_numbers = $flag;
  886.         $this->line_nth_row = $nth_row;
  887.     }
  888.  
  889. /**    
  890.      * Sets wether spans and other HTML markup generated by GeSHi can
  891.      * span over multiple lines or not. Defaults to true to reduce overhead.
  892.      * Set it to false if you want to manipulate the output or manually display
  893.      * the code in an ordered list.
  894.      *
  895.      * @param boolean Wether multiline spans are allowed or not
  896.      * @since 1.0.7.22
  897.      */
  898.     function enable_multiline_span($flag) {
  899.         $this->allow_multiline_span = (bool) $flag;
  900.     }
  901.  
  902. /**    
  903.      * Get current setting for multiline spans, see GeSHi->enable_multiline_span().
  904.      *
  905.      * @see enable_multiline_span
  906.      * @return bool 
  907.      */
  908.     function get_multiline_span() {
  909.         return $this->allow_multiline_span;
  910.     }
  911.  
  912. /**    
  913.      * Sets the style for a keyword group. If $preserve_defaults is
  914.      * true, then styles are merged with the default styles, with the
  915.      * user defined styles having priority
  916.      *
  917.      * @param int     The key of the keyword group to change the styles of
  918.      * @param string  The style to make the keywords
  919.      * @param boolean Whether to merge the new styles with the old or just
  920.      *                 to overwrite them
  921.      * @since 1.0.0
  922.      */
  923.     function set_keyword_group_style($key, $style, $preserve_defaults = false) {
  924.         //Set the style for this keyword group
  925.         if (!$preserve_defaults) {
  926.             $this->language_data['STYLES']['KEYWORDS'][$key] = $style;
  927.         } else {
  928.             $this->language_data['STYLES']['KEYWORDS'][$key] .= $style;
  929.         }
  930.  
  931.         //Update the lexic permissions
  932.         if (!isset($this->lexic_permissions['KEYWORDS'][$key])) {
  933.             $this->lexic_permissions['KEYWORDS'][$key] = true;
  934.         }
  935.     }
  936.  
  937. /**    
  938.      * Turns highlighting on/off for a keyword group
  939.      *
  940.      * @param int     The key of the keyword group to turn on or off
  941.      * @param boolean Whether to turn highlighting for that group on or off
  942.      * @since 1.0.0
  943.      */
  944.     function set_keyword_group_highlighting($key, $flag = true) {
  945.         $this->lexic_permissions['KEYWORDS'][$key] = ($flag) ? true : false;
  946.     }
  947.  
  948. /**    
  949.      * Sets the styles for comment groups.  If $preserve_defaults is
  950.      * true, then styles are merged with the default styles, with the
  951.      * user defined styles having priority
  952.      *
  953.      * @param int     The key of the comment group to change the styles of
  954.      * @param string  The style to make the comments
  955.      * @param boolean Whether to merge the new styles with the old or just
  956.      *                 to overwrite them
  957.      * @since 1.0.0
  958.      */
  959.     function set_comments_style($key, $style, $preserve_defaults = false) {
  960.         if (!$preserve_defaults) {
  961.             $this->language_data['STYLES']['COMMENTS'][$key] = $style;
  962.         } else {
  963.             $this->language_data['STYLES']['COMMENTS'][$key] .= $style;
  964.         }
  965.     }
  966.  
  967. /**    
  968.      * Turns highlighting on/off for comment groups
  969.      *
  970.      * @param int     The key of the comment group to turn on or off
  971.      * @param boolean Whether to turn highlighting for that group on or off
  972.      * @since 1.0.0
  973.      */
  974.     function set_comments_highlighting($key, $flag = true) {
  975.         $this->lexic_permissions['COMMENTS'][$key] = ($flag) ? true : false;
  976.     }
  977.  
  978. /**    
  979.      * Sets the styles for escaped characters. If $preserve_defaults is
  980.      * true, then styles are merged with the default styles, with the
  981.      * user defined styles having priority
  982.      *
  983.      * @param string  The style to make the escape characters
  984.      * @param boolean Whether to merge the new styles with the old or just
  985.      *                 to overwrite them
  986.      * @since 1.0.0
  987.      */
  988.     function set_escape_characters_style($style, $preserve_defaults = false) {
  989.         if (!$preserve_defaults) {
  990.             $this->language_data['STYLES']['ESCAPE_CHAR'][0] = $style;
  991.         } else {
  992.             $this->language_data['STYLES']['ESCAPE_CHAR'][0] .= $style;
  993.         }
  994.     }
  995.  
  996. /**    
  997.      * Turns highlighting on/off for escaped characters
  998.      *
  999.      * @param boolean Whether to turn highlighting for escape characters on or off
  1000.      * @since 1.0.0
  1001.      */
  1002.     function set_escape_characters_highlighting($flag = true) {
  1003.         $this->lexic_permissions['ESCAPE_CHAR'] = ($flag) ? true : false;
  1004.     }
  1005.  
  1006. /**    
  1007.      * Sets the styles for brackets. If $preserve_defaults is
  1008.      * true, then styles are merged with the default styles, with the
  1009.      * user defined styles having priority
  1010.      *
  1011.      * This method is DEPRECATED: use set_symbols_style instead.
  1012.      * This method will be removed in 1.2.X
  1013.      *
  1014.      * @param string  The style to make the brackets
  1015.      * @param boolean Whether to merge the new styles with the old or just
  1016.      *                 to overwrite them
  1017.      * @since 1.0.0
  1018.      * @deprecated In favour of set_symbols_style
  1019.      */
  1020.     function set_brackets_style($style, $preserve_defaults = false) {
  1021.         if (!$preserve_defaults) {
  1022.             $this->language_data['STYLES']['BRACKETS'][0] = $style;
  1023.         } else {
  1024.             $this->language_data['STYLES']['BRACKETS'][0] .= $style;
  1025.         }
  1026.     }
  1027.  
  1028. /**    
  1029.      * Turns highlighting on/off for brackets
  1030.      *
  1031.      * This method is DEPRECATED: use set_symbols_highlighting instead.
  1032.      * This method will be remove in 1.2.X
  1033.      *
  1034.      * @param boolean Whether to turn highlighting for brackets on or off
  1035.      * @since 1.0.0
  1036.      * @deprecated In favour of set_symbols_highlighting
  1037.      */
  1038.     function set_brackets_highlighting($flag) {
  1039.         $this->lexic_permissions['BRACKETS'] = ($flag) ? true : false;
  1040.     }
  1041.  
  1042. /**    
  1043.      * Sets the styles for symbols. If $preserve_defaults is
  1044.      * true, then styles are merged with the default styles, with the
  1045.      * user defined styles having priority
  1046.      *
  1047.      * @param string  The style to make the symbols
  1048.      * @param boolean Whether to merge the new styles with the old or just
  1049.      *                 to overwrite them
  1050.      * @param int     Tells the group of symbols for which style should be set.
  1051.      * @since 1.0.1
  1052.      */
  1053.     function set_symbols_style($style, $preserve_defaults = false, $group = 0) {
  1054.         // Update the style of symbols
  1055.         if (!$preserve_defaults) {
  1056.             $this->language_data['STYLES']['SYMBOLS'][$group] = $style;
  1057.         } else {
  1058.             $this->language_data['STYLES']['SYMBOLS'][$group] .= $style;
  1059.         }
  1060.  
  1061.         // For backward compatibility
  1062.         if (0 == $group) {
  1063.             $this->set_brackets_style ($style, $preserve_defaults);
  1064.         }
  1065.     }
  1066.  
  1067. /**    
  1068.      * Turns highlighting on/off for symbols
  1069.      *
  1070.      * @param boolean Whether to turn highlighting for symbols on or off
  1071.      * @since 1.0.0
  1072.      */
  1073.     function set_symbols_highlighting($flag) {
  1074.         // Update lexic permissions for this symbol group
  1075.         $this->lexic_permissions['SYMBOLS'] = ($flag) ? true : false;
  1076.  
  1077.         // For backward compatibility
  1078.         $this->set_brackets_highlighting ($flag);
  1079.     }
  1080.  
  1081. /**    
  1082.      * Sets the styles for strings. If $preserve_defaults is
  1083.      * true, then styles are merged with the default styles, with the
  1084.      * user defined styles having priority
  1085.      *
  1086.      * @param string  The style to make the escape characters
  1087.      * @param boolean Whether to merge the new styles with the old or just
  1088.      *                 to overwrite them
  1089.      * @since 1.0.0
  1090.      */
  1091.     function set_strings_style($style, $preserve_defaults = false) {
  1092.         if (!$preserve_defaults) {
  1093.             $this->language_data['STYLES']['STRINGS'][0] = $style;
  1094.         } else {
  1095.             $this->language_data['STYLES']['STRINGS'][0] .= $style;
  1096.         }
  1097.     }
  1098.  
  1099. /**    
  1100.      * Turns highlighting on/off for strings
  1101.      *
  1102.      * @param boolean Whether to turn highlighting for strings on or off
  1103.      * @since 1.0.0
  1104.      */
  1105.     function set_strings_highlighting($flag) {
  1106.         $this->lexic_permissions['STRINGS'] = ($flag) ? true : false;
  1107.     }
  1108.  
  1109. /**    
  1110.      * Sets the styles for numbers. If $preserve_defaults is
  1111.      * true, then styles are merged with the default styles, with the
  1112.      * user defined styles having priority
  1113.      *
  1114.      * @param string  The style to make the numbers
  1115.      * @param boolean Whether to merge the new styles with the old or just
  1116.      *                 to overwrite them
  1117.      * @since 1.0.0
  1118.      */
  1119.     function set_numbers_style($style, $preserve_defaults = false) {
  1120.         if (!$preserve_defaults) {
  1121.             $this->language_data['STYLES']['NUMBERS'][0] = $style;
  1122.         } else {
  1123.             $this->language_data['STYLES']['NUMBERS'][0] .= $style;
  1124.         }
  1125.     }
  1126.  
  1127. /**    
  1128.      * Turns highlighting on/off for numbers
  1129.      *
  1130.      * @param boolean Whether to turn highlighting for numbers on or off
  1131.      * @since 1.0.0
  1132.      */
  1133.     function set_numbers_highlighting($flag) {
  1134.         $this->lexic_permissions['NUMBERS'] = ($flag) ? true : false;
  1135.     }
  1136.  
  1137. /**    
  1138.      * Sets the styles for methods. $key is a number that references the
  1139.      * appropriate "object splitter" - see the language file for the language
  1140.      * you are highlighting to get this number. If $preserve_defaults is
  1141.      * true, then styles are merged with the default styles, with the
  1142.      * user defined styles having priority
  1143.      *
  1144.      * @param int     The key of the object splitter to change the styles of
  1145.      * @param string  The style to make the methods
  1146.      * @param boolean Whether to merge the new styles with the old or just
  1147.      *                 to overwrite them
  1148.      * @since 1.0.0
  1149.      */
  1150.     function set_methods_style($key, $style, $preserve_defaults = false) {
  1151.         if (!$preserve_defaults) {
  1152.             $this->language_data['STYLES']['METHODS'][$key] = $style;
  1153.         } else {
  1154.             $this->language_data['STYLES']['METHODS'][$key] .= $style;
  1155.         }
  1156.     }
  1157.  
  1158. /**    
  1159.      * Turns highlighting on/off for methods
  1160.      *
  1161.      * @param boolean Whether to turn highlighting for methods on or off
  1162.      * @since 1.0.0
  1163.      */
  1164.     function set_methods_highlighting($flag) {
  1165.         $this->lexic_permissions['METHODS'] = ($flag) ? true : false;
  1166.     }
  1167.  
  1168. /**    
  1169.      * Sets the styles for regexps. If $preserve_defaults is
  1170.      * true, then styles are merged with the default styles, with the
  1171.      * user defined styles having priority
  1172.      *
  1173.      * @param string  The style to make the regular expression matches
  1174.      * @param boolean Whether to merge the new styles with the old or just
  1175.      *                 to overwrite them
  1176.      * @since 1.0.0
  1177.      */
  1178.     function set_regexps_style($key, $style, $preserve_defaults = false) {
  1179.         if (!$preserve_defaults) {
  1180.             $this->language_data['STYLES']['REGEXPS'][$key] = $style;
  1181.         } else {
  1182.             $this->language_data['STYLES']['REGEXPS'][$key] .= $style;
  1183.         }
  1184.     }
  1185.  
  1186. /**    
  1187.      * Turns highlighting on/off for regexps
  1188.      *
  1189.      * @param int     The key of the regular expression group to turn on or off
  1190.      * @param boolean Whether to turn highlighting for the regular expression group on or off
  1191.      * @since 1.0.0
  1192.      */
  1193.     function set_regexps_highlighting($key, $flag) {
  1194.         $this->lexic_permissions['REGEXPS'][$key] = ($flag) ? true : false;
  1195.     }
  1196.  
  1197. /**    
  1198.      * Sets whether a set of keywords are checked for in a case sensitive manner
  1199.      *
  1200.      * @param int The key of the keyword group to change the case sensitivity of
  1201.      * @param boolean Whether to check in a case sensitive manner or not
  1202.      * @since 1.0.0
  1203.      */
  1204.     function set_case_sensitivity($key, $case) {
  1205.         $this->language_data['CASE_SENSITIVE'][$key] = ($case) ? true : false;
  1206.     }
  1207.  
  1208. /**    
  1209.      * Sets the case that keywords should use when found. Use the constants:
  1210.      *
  1211.      *  - GESHI_CAPS_NO_CHANGE: leave keywords as-is
  1212.      *  - GESHI_CAPS_UPPER: convert all keywords to uppercase where found
  1213.      *  - GESHI_CAPS_LOWER: convert all keywords to lowercase where found
  1214.      *
  1215.      * @param int A constant specifying what to do with matched keywords
  1216.      * @since 1.0.1
  1217.      */
  1218.     function set_case_keywords($case) {
  1219.         if (in_array($case, array(
  1220.             <a href="../geshi/core/_geshi.php.html#defineGESHI_CAPS_NO_CHANGE">GESHI_CAPS_NO_CHANGE</a>, <a href="../geshi/core/_geshi.php.html#defineGESHI_CAPS_UPPER">GESHI_CAPS_UPPER</a>, <a href="../geshi/core/_geshi.php.html#defineGESHI_CAPS_LOWER">GESHI_CAPS_LOWER</a>))) {
  1221.             $this->language_data['CASE_KEYWORDS'] = $case;
  1222.         }
  1223.     }
  1224.  
  1225. /**    
  1226.      * Sets how many spaces a tab is substituted for
  1227.      *
  1228.      * Widths below zero are ignored
  1229.      *
  1230.      * @param int The tab width
  1231.      * @since 1.0.0
  1232.      */
  1233.     function set_tab_width($width) {
  1234.         $this->tab_width = intval($width);
  1235.  
  1236.         //Check if it fit's the constraints:
  1237.         if ($this->tab_width < 1) {
  1238.             //Return it to the default
  1239.             $this->tab_width = 8;
  1240.         }
  1241.     }
  1242.  
  1243. /**    
  1244.      * Sets whether or not to use tab-stop width specifed by language
  1245.      *
  1246.      * @param boolean Whether to use language-specific tab-stop widths
  1247.      * @since 1.0.7.20
  1248.      */
  1249.     function set_use_language_tab_width($use) {
  1250.         $this->use_language_tab_width = (bool) $use;
  1251.     }
  1252.  
  1253. /**    
  1254.      * Returns the tab width to use, based on the current language and user
  1255.      * preference
  1256.      *
  1257.      * @return int Tab width
  1258.      * @since 1.0.7.20
  1259.      */
  1260.     function get_real_tab_width() {
  1261.         if (!$this->use_language_tab_width ||
  1262.             !isset($this->language_data['TAB_WIDTH'])) {
  1263.             return $this->tab_width;
  1264.         } else {
  1265.             return $this->language_data['TAB_WIDTH'];
  1266.         }
  1267.     }
  1268.  
  1269. /**    
  1270.      * Enables/disables strict highlighting. Default is off, calling this
  1271.      * method without parameters will turn it on. See documentation
  1272.      * for more details on strict mode and where to use it.
  1273.      *
  1274.      * @param boolean Whether to enable strict mode or not
  1275.      * @since 1.0.0
  1276.      */
  1277.     function enable_strict_mode($mode = true) {
  1278.         if (GESHI_MAYBE == $this->language_data['STRICT_MODE_APPLIES']) {
  1279.             $this->strict_mode = ($mode) ? GESHI_ALWAYS : GESHI_NEVER;
  1280.         }
  1281.     }
  1282.  
  1283. /**    
  1284.      * Disables all highlighting
  1285.      *
  1286.      * @since 1.0.0
  1287.      * @todo  Rewrite with array traversal
  1288.      * @deprecated In favour of enable_highlighting
  1289.      */
  1290.     function disable_highlighting() {
  1291.         $this->enable_highlighting(false);
  1292.     }
  1293.  
  1294. /**    
  1295.      * Enables all highlighting
  1296.      *
  1297.      * The optional flag parameter was added in version 1.0.7.21 and can be used
  1298.      * to enable (true) or disable (false) all highlighting.
  1299.      *
  1300.      * @since 1.0.0
  1301.      * @param boolean A flag specifying whether to enable or disable all highlighting
  1302.      * @todo  Rewrite with array traversal
  1303.      */
  1304.     function enable_highlighting($flag = true) {
  1305.         $flag = $flag ? true : false;
  1306.         foreach ($this->lexic_permissions as $key => $value) {
  1307.             if (is_array($value)) {
  1308.                 foreach ($value as $k => $v) {
  1309.                     $this->lexic_permissions[$key][$k] = $flag;
  1310.                 }
  1311.             } else {
  1312.                 $this->lexic_permissions[$key] = $flag;
  1313.             }
  1314.         }
  1315.  
  1316.         // Context blocks
  1317.         $this->enable_important_blocks = $flag;
  1318.     }
  1319.  
  1320. /**    
  1321.      * Given a file extension, this method returns either a valid geshi language
  1322.      * name, or the empty string if it couldn't be found
  1323.      *
  1324.      * @param string The extension to get a language name for
  1325.      * @param array  A lookup array to use instead of the default one
  1326.      * @since 1.0.5
  1327.      * @todo Re-think about how this method works (maybe make it private and/or make it
  1328.      *        a extension->lang lookup?)
  1329.      * @todo static?
  1330.      */
  1331.     function get_language_name_from_extension( $extension, $lookup = array() ) {
  1332.         if ( !is_array($lookup) || empty($lookup)) {
  1333.             $lookup = array(
  1334.                 'actionscript' => array('as'),
  1335.                 'ada' => array('a', 'ada', 'adb', 'ads'),
  1336.                 'apache' => array('conf'),
  1337.                 'asm' => array('ash', 'asm', 'inc'),
  1338.                 'asp' => array('asp'),
  1339.                 'bash' => array('sh'),
  1340.                 'bf' => array('bf'),
  1341.                 'c' => array('c', 'h'),
  1342.                 'c_mac' => array('c', 'h'),
  1343.                 'caddcl' => array(),
  1344.                 'cadlisp' => array(),
  1345.                 'cdfg' => array('cdfg'),
  1346.                 'cobol' => array('cbl'),
  1347.                 'cpp' => array('cpp', 'hpp', 'C', 'H', 'CPP', 'HPP'),
  1348.                 'csharp' => array('cs'),
  1349.                 'css' => array('css'),
  1350.                 'd' => array('d'),
  1351.                 'delphi' => array('dpk', 'dpr', 'pp', 'pas'),
  1352.                 'diff' => array('diff', 'patch'),
  1353.                 'dos' => array('bat', 'cmd'),
  1354.                 'gettext' => array('po', 'pot'),
  1355.                 'gml' => array('gml'),
  1356.                 'gnuplot' => array('plt'),
  1357.                 'groovy' => array('groovy'),
  1358.                 'haskell' => array('hs'),
  1359.                 'html4strict' => array('html', 'htm'),
  1360.                 'ini' => array('ini', 'desktop'),
  1361.                 'java' => array('java'),
  1362.                 'javascript' => array('js'),
  1363.                 'klonec' => array('kl1'),
  1364.                 'klonecpp' => array('klx'),
  1365.                 'latex' => array('tex'),
  1366.                 'lisp' => array('lisp'),
  1367.                 'lua' => array('lua'),
  1368.                 'matlab' => array('m'),
  1369.                 'mpasm' => array(),
  1370.                 'mysql' => array('sql'),
  1371.                 'nsis' => array(),
  1372.                 'objc' => array(),
  1373.                 'oobas' => array(),
  1374.                 'oracle8' => array(),
  1375.                 'oracle10' => array(),
  1376.                 'pascal' => array('pas'),
  1377.                 'perl' => array('pl', 'pm'),
  1378.                 'php' => array('php', 'php5', 'phtml', 'phps'),
  1379.                 'povray' => array('pov'),
  1380.                 'providex' => array('pvc', 'pvx'),
  1381.                 'prolog' => array('pl'),
  1382.                 'python' => array('py'),
  1383.                 'qbasic' => array('bi'),
  1384.                 'reg' => array('reg'),
  1385.                 'ruby' => array('rb'),
  1386.                 'sas' => array('sas'),
  1387.                 'scala' => array('scala'),
  1388.                 'scheme' => array('scm'),
  1389.                 'scilab' => array('sci'),
  1390.                 'smalltalk' => array('st'),
  1391.                 'smarty' => array(),
  1392.                 'tcl' => array('tcl'),
  1393.                 'vb' => array('bas'),
  1394.                 'vbnet' => array(),
  1395.                 'visualfoxpro' => array(),
  1396.                 'whitespace' => array('ws'),
  1397.                 'xml' => array('xml', 'svg'),
  1398.                 'z80' => array('z80', 'asm', 'inc')
  1399.             );
  1400.         }
  1401.  
  1402.         foreach ($lookup as $lang => $extensions) {
  1403.             if (in_array($extension, $extensions)) {
  1404.                 return $lang;
  1405.             }
  1406.         }
  1407.         return '';
  1408.     }
  1409.  
  1410. /**    
  1411.      * Given a file name, this method loads its contents in, and attempts
  1412.      * to set the language automatically. An optional lookup table can be
  1413.      * passed for looking up the language name. If not specified a default
  1414.      * table is used
  1415.      *
  1416.      * The language table is in the form
  1417.      * <pre>array(
  1418.      *   'lang_name' => array('extension', 'extension', ...),
  1419.      *   'lang_name' ...
  1420.      * );</pre>
  1421.      *
  1422.      * @param string The filename to load the source from
  1423.      * @param array  A lookup array to use instead of the default one
  1424.      * @todo Complete rethink of this and above method
  1425.      * @since 1.0.5
  1426.      */
  1427.     function load_from_file($file_name, $lookup = array()) {
  1428.         if (is_readable($file_name)) {
  1429.             $this->set_source(file_get_contents($file_name));
  1430.             $this->set_language($this->get_language_name_from_extension(substr(strrchr($file_name, '.'), 1), $lookup));
  1431.         } else {
  1432.             $this->error = GESHI_ERROR_FILE_NOT_READABLE;
  1433.         }
  1434.     }
  1435.  
  1436. /**    
  1437.      * Adds a keyword to a keyword group for highlighting
  1438.      *
  1439.      * @param int    The key of the keyword group to add the keyword to
  1440.      * @param string The word to add to the keyword group
  1441.      * @since 1.0.0
  1442.      */
  1443.     function add_keyword($key, $word) {
  1444.         if (!in_array($word, $this->language_data['KEYWORDS'][$key])) {
  1445.             $this->language_data['KEYWORDS'][$key][] = $word;
  1446.  
  1447.             //NEW in 1.0.8 don't recompile the whole optimized regexp, simply append it
  1448.             if ($this->parse_cache_built) {
  1449.                 $subkey = count($this->language_data['CACHED_KEYWORD_LISTS'][$key]) - 1;
  1450.                 $this->language_data['CACHED_KEYWORD_LISTS'][$key][$subkey] .= '|' . preg_quote($word, '/');
  1451.             }
  1452.         }
  1453.     }
  1454.  
  1455. /**    
  1456.      * Removes a keyword from a keyword group
  1457.      *
  1458.      * @param int    The key of the keyword group to remove the keyword from
  1459.      * @param string The word to remove from the keyword group
  1460.      * @param bool   Wether to automatically recompile the optimized regexp list or not.
  1461.      *                Note: if you set this to false and @see GeSHi->parse_code() was already called once,
  1462.      *                for the current language, you have to manually call @see GeSHi->optimize_keyword_group()
  1463.      *                or the removed keyword will stay in cache and still be highlighted! On the other hand
  1464.      *                it might be too expensive to recompile the regexp list for every removal if you want to
  1465.      *                remove a lot of keywords.
  1466.      * @since 1.0.0
  1467.      */
  1468.     function remove_keyword($key, $word, $recompile = true) {
  1469.         $key_to_remove = array_search($word, $this->language_data['KEYWORDS'][$key]);
  1470.         if ($key_to_remove !== false) {
  1471.             unset($this->language_data['KEYWORDS'][$key][$key_to_remove]);
  1472.  
  1473.             //NEW in 1.0.8, optionally recompile keyword group
  1474.             if ($recompile && $this->parse_cache_built) {
  1475.                 $this->optimize_keyword_group($key);
  1476.             }
  1477.         }
  1478.     }
  1479.  
  1480. /**    
  1481.      * Creates a new keyword group
  1482.      *
  1483.      * @param int    The key of the keyword group to create
  1484.      * @param string The styles for the keyword group
  1485.      * @param boolean Whether the keyword group is case sensitive ornot
  1486.      * @param array  The words to use for the keyword group
  1487.      * @since 1.0.0
  1488.      */
  1489.     function add_keyword_group($key, $styles, $case_sensitive = true, $words = array()) {
  1490.         $words = (array) $words;
  1491.         if  (empty($words)) {
  1492.             // empty word lists mess up highlighting
  1493.             return false;
  1494.         }
  1495.  
  1496.         //Add the new keyword group internally
  1497.         $this->language_data['KEYWORDS'][$key] = $words;
  1498.         $this->lexic_permissions['KEYWORDS'][$key] = true;
  1499.         $this->language_data['CASE_SENSITIVE'][$key] = $case_sensitive;
  1500.         $this->language_data['STYLES']['KEYWORDS'][$key] = $styles;
  1501.  
  1502.         //NEW in 1.0.8, cache keyword regexp
  1503.         if ($this->parse_cache_built) {
  1504.             $this->optimize_keyword_group($key);
  1505.         }
  1506.     }
  1507.  
  1508. /**    
  1509.      * Removes a keyword group
  1510.      *
  1511.      * @param int    The key of the keyword group to remove
  1512.      * @since 1.0.0
  1513.      */
  1514.     function remove_keyword_group ($key) {
  1515.         //Remove the keyword group internally
  1516.         unset($this->language_data['KEYWORDS'][$key]);
  1517.         unset($this->lexic_permissions['KEYWORDS'][$key]);
  1518.         unset($this->language_data['CASE_SENSITIVE'][$key]);
  1519.         unset($this->language_data['STYLES']['KEYWORDS'][$key]);
  1520.  
  1521.         //NEW in 1.0.8
  1522.         unset($this->language_data['CACHED_KEYWORD_LISTS'][$key]);
  1523.     }
  1524.  
  1525. /**    
  1526.      * compile optimized regexp list for keyword group
  1527.      *
  1528.      * @param int   The key of the keyword group to compile & optimize
  1529.      * @since 1.0.8
  1530.      */
  1531.     function optimize_keyword_group($key) {
  1532.         $this->language_data['CACHED_KEYWORD_LISTS'][$key] =
  1533.             $this->optimize_regexp_list($this->language_data['KEYWORDS'][$key]);
  1534.     }
  1535.  
  1536. /**    
  1537.      * Sets the content of the header block
  1538.      *
  1539.      * @param string The content of the header block
  1540.      * @since 1.0.2
  1541.      */
  1542.     function set_header_content($content) {
  1543.         $this->header_content = $content;
  1544.     }
  1545.  
  1546. /**    
  1547.      * Sets the content of the footer block
  1548.      *
  1549.      * @param string The content of the footer block
  1550.      * @since 1.0.2
  1551.      */
  1552.     function set_footer_content($content) {
  1553.         $this->footer_content = $content;
  1554.     }
  1555.  
  1556. /**    
  1557.      * Sets the style for the header content
  1558.      *
  1559.      * @param string The style for the header content
  1560.      * @since 1.0.2
  1561.      */
  1562.     function set_header_content_style($style) {
  1563.         $this->header_content_style = $style;
  1564.     }
  1565.  
  1566. /**    
  1567.      * Sets the style for the footer content
  1568.      *
  1569.      * @param string The style for the footer content
  1570.      * @since 1.0.2
  1571.      */
  1572.     function set_footer_content_style($style) {
  1573.         $this->footer_content_style = $style;
  1574.     }
  1575.  
  1576. /**    
  1577.      * Sets whether to force a surrounding block around
  1578.      * the highlighted code or not
  1579.      *
  1580.      * @param boolean Tells whether to enable or disable this feature
  1581.      * @since 1.0.7.20
  1582.      */
  1583.     function enable_inner_code_block($flag) {
  1584.         $this->force_code_block = (bool)$flag;
  1585.     }
  1586.  
  1587. /**    
  1588.      * Sets the base URL to be used for keywords
  1589.      *
  1590.      * @param int The key of the keyword group to set the URL for
  1591.      * @param string The URL to set for the group. If {FNAME} is in
  1592.      *                the url somewhere, it is replaced by the keyword
  1593.      *                that the URL is being made for
  1594.      * @since 1.0.2
  1595.      */
  1596.     function set_url_for_keyword_group($group, $url) {
  1597.         $this->language_data['URLS'][$group] = $url;
  1598.     }
  1599.  
  1600. /**    
  1601.      * Sets styles for links in code
  1602.      *
  1603.      * @param int A constant that specifies what state the style is being
  1604.      *             set for - e.g. :hover or :visited
  1605.      * @param string The styles to use for that state
  1606.      * @since 1.0.2
  1607.      */
  1608.     function set_link_styles($type, $styles) {
  1609.         $this->link_styles[$type] = $styles;
  1610.     }
  1611.  
  1612. /**    
  1613.      * Sets the target for links in code
  1614.      *
  1615.      * @param string The target for links in the code, e.g. _blank
  1616.      * @since 1.0.3
  1617.      */
  1618.     function set_link_target($target) {
  1619.         if (!$target) {
  1620.             $this->link_target = '';
  1621.         } else {
  1622.             $this->link_target = ' target="' . $target . '" ';
  1623.         }
  1624.     }
  1625.  
  1626. /**    
  1627.      * Sets styles for important parts of the code
  1628.      *
  1629.      * @param string The styles to use on important parts of the code
  1630.      * @since 1.0.2
  1631.      */
  1632.     function set_important_styles($styles) {
  1633.         $this->important_styles = $styles;
  1634.     }
  1635.  
  1636. /**    
  1637.      * Sets whether context-important blocks are highlighted
  1638.      *
  1639.      * @param boolean Tells whether to enable or disable highlighting of important blocks
  1640.      * @todo REMOVE THIS SHIZ FROM GESHI!
  1641.      * @deprecated
  1642.      * @since 1.0.2
  1643.      */
  1644.     function enable_important_blocks($flag) {
  1645.         $this->enable_important_blocks = ( $flag ) ? true : false;
  1646.     }
  1647.  
  1648. /**    
  1649.      * Whether CSS IDs should be added to each line
  1650.      *
  1651.      * @param boolean If true, IDs will be added to each line.
  1652.      * @since 1.0.2
  1653.      */
  1654.     function enable_ids($flag = true) {
  1655.         $this->add_ids = ($flag) ? true : false;
  1656.     }
  1657.  
  1658. /**    
  1659.      * Specifies which lines to highlight extra
  1660.      *
  1661.      * The extra style parameter was added in 1.0.7.21.
  1662.      *
  1663.      * @param mixed An array of line numbers to highlight, or just a line
  1664.      *               number on its own.
  1665.      * @param string A string specifying the style to use for this line.
  1666.      *               If null is specified, the default style is used.
  1667.      *               If false is specified, the line will be removed from
  1668.      *               special highlighting
  1669.      * @since 1.0.2
  1670.      * @todo  Some data replication here that could be cut down on
  1671.      */
  1672.     function highlight_lines_extra($lines, $style = null) {
  1673.         if (is_array($lines)) {
  1674.             //Split up the job using single lines at a time
  1675.             foreach ($lines as $line) {
  1676.                 $this->highlight_lines_extra($line, $style);
  1677.             }
  1678.         } else {
  1679.             //Mark the line as being highlighted specially
  1680.             $lines = intval($lines);
  1681.             $this->highlight_extra_lines[$lines] = $lines;
  1682.  
  1683.             //Decide on which style to use
  1684.             if ($style === null) { //Check if we should use default style
  1685.                 unset($this->highlight_extra_lines_styles[$lines]);
  1686.             } else if ($style === false) { //Check if to remove this line
  1687.                 unset($this->highlight_extra_lines[$lines]);
  1688.                 unset($this->highlight_extra_lines_styles[$lines]);
  1689.             } else {
  1690.                 $this->highlight_extra_lines_styles[$lines] = $style;
  1691.             }
  1692.         }
  1693.     }
  1694.  
  1695. /**    
  1696.      * Sets the style for extra-highlighted lines
  1697.      *
  1698.      * @param string The style for extra-highlighted lines
  1699.      * @since 1.0.2
  1700.      */
  1701.     function set_highlight_lines_extra_style($styles) {
  1702.         $this->highlight_extra_lines_style = $styles;
  1703.     }
  1704.  
  1705. /**    
  1706.      * Sets the line-ending
  1707.      *
  1708.      * @param string The new line-ending
  1709.      * @since 1.0.2
  1710.      */
  1711.     function set_line_ending($line_ending) {
  1712.         $this->line_ending = (string)$line_ending;
  1713.     }
  1714.  
  1715. /**    
  1716.      * Sets what number line numbers should start at. Should
  1717.      * be a positive integer, and will be converted to one.
  1718.      *
  1719.      * <b>Warning:</b> Using this method will add the "start"
  1720.      * attribute to the &lt;ol&gt; that is used for line numbering.
  1721.      * This is <b>not</b> valid XHTML strict, so if that's what you
  1722.      * care about then don't use this method. Firefox is getting
  1723.      * support for the CSS method of doing this in 1.1 and Opera
  1724.      * has support for the CSS method, but (of course) IE doesn't
  1725.      * so it's not worth doing it the CSS way yet.
  1726.      *
  1727.      * @param int The number to start line numbers at
  1728.      * @since 1.0.2
  1729.      */
  1730.     function start_line_numbers_at($number) {
  1731.         $this->line_numbers_start = abs(intval($number));
  1732.     }
  1733.  
  1734. /**    
  1735.      * Sets the encoding used for htmlspecialchars(), for international
  1736.      * support.
  1737.      *
  1738.      * NOTE: This is not needed for now because htmlspecialchars() is not
  1739.      * being used (it has a security hole in PHP4 that has not been patched).
  1740.      * Maybe in a future version it may make a return for speed reasons, but
  1741.      * I doubt it.
  1742.      *
  1743.      * @param string The encoding to use for the source
  1744.      * @since 1.0.3
  1745.      */
  1746.     function set_encoding($encoding) {
  1747.         if ($encoding) {
  1748.           $this->encoding = strtolower($encoding);
  1749.         }
  1750.     }
  1751.  
  1752. /**    
  1753.      * Turns linking of keywords on or off.
  1754.      *
  1755.      * @param boolean If true, links will be added to keywords
  1756.      * @since 1.0.2
  1757.      */
  1758.     function enable_keyword_links($enable = true) {
  1759.         $this->keyword_links = (bool) $enable;
  1760.     }
  1761.  
  1762. /**    
  1763.      * Setup caches needed for styling. This is automatically called in
  1764.      * parse_code() and get_stylesheet() when appropriate. This function helps
  1765.      * stylesheet generators as they rely on some style information being
  1766.      * preprocessed
  1767.      *
  1768.      * @since 1.0.8
  1769.      * @access private
  1770.      */
  1771.     function build_style_cache() {
  1772.         //Build the style cache needed to highlight numbers appropriate
  1773.         if($this->lexic_permissions['NUMBERS']) {
  1774.             //First check what way highlighting information for numbers are given
  1775.             if(!isset($this->language_data['NUMBERS'])) {
  1776.                 $this->language_data['NUMBERS'] = 0;
  1777.             }
  1778.  
  1779.             if(is_array($this->language_data['NUMBERS'])) {
  1780.                 $this->language_data['NUMBERS_CACHE'] = $this->language_data['NUMBERS'];
  1781.             } else {
  1782.                 $this->language_data['NUMBERS_CACHE'] = array();
  1783.                 if(!$this->language_data['NUMBERS']) {
  1784.                     $this->language_data['NUMBERS'] =
  1785.                         GESHI_NUMBER_INT_BASIC |
  1786.                         GESHI_NUMBER_FLT_NONSCI;
  1787.                 }
  1788.  
  1789.                 for($i = 0, $j = $this->language_data['NUMBERS']; $j > 0; ++$i, $j>>=1) {
  1790.                     //Rearrange style indices if required ...
  1791.                     if(isset($this->language_data['STYLES']['NUMBERS'][1<<$i])) {
  1792.                         $this->language_data['STYLES']['NUMBERS'][$i] =
  1793.                             $this->language_data['STYLES']['NUMBERS'][1<<$i];
  1794.                         unset($this->language_data['STYLES']['NUMBERS'][1<<$i]);
  1795.                     }
  1796.  
  1797.                     //Check if this bit is set for highlighting
  1798.                     if($j&1) {
  1799.                         //So this bit is set ...
  1800.                         //Check if it belongs to group 0 or the actual stylegroup
  1801.                         if(isset($this->language_data['STYLES']['NUMBERS'][$i])) {
  1802.                             $this->language_data['NUMBERS_CACHE'][$i] = 1 << $i;
  1803.                         } else {
  1804.                             if(!isset($this->language_data['NUMBERS_CACHE'][0])) {
  1805.                                 $this->language_data['NUMBERS_CACHE'][0] = 0;
  1806.                             }
  1807.                             $this->language_data['NUMBERS_CACHE'][0] |= 1 << $i;
  1808.                         }
  1809.                     }
  1810.                 }
  1811.             }
  1812.         }
  1813.     }
  1814.  
  1815. /**    
  1816.      * Setup caches needed for parsing. This is automatically called in parse_code() when appropriate.
  1817.      * This function makes stylesheet generators much faster as they do not need these caches.
  1818.      *
  1819.      * @since 1.0.8
  1820.      * @access private
  1821.      */
  1822.     function build_parse_cache() {
  1823.         // cache symbol regexp
  1824.         //As this is a costy operation, we avoid doing it for multiple groups ...
  1825.         //Instead we perform it for all symbols at once.
  1826.         //
  1827.         //For this to work, we need to reorganize the data arrays.
  1828.         if ($this->lexic_permissions['SYMBOLS'] && !empty($this->language_data['SYMBOLS'])) {
  1829.             $this->language_data['MULTIPLE_SYMBOL_GROUPS'] = count($this->language_data['STYLES']['SYMBOLS']) > 1;
  1830.  
  1831.             $this->language_data['SYMBOL_DATA'] = array();
  1832.             $symbol_preg_multi = array(); // multi char symbols
  1833.             $symbol_preg_single = array(); // single char symbols
  1834.             foreach ($this->language_data['SYMBOLS'] as $key => $symbols) {
  1835.                 if (is_array($symbols)) {
  1836.                     foreach ($symbols as $sym) {
  1837.                         $sym = $this->hsc($sym);
  1838.                         if (!isset($this->language_data['SYMBOL_DATA'][$sym])) {
  1839.                             $this->language_data['SYMBOL_DATA'][$sym] = $key;
  1840.                             if (isset($sym[1])) { // multiple chars
  1841.                                 $symbol_preg_multi[] = preg_quote($sym, '/');
  1842.                             } else { // single char
  1843.                                 if ($sym == '-') {
  1844.                                     // don't trigger range out of order error
  1845.                                     $symbol_preg_single[] = '\-';
  1846.                                 } else {
  1847.                                     $symbol_preg_single[] = preg_quote($sym, '/');
  1848.                                 }
  1849.                             }
  1850.                         }
  1851.                     }
  1852.                 } else {
  1853.                     $symbols = $this->hsc($symbols);
  1854.                     if (!isset($this->language_data['SYMBOL_DATA'][$symbols])) {
  1855.                         $this->language_data['SYMBOL_DATA'][$symbols] = 0;
  1856.                         if (isset($symbols[1])) { // multiple chars
  1857.                             $symbol_preg_multi[] = preg_quote($symbols, '/');
  1858.                         } else if ($symbols == '-') {
  1859.                             // don't trigger range out of order error
  1860.                             $symbol_preg_single[] = '\-';
  1861.                         } else { // single char
  1862.                             $symbol_preg_single[] = preg_quote($symbols, '/');
  1863.                         }
  1864.                     }
  1865.                 }
  1866.             }
  1867.  
  1868.             //Now we have an array with each possible symbol as the key and the style as the actual data.
  1869.             //This way we can set the correct style just the moment we highlight ...
  1870.             //
  1871.             //Now we need to rewrite our array to get a search string that
  1872.             $symbol_preg = array();
  1873.             if (!empty($symbol_preg_multi)) {
  1874.                 rsort($symbol_preg_multi);
  1875.                 $symbol_preg[] = implode('|', $symbol_preg_multi);
  1876.             }
  1877.             if (!empty($symbol_preg_single)) {
  1878.                 rsort($symbol_preg_single);
  1879.                 $symbol_preg[] = '[' . implode('', $symbol_preg_single) . ']';
  1880.             }
  1881.             $this->language_data['SYMBOL_SEARCH'] = implode("|", $symbol_preg);
  1882.         }
  1883.  
  1884.         // cache optimized regexp for keyword matching
  1885.         // remove old cache
  1886.         $this->language_data['CACHED_KEYWORD_LISTS'] = array();
  1887.         foreach (array_keys($this->language_data['KEYWORDS']) as $key) {
  1888.             if (!isset($this->lexic_permissions['KEYWORDS'][$key]) ||
  1889.                     $this->lexic_permissions['KEYWORDS'][$key]) {
  1890.                 $this->optimize_keyword_group($key);
  1891.             }
  1892.         }
  1893.  
  1894.         // brackets
  1895.         if ($this->lexic_permissions['BRACKETS']) {
  1896.             $this->language_data['CACHE_BRACKET_MATCH'] = array('[', ']', '(', ')', '{', '}');
  1897.             if (!$this->use_classes && isset($this->language_data['STYLES']['BRACKETS'][0])) {
  1898.                 $this->language_data['CACHE_BRACKET_REPLACE'] = array(
  1899.                     '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#91;|>',
  1900.                     '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#93;|>',
  1901.                     '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#40;|>',
  1902.                     '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#41;|>',
  1903.                     '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#123;|>',
  1904.                     '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#125;|>',
  1905.                 );
  1906.             }
  1907.             else {
  1908.                 $this->language_data['CACHE_BRACKET_REPLACE'] = array(
  1909.                     '<| class="br0">&#91;|>',
  1910.                     '<| class="br0">&#93;|>',
  1911.                     '<| class="br0">&#40;|>',
  1912.                     '<| class="br0">&#41;|>',
  1913.                     '<| class="br0">&#123;|>',
  1914.                     '<| class="br0">&#125;|>',
  1915.                 );
  1916.             }
  1917.         }
  1918.  
  1919.         //Build the parse cache needed to highlight numbers appropriate
  1920.         if($this->lexic_permissions['NUMBERS']) {
  1921.             //Check if the style rearrangements have been processed ...
  1922.             //This also does some preprocessing to check which style groups are useable ...
  1923.             if(!isset($this->language_data['NUMBERS_CACHE'])) {
  1924.                 $this->build_style_cache();
  1925.             }
  1926.  
  1927.             //Number format specification
  1928.             //All this formats are matched case-insensitively!
  1929.             static $numbers_format = array(
  1930.                 GESHI_NUMBER_INT_BASIC =>
  1931.                     '(?<![0-9a-z_\.%])(?<![\d\.]e[+\-])([1-9]\d*?|0)(?![0-9a-z\.])',
  1932.                 GESHI_NUMBER_INT_CSTYLE =>
  1933.                     '(?<![0-9a-z_\.%])(?<![\d\.]e[+\-])([1-9]\d*?|0)l(?![0-9a-z\.])',
  1934.                 GESHI_NUMBER_BIN_SUFFIX =>
  1935.                     '(?<![0-9a-z_\.])(?<![\d\.]e[+\-])[01]+?b(?![0-9a-z\.])',
  1936.                 GESHI_NUMBER_BIN_PREFIX_PERCENT =>
  1937.                     '(?<![0-9a-z_\.%])(?<![\d\.]e[+\-])%[01]+?(?![0-9a-z\.])',
  1938.                 GESHI_NUMBER_BIN_PREFIX_0B =>
  1939.                     '(?<![0-9a-z_\.%])(?<![\d\.]e[+\-])0b[01]+?(?![0-9a-z\.])',
  1940.                 GESHI_NUMBER_OCT_PREFIX =>
  1941.                     '(?<![0-9a-z_\.])(?<![\d\.]e[+\-])0[0-7]+?(?![0-9a-z\.])',
  1942.                 GESHI_NUMBER_OCT_SUFFIX =>
  1943.                     '(?<![0-9a-z_\.])(?<![\d\.]e[+\-])[0-7]+?o(?![0-9a-z\.])',
  1944.                 GESHI_NUMBER_HEX_PREFIX =>
  1945.                     '(?<![0-9a-z_\.])(?<![\d\.]e[+\-])0x[0-9a-f]+?(?![0-9a-z\.])',
  1946.                 GESHI_NUMBER_HEX_SUFFIX =>
  1947.                     '(?<![0-9a-z_\.])(?<![\d\.]e[+\-])\d[0-9a-f]*?h(?![0-9a-z\.])',
  1948.                 GESHI_NUMBER_FLT_NONSCI =>
  1949.                     '(?<![0-9a-z_\.])(?<![\d\.]e[+\-])\d+?\.\d+?(?![0-9a-z\.])',
  1950.                 GESHI_NUMBER_FLT_NONSCI_F =>
  1951.                     '(?<![0-9a-z_\.])(?<![\d\.]e[+\-])(?:\d+?(?:\.\d*?)?|\.\d+?)f(?![0-9a-z\.])',
  1952.                 GESHI_NUMBER_FLT_SCI_SHORT =>
  1953.                     '(?<![0-9a-z_\.])(?<![\d\.]e[+\-])\.\d+?(?:e[+\-]?\d+?)?(?![0-9a-z\.])',
  1954.                 GESHI_NUMBER_FLT_SCI_ZERO =>
  1955.                     '(?<![0-9a-z_\.])(?<![\d\.]e[+\-])(?:\d+?(?:\.\d*?)?|\.\d+?)(?:e[+\-]?\d+?)?(?![0-9a-z\.])'
  1956.                 );
  1957.  
  1958.             //At this step we have an associative array with flag groups for a
  1959.             //specific style or an string denoting a regexp given its index.
  1960.             $this->language_data['NUMBERS_RXCACHE'] = array();
  1961.             foreach($this->language_data['NUMBERS_CACHE'] as $key => $rxdata) {
  1962.                 if(is_string($rxdata)) {
  1963.                     $regexp = $rxdata;
  1964.                 } else {
  1965.                     //This is a bitfield of number flags to highlight:
  1966.                     //Build an array, implode them together and make this the actual RX
  1967.                     $rxuse = array();
  1968.                     for($i = 1; $i <= $rxdata; $i<<=1) {
  1969.                         if($rxdata & $i) {
  1970.                             $rxuse[] = $numbers_format[$i];
  1971.                         }
  1972.                     }
  1973.                     $regexp = implode("|", $rxuse);
  1974.                 }
  1975.  
  1976.                 $this->language_data['NUMBERS_RXCACHE'][$key] =
  1977.                     "/(?<!<\|\/NUM!)(?<!\d\/>)($regexp)(?!\|>)/i";
  1978.             }
  1979.         }
  1980.  
  1981.         $this->parse_cache_built = true;
  1982.     }
  1983.  
  1984.     /**
  1985.      * Returns the code in $this->source, highlighted and surrounded by the
  1986.      * nessecary HTML.
  1987.      *
  1988.      * This should only be called ONCE, cos it's SLOW! If you want to highlight
  1989.      * the same source multiple times, you're better off doing a whole lot of
  1990.      * str_replaces to replace the &lt;span&gt;s
  1991.      *
  1992.      * @since 1.0.0
  1993.      */
  1994.     function parse_code () {
  1995.         // Start the timer
  1996.         $start_time = microtime();
  1997.  
  1998.         // Firstly, if there is an error, we won't highlight
  1999.         if ($this->error) {
  2000.             //Escape the source for output
  2001.             $result = $this->hsc($this->source);
  2002.  
  2003.             //This fix is related to SF#1923020, but has to be applied regardless of
  2004.             //actually highlighting symbols.
  2005.             $result = str_replace(array('<SEMI>', '<PIPE>'), array(';', '|'), $result);
  2006.  
  2007.             // Timing is irrelevant
  2008.             $this->set_time($start_time, $start_time);
  2009.             $this->finalise($result);
  2010.             return $result;
  2011.         }
  2012.  
  2013.         // make sure the parse cache is up2date
  2014.         if (!$this->parse_cache_built) {
  2015.             $this->build_parse_cache();
  2016.         }
  2017.  
  2018.         // Replace all newlines to a common form.
  2019.         $code = str_replace("\r\n", "\n", $this->source);
  2020.         $code = str_replace("\r", "\n", $code);
  2021.  
  2022.         // Add spaces for regular expression matching and line numbers
  2023. //        $code = "\n" . $code . "\n";
  2024.         // Initialise various stuff
  2025.         $length           = strlen($code);
  2026.         $COMMENT_MATCHED  = false;
  2027.         $stuff_to_parse   = '';
  2028.         $endresult        = '';
  2029.  
  2030.         // "Important" selections are handled like multiline comments
  2031.         // @todo GET RID OF THIS SHIZ
  2032.         if ($this->enable_important_blocks) {
  2033.             $this->language_data['COMMENT_MULTI'][<a href="../geshi/core/_geshi.php.html#defineGESHI_START_IMPORTANT">GESHI_START_IMPORTANT</a>] = <a href="../geshi/core/_geshi.php.html#defineGESHI_END_IMPORTANT">GESHI_END_IMPORTANT</a>;
  2034.         }
  2035.  
  2036.         if ($this->strict_mode) {
  2037.             // Break the source into bits. Each bit will be a portion of the code
  2038.             // within script delimiters - for example, HTML between < and >
  2039.             $k = 0;
  2040.             $parts = array();
  2041.             $matches = array();
  2042.             $next_match_pointer = null;
  2043.             // we use a copy to unset delimiters on demand (when they are not found)
  2044.             $delim_copy = $this->language_data['SCRIPT_DELIMITERS'];
  2045.             $i = 0;
  2046.             while ($i < $length) {
  2047.                 $next_match_pos = $length + 1; // never true
  2048.                 foreach ($delim_copy as $dk => $delimiters) {
  2049.                     if(is_array($delimiters)) {
  2050.                         foreach ($delimiters as $open => $close) {
  2051.                             // make sure the cache is setup properly
  2052.                             if (!isset($matches[$dk][$open])) {
  2053.                                 $matches[$dk][$open] = array(
  2054.                                     'next_match' => -1,
  2055.                                     'dk' => $dk,
  2056.  
  2057.                                     'open' => $open, // needed for grouping of adjacent code blocks (see below)
  2058.                                     'open_strlen' => strlen($open),
  2059.  
  2060.                                     'close' => $close,
  2061.                                     'close_strlen' => strlen($close),
  2062.                                 );
  2063.                             }
  2064.                             // Get the next little bit for this opening string
  2065.                             if ($matches[$dk][$open]['next_match'] < $i) {
  2066.                                 // only find the next pos if it was not already cached
  2067.                                 $open_pos = strpos($code, $open, $i);
  2068.                                 if ($open_pos === false) {
  2069.                                     // no match for this delimiter ever
  2070.                                     unset($delim_copy[$dk][$open]);
  2071.                                     continue;
  2072.                                 }
  2073.                                 $matches[$dk][$open]['next_match'] = $open_pos;
  2074.                             }
  2075.                             if ($matches[$dk][$open]['next_match'] < $next_match_pos) {
  2076.                                 //So we got a new match, update the close_pos
  2077.                                 $matches[$dk][$open]['close_pos'] =
  2078.                                     strpos($code, $close, $matches[$dk][$open]['next_match']+1);
  2079.  
  2080.                                 $next_match_pointer =& $matches[$dk][$open];
  2081.                                 $next_match_pos = $matches[$dk][$open]['next_match'];
  2082.                             }
  2083.                         }
  2084.                     } else {
  2085.                         //So we should match an RegExp as Strict Block ...
  2086. /**                        
  2087.                          * The value in $delimiters is expected to be an RegExp
  2088.                          * containing exactly 2 matching groups:
  2089.                          *  - Group 1 is the opener
  2090.                          *  - Group 2 is the closer
  2091.                          */
  2092.                         if(!GESHI_PHP_PRE_433 && //Needs proper rewrite to work with PHP >=4.3.0; 4.3.3 is guaranteed to work.
  2093.                             preg_match($delimiters, $code, $matches_rx, PREG_OFFSET_CAPTURE, $i)) {
  2094.                             //We got a match ...
  2095.                             $matches[$dk] = array(
  2096.                                 'next_match' => $matches_rx[1][1],
  2097.                                 'dk' => $dk,
  2098.  
  2099.                                 'close_strlen' => strlen($matches_rx[2][0]),
  2100.                                 'close_pos' => $matches_rx[2][1],
  2101.                                 );
  2102.                         } else {
  2103.                             // no match for this delimiter ever
  2104.                             unset($delim_copy[$dk]);
  2105.                             continue;
  2106.                         }
  2107.  
  2108.                         if ($matches[$dk]['next_match'] <= $next_match_pos) {
  2109.                             $next_match_pointer =& $matches[$dk];
  2110.                             $next_match_pos = $matches[$dk]['next_match'];
  2111.                         }
  2112.                     }
  2113.                 }
  2114.                 // non-highlightable text
  2115.                 $parts[$k] = array(
  2116.                     1 => substr($code, $i, $next_match_pos - $i)
  2117.                 );
  2118.                 ++$k;
  2119.  
  2120.                 if ($next_match_pos > $length) {
  2121.                     // out of bounds means no next match was found
  2122.                     break;
  2123.                 }
  2124.  
  2125.                 // highlightable code
  2126.                 $parts[$k][0] = $next_match_pointer['dk'];
  2127.  
  2128.                 //Only combine for non-rx script blocks
  2129.                 if(is_array($delim_copy[$next_match_pointer['dk']])) {
  2130.                     // group adjacent script blocks, e.g. <foobar><asdf> should be one block, not three!
  2131.                     $i = $next_match_pos + $next_match_pointer['open_strlen'];
  2132.                     while (true) {
  2133.                         $close_pos = strpos($code, $next_match_pointer['close'], $i);
  2134.                         if ($close_pos == false) {
  2135.                             break;
  2136.                         }
  2137.                         $i = $close_pos + $next_match_pointer['close_strlen'];
  2138.                         if ($i == $length) {
  2139.                             break;
  2140.                         }
  2141.                         if ($code[$i] == $next_match_pointer['open'][0] && ($next_match_pointer['open_strlen'] == 1 ||
  2142.                             substr($code, $i, $next_match_pointer['open_strlen']) == $next_match_pointer['open'])) {
  2143.                             // merge adjacent but make sure we don't merge things like <tag><!-- comment -->
  2144.                             foreach ($matches as $submatches) {
  2145.                                 foreach ($submatches as $match) {
  2146.                                     if ($match['next_match'] == $i) {
  2147.                                         // a different block already matches here!
  2148.                                         break 3;
  2149.                                     }
  2150.                                 }
  2151.                             }
  2152.                         } else {
  2153.                             break;
  2154.                         }
  2155.                     }
  2156.                 } else {
  2157.                     $close_pos = $next_match_pointer['close_pos'] + $next_match_pointer['close_strlen'];
  2158.                     $i = $close_pos;
  2159.                 }
  2160.  
  2161.                 if ($close_pos === false) {
  2162.                     // no closing delimiter found!
  2163.                     $parts[$k][1] = substr($code, $next_match_pos);
  2164.                     ++$k;
  2165.                     break;
  2166.                 } else {
  2167.                     $parts[$k][1] = substr($code, $next_match_pos, $i - $next_match_pos);
  2168.                     ++$k;
  2169.                 }
  2170.             }
  2171.             unset($delim_copy, $next_match_pointer, $next_match_pos, $matches);
  2172.             $num_parts = $k;
  2173.  
  2174.             if ($num_parts == 1 && $this->strict_mode == GESHI_MAYBE) {
  2175.                 // when we have only one part, we don't have anything to highlight at all.
  2176.                 // if we have a "maybe" strict language, this should be handled as highlightable code
  2177.                 $parts = array(
  2178.                     0 => array(
  2179.                         0 => '',
  2180.                         1 => ''
  2181.                     ),
  2182.                     1 => array(
  2183.                         0 => null,
  2184.                         1 => $parts[0][1]
  2185.                     )
  2186.                 );
  2187.                 $num_parts = 2;
  2188.             }
  2189.  
  2190.         } else {
  2191.             // Not strict mode - simply dump the source into
  2192.             // the array at index 1 (the first highlightable block)
  2193.             $parts = array(
  2194.                 0 => array(
  2195.                     0 => '',
  2196.                     1 => ''
  2197.                 ),
  2198.                 1 => array(
  2199.                     0 => null,
  2200.                     1 => $code
  2201.                 )
  2202.             );
  2203.             $num_parts = 2;
  2204.         }
  2205.  
  2206.         //Unset variables we won't need any longer
  2207.         unset($code);
  2208.  
  2209.         //Preload some repeatedly used values regarding hardquotes ...
  2210.         $hq = isset($this->language_data['HARDQUOTE']) ? $this->language_data['HARDQUOTE'][0] : false;
  2211.         $hq_strlen = strlen($hq);
  2212.  
  2213.         //Preload if line numbers are to be generated afterwards
  2214.         //Added a check if line breaks should be forced even without line numbers, fixes SF#1727398
  2215.         $check_linenumbers = $this->line_numbers != <a href="../geshi/core/_geshi.php.html#defineGESHI_NO_LINE_NUMBERS">GESHI_NO_LINE_NUMBERS</a> ||
  2216.             !empty($this->highlight_extra_lines) || !$this->allow_multiline_span;
  2217.  
  2218.         //preload the escape char for faster checking ...
  2219.         $escaped_escape_char = $this->hsc($this->language_data['ESCAPE_CHAR']);
  2220.  
  2221.         // this is used for single-line comments
  2222.         $sc_disallowed_before = "";
  2223.         $sc_disallowed_after = "";
  2224.  
  2225.         if (isset($this->language_data['PARSER_CONTROL'])) {
  2226.             if (isset($this->language_data['PARSER_CONTROL']['COMMENTS'])) {
  2227.                 if (isset($this->language_data['PARSER_CONTROL']['COMMENTS']['DISALLOWED_BEFORE'])) {
  2228.                     $sc_disallowed_before = $this->language_data['PARSER_CONTROL']['COMMENTS']['DISALLOWED_BEFORE'];
  2229.                 }
  2230.                 if (isset($this->language_data['PARSER_CONTROL']['COMMENTS']['DISALLOWED_AFTER'])) {
  2231.                     $sc_disallowed_after = $this->language_data['PARSER_CONTROL']['COMMENTS']['DISALLOWED_AFTER'];
  2232.                 }
  2233.             }
  2234.         }
  2235.  
  2236.         //Fix for SF#1932083: Multichar Quotemarks unsupported
  2237.         $is_string_starter = array();
  2238.         if ($this->lexic_permissions['STRINGS']) {
  2239.             foreach ($this->language_data['QUOTEMARKS'] as $quotemark) {
  2240.                 if (!isset($is_string_starter[$quotemark[0]])) {
  2241.                     $is_string_starter[$quotemark[0]] = (string)$quotemark;
  2242.                 } else if (is_string($is_string_starter[$quotemark[0]])) {
  2243.                     $is_string_starter[$quotemark[0]] = array(
  2244.                         $is_string_starter[$quotemark[0]],
  2245.                         $quotemark);
  2246.                 } else {
  2247.                     $is_string_starter[$quotemark[0]][] = $quotemark;
  2248.                 }
  2249.             }
  2250.         }
  2251.  
  2252.         // Now we go through each part. We know that even-indexed parts are
  2253.         // code that shouldn't be highlighted, and odd-indexed parts should
  2254.         // be highlighted
  2255.         for ($key = 0; $key < $num_parts; ++$key) {
  2256.             $STRICTATTRS = '';
  2257.  
  2258.             // If this block should be highlighted...
  2259.             if (!($key & 1)) {
  2260.                 // Else not a block to highlight
  2261.                 $endresult .= $this->hsc($parts[$key][1]);
  2262.                 unset($parts[$key]);
  2263.                 continue;
  2264.             }
  2265.  
  2266.             $result = '';
  2267.             $part = $parts[$key][1];
  2268.  
  2269.             $highlight_part = true;
  2270.             if ($this->strict_mode && !is_null($parts[$key][0])) {
  2271.                 // get the class key for this block of code
  2272.                 $script_key = $parts[$key][0];
  2273.                 $highlight_part = $this->language_data['HIGHLIGHT_STRICT_BLOCK'][$script_key];
  2274.                 if ($this->language_data['STYLES']['SCRIPT'][$script_key] != '' &&
  2275.                     $this->lexic_permissions['SCRIPT']) {
  2276.                     // Add a span element around the source to
  2277.                     // highlight the overall source block
  2278.                     if (!$this->use_classes &&
  2279.                         $this->language_data['STYLES']['SCRIPT'][$script_key] != '') {
  2280.                         $attributes = ' style="' . $this->language_data['STYLES']['SCRIPT'][$script_key] . '"';
  2281.                     } else {
  2282.                         $attributes = ' class="sc' . $script_key . '"';
  2283.                     }
  2284.                     $result .= "<span$attributes>";
  2285.                     $STRICTATTRS = $attributes;
  2286.                 }
  2287.             }
  2288.  
  2289.             if ($highlight_part) {
  2290.                 // Now, highlight the code in this block. This code
  2291.                 // is really the engine of GeSHi (along with the method
  2292.                 // parse_non_string_part).
  2293.                 // cache comment regexps incrementally
  2294.                 $next_comment_regexp_key = '';
  2295.                 $next_comment_regexp_pos = -1;
  2296.                 $next_comment_multi_pos = -1;
  2297.                 $next_comment_single_pos = -1;
  2298.                 $comment_regexp_cache_per_key = array();
  2299.                 $comment_multi_cache_per_key = array();
  2300.                 $comment_single_cache_per_key = array();
  2301.                 $next_open_comment_multi = '';
  2302.                 $next_comment_single_key = '';
  2303.                 $escape_regexp_cache_per_key = array();
  2304.                 $next_escape_regexp_key = '';
  2305.                 $next_escape_regexp_pos = -1;
  2306.  
  2307.                 $length = strlen($part);
  2308.                 for ($i = 0; $i < $length; ++$i) {
  2309.                     // Get the next char
  2310.                     $char = $part[$i];
  2311.                     $char_len = 1;
  2312.  
  2313.                     // update regexp comment cache if needed
  2314.                     if (isset($this->language_data['COMMENT_REGEXP']) && $next_comment_regexp_pos < $i) {
  2315.                         $next_comment_regexp_pos = $length;
  2316.                         foreach ($this->language_data['COMMENT_REGEXP'] as $comment_key => $regexp) {
  2317.                             $match_i = false;
  2318.                             if (isset($comment_regexp_cache_per_key[$comment_key]) &&
  2319.                                 ($comment_regexp_cache_per_key[$comment_key]['pos'] >= $i ||
  2320.                                  $comment_regexp_cache_per_key[$comment_key]['pos'] === false)) {
  2321.                                 // we have already matched something
  2322.                                 if ($comment_regexp_cache_per_key[$comment_key]['pos'] === false) {
  2323.                                     // this comment is never matched
  2324.                                     continue;
  2325.                                 }
  2326.                                 $match_i = $comment_regexp_cache_per_key[$comment_key]['pos'];
  2327.                             } else if (
  2328.                                 //This is to allow use of the offset parameter in preg_match and stay as compatible with older PHP versions as possible
  2329.                                 (GESHI_PHP_PRE_433 && preg_match($regexp, substr($part, $i), $match, PREG_OFFSET_CAPTURE)) ||
  2330.                                 (!GESHI_PHP_PRE_433 && preg_match($regexp, $part, $match, PREG_OFFSET_CAPTURE, $i))
  2331.                                 ) {
  2332.                                 $match_i = $match[0][1];
  2333.                                 if (GESHI_PHP_PRE_433) {
  2334.                                     $match_i += $i;
  2335.                                 }
  2336.  
  2337.                                 $comment_regexp_cache_per_key[$comment_key] = array(
  2338.                                     'key' => $comment_key,
  2339.                                     'length' => strlen($match[0][0]),
  2340.                                     'pos' => $match_i
  2341.                                 );
  2342.                             } else {
  2343.                                 $comment_regexp_cache_per_key[$comment_key]['pos'] = false;
  2344.                                 continue;
  2345.                             }
  2346.  
  2347.                             if ($match_i !== false && $match_i < $next_comment_regexp_pos) {
  2348.                                 $next_comment_regexp_pos = $match_i;
  2349.                                 $next_comment_regexp_key = $comment_key;
  2350.                                 if ($match_i === $i) {
  2351.                                     break;
  2352.                                 }
  2353.                             }
  2354.                         }
  2355.                     }
  2356.  
  2357.                     $string_started = false;
  2358.  
  2359.                     if (isset($is_string_starter[$char])) {
  2360.                         // Possibly the start of a new string ...
  2361.                         //Check which starter it was ...
  2362.                         //Fix for SF#1932083: Multichar Quotemarks unsupported
  2363.                         if (is_array($is_string_starter[$char])) {
  2364.                             $char_new = '';
  2365.                             foreach ($is_string_starter[$char] as $testchar) {
  2366.                                 if ($testchar === substr($part, $i, strlen($testchar)) &&
  2367.                                     strlen($testchar) > strlen($char_new)) {
  2368.                                     $char_new = $testchar;
  2369.                                     $string_started = true;
  2370.                                 }
  2371.                             }
  2372.                             if ($string_started) {
  2373.                                 $char = $char_new;
  2374.                             }
  2375.                         } else {
  2376.                             $testchar = $is_string_starter[$char];
  2377.                             if ($testchar === substr($part, $i, strlen($testchar))) {
  2378.                                 $char = $testchar;
  2379.                                 $string_started = true;
  2380.                             }
  2381.                         }
  2382.                         $char_len = strlen($char);
  2383.                     }
  2384.  
  2385.                     if ($string_started && $i != $next_comment_regexp_pos) {
  2386.                         // Hand out the correct style information for this string
  2387.                         $string_key = array_search($char, $this->language_data['QUOTEMARKS']);
  2388.                         if (!isset($this->language_data['STYLES']['STRINGS'][$string_key]) ||
  2389.                             !isset($this->language_data['STYLES']['ESCAPE_CHAR'][$string_key])) {
  2390.                             $string_key = 0;
  2391.                         }
  2392.  
  2393.                         // parse the stuff before this
  2394.                         $result .= $this->parse_non_string_part($stuff_to_parse);
  2395.                         $stuff_to_parse = '';
  2396.  
  2397.                         if (!$this->use_classes) {
  2398.                             $string_attributes = ' style="' . $this->language_data['STYLES']['STRINGS'][$string_key] . '"';
  2399.                         } else {
  2400.                             $string_attributes = ' class="st'.$string_key.'"';
  2401.                         }
  2402.  
  2403.                         // now handle the string
  2404.                         $string = "<span$string_attributes>GeSHi::hsc($char);
  2405.                         $start = $i + $char_len;
  2406.                         $string_open = true;
  2407.  
  2408.                         if(empty($this->language_data['ESCAPE_REGEXP'])) {
  2409.                             $next_escape_regexp_pos = $length;
  2410.                         }
  2411.  
  2412.                         do {
  2413.                             //Get the regular ending pos ...
  2414.                             $close_pos = strpos($part, $char, $start);
  2415.                             if(false === $close_pos) {
  2416.                                 $close_pos = $length;
  2417.                             }
  2418.  
  2419.                             if($this->lexic_permissions['ESCAPE_CHAR']) {
  2420.                                 // update escape regexp cache if needed
  2421.                                 if (isset($this->language_data['ESCAPE_REGEXP']) && $next_escape_regexp_pos < $start) {
  2422.                                     $next_escape_regexp_pos = $length;
  2423.                                     foreach ($this->language_data['ESCAPE_REGEXP'] as $escape_key => $regexp) {
  2424.                                         $match_i = false;
  2425.                                         if (isset($escape_regexp_cache_per_key[$escape_key]) &&
  2426.                                             ($escape_regexp_cache_per_key[$escape_key]['pos'] >= $start ||
  2427.                                              $escape_regexp_cache_per_key[$escape_key]['pos'] === false)) {
  2428.                                             // we have already matched something
  2429.                                             if ($escape_regexp_cache_per_key[$escape_key]['pos'] === false) {
  2430.                                                 // this comment is never matched
  2431.                                                 continue;
  2432.                                             }
  2433.                                             $match_i = $escape_regexp_cache_per_key[$escape_key]['pos'];
  2434.                                         } else if (
  2435.                                             //This is to allow use of the offset parameter in preg_match and stay as compatible with older PHP versions as possible
  2436.                                             (GESHI_PHP_PRE_433 && preg_match($regexp, substr($part, $start), $match, PREG_OFFSET_CAPTURE)) ||
  2437.                                             (!GESHI_PHP_PRE_433 && preg_match($regexp, $part, $match, PREG_OFFSET_CAPTURE, $start))
  2438.                                             ) {
  2439.                                             $match_i = $match[0][1];
  2440.                                             if (GESHI_PHP_PRE_433) {
  2441.                                                 $match_i += $start;
  2442.                                             }
  2443.  
  2444.                                             $escape_regexp_cache_per_key[$escape_key] = array(
  2445.                                                 'key' => $escape_key,
  2446.                                                 'length' => strlen($match[0][0]),
  2447.                                                 'pos' => $match_i
  2448.                                             );
  2449.                                         } else {
  2450.                                             $escape_regexp_cache_per_key[$escape_key]['pos'] = false;
  2451.                                             continue;
  2452.                                         }
  2453.  
  2454.                                         if ($match_i !== false && $match_i < $next_escape_regexp_pos) {
  2455.                                             $next_escape_regexp_pos = $match_i;
  2456.                                             $next_escape_regexp_key = $escape_key;
  2457.                                             if ($match_i === $start) {
  2458.                                                 break;
  2459.                                             }
  2460.                                         }
  2461.                                     }
  2462.                                 }
  2463.  
  2464.                                 //Find the next simple escape position
  2465.                                 if('' != $this->language_data['ESCAPE_CHAR']) {
  2466.                                     $simple_escape = strpos($part, $this->language_data['ESCAPE_CHAR'], $start);
  2467.                                     if(false === $simple_escape) {
  2468.                                         $simple_escape = $length;
  2469.                                     }
  2470.                                 } else {
  2471.                                     $simple_escape = $length;
  2472.                                 }
  2473.                             } else {
  2474.                                 $next_escape_regexp_pos = $length;
  2475.                                 $simple_escape = $length;
  2476.                             }
  2477.  
  2478.                             if($simple_escape < $next_escape_regexp_pos &&
  2479.                                 $simple_escape < $length &&
  2480.                                 $simple_escape < $close_pos) {
  2481.                                 //The nexxt escape sequence is a simple one ...
  2482.                                 $es_pos = $simple_escape;
  2483.  
  2484.                                 //Add the stuff not in the string yet ...
  2485.                                 $string .= $this->hsc(substr($part, $start, $es_pos - $start));
  2486.  
  2487.                                 //Get the style for this escaped char ...
  2488.                                 if (!$this->use_classes) {
  2489.                                     $escape_char_attributes = ' style="' . $this->language_data['STYLES']['ESCAPE_CHAR'][0] . '"';
  2490.                                 } else {
  2491.                                     $escape_char_attributes = ' class="es0"';
  2492.                                 }
  2493.  
  2494.                                 //Add the style for the escape char ...
  2495.                                 $string .= "<span$escape_char_attributes>.
  2496.                                     GeSHi::hsc($this->language_data['ESCAPE_CHAR']);
  2497.  
  2498.                                 //Get the byte AFTER the ESCAPE_CHAR we just found
  2499.                                 $es_char = $part[$es_pos + 1];
  2500.                                 if ($es_char == "\n") {
  2501.                                     // don't put a newline around newlines
  2502.                                     $string .= "</span>\n";
  2503.                                     $start = $es_pos + 2;
  2504.                                 } else if (ord($es_char) >= 128) {
  2505.                                     //This is an non-ASCII char (UTF8 or single byte)
  2506.                                     //This code tries to work around SF#2037598 ...
  2507.                                     if(function_exists('mb_substr')) {
  2508.                                         $es_char_m = mb_substr(substr($part, $es_pos+1, 16), 0, 1, $this->encoding);
  2509.                                         $string .= $es_char_m . '</span>';
  2510.                                     } else if (!GESHI_PHP_PRE_433 && 'utf-8' == $this->encoding) {
  2511.                                         if(preg_match("/[\xC2-\xDF][\x80-\xBF]".
  2512.                                             "|\xE0[\xA0-\xBF][\x80-\xBF]".
  2513.                                             "|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}".
  2514.                                             "|\xED[\x80-\x9F][\x80-\xBF]".
  2515.                                             "|\xF0[\x90-\xBF][\x80-\xBF]{2}".
  2516.                                             "|[\xF1-\xF3][\x80-\xBF]{3}".
  2517.                                             "|\xF4[\x80-\x8F][\x80-\xBF]{2}/s",
  2518.                                             $part, $es_char_m, null, $es_pos + 1)) {
  2519.                                             $es_char_m = $es_char_m[0];
  2520.                                         } else {
  2521.                                             $es_char_m = $es_char;
  2522.                                         }
  2523.                                         $string .= $this->hsc($es_char_m) . '</span>';
  2524.                                     } else {
  2525.                                         $es_char_m = $this->hsc($es_char);
  2526.                                     }
  2527.                                     $start = $es_pos + strlen($es_char_m) + 1;
  2528.                                 } else {
  2529.                                     $string .= $this->hsc($es_char) . '</span>';
  2530.                                     $start = $es_pos + 2;
  2531.                                 }
  2532.                             } else if ($next_escape_regexp_pos < $length &&
  2533.                                 $next_escape_regexp_pos < $close_pos) {
  2534.                                 $es_pos = $next_escape_regexp_pos;
  2535.                                 //Add the stuff not in the string yet ...
  2536.                                 $string .= $this->hsc(substr($part, $start, $es_pos - $start));
  2537.  
  2538.                                 //Get the key and length of this match ...
  2539.                                 $escape = $escape_regexp_cache_per_key[$next_escape_regexp_key];
  2540.                                 $escape_str = substr($part, $es_pos, $escape['length']);
  2541.                                 $escape_key = $escape['key'];
  2542.  
  2543.                                 //Get the style for this escaped char ...
  2544.                                 if (!$this->use_classes) {
  2545.                                     $escape_char_attributes = ' style="' . $this->language_data['STYLES']['ESCAPE_CHAR'][$escape_key] . '"';
  2546.                                 } else {
  2547.                                     $escape_char_attributes = ' class="es' . $escape_key . '"';
  2548.                                 }
  2549.  
  2550.                                 //Add the style for the escape char ...
  2551.                                 $string .= "<span$escape_char_attributes>.
  2552.                                     $this->hsc($escape_str) . '</span>';
  2553.  
  2554.                                 $start = $es_pos + $escape['length'];
  2555.                             } else {
  2556.                                 //Copy the remainder of the string ...
  2557.                                 $string .= $this->hsc(substr($part, $start, $close_pos - $start + $char_len)) . '</span>';
  2558.                                 $start = $close_pos + $char_len;
  2559.                                 $string_open = false;
  2560.                             }
  2561.                         } while($string_open);
  2562.  
  2563.                         if ($check_linenumbers) {
  2564.                             // Are line numbers used? If, we should end the string before
  2565.                             // the newline and begin it again (so when <li>s are put in the source
  2566.                             // remains XHTML compliant)
  2567.                             // note to self: This opens up possibility of config files specifying
  2568.                             // that languages can/cannot have multiline strings???
  2569.                             $string = str_replace("\n", "</span>\n<span$string_attributes>", $string);
  2570.                         }
  2571.  
  2572.                         $result .= $string;
  2573.                         $string = '';
  2574.                         $i = $start - 1;
  2575.                         continue;
  2576.                     } else if ($this->lexic_permissions['STRINGS'] && $hq && $hq[0] == $char &&
  2577.                         substr($part, $i, $hq_strlen) == $hq) {
  2578.                         // The start of a hard quoted string
  2579.                         if (!$this->use_classes) {
  2580.                             $string_attributes = ' style="' . $this->language_data['STYLES']['STRINGS']['HARD'] . '"';
  2581.                             $escape_char_attributes = ' style="' . $this->language_data['STYLES']['ESCAPE_CHAR']['HARD'] . '"';
  2582.                         } else {
  2583.                             $string_attributes = ' class="st_h"';
  2584.                             $escape_char_attributes = ' class="es_h"';
  2585.                         }
  2586.                         // parse the stuff before this
  2587.                         $result .= $this->parse_non_string_part($stuff_to_parse);
  2588.                         $stuff_to_parse = '';
  2589.  
  2590.                         // now handle the string
  2591.                         $string = '';
  2592.  
  2593.                         // look for closing quote
  2594.                         $start = $i + $hq_strlen;
  2595.                         while ($close_pos = strpos($part, $this->language_data['HARDQUOTE'][1], $start)) {
  2596.                             $start = $close_pos + 1;
  2597.                             if ($this->lexic_permissions['ESCAPE_CHAR'] && $part[$close_pos - 1] == $this->language_data['ESCAPE_CHAR']) {
  2598.                                 // make sure this quote is not escaped
  2599.                                 foreach ($this->language_data['HARDESCAPE'] as $hardescape) {
  2600.                                     if (substr($part, $close_pos - 1, strlen($hardescape)) == $hardescape) {
  2601.                                         // check wether this quote is escaped or if it is something like '\\'
  2602.                                         $escape_char_pos = $close_pos - 1;
  2603.                                         while ($escape_char_pos > 0
  2604.                                                 && $part[$escape_char_pos - 1] == $this->language_data['ESCAPE_CHAR']) {
  2605.                                             --$escape_char_pos;
  2606.                                         }
  2607.                                         if (($close_pos - $escape_char_pos) & 1) {
  2608.                                             // uneven number of escape chars => this quote is escaped
  2609.                                             continue 2;
  2610.                                         }
  2611.                                     }
  2612.                                 }
  2613.                             }
  2614.  
  2615.                             // found closing quote
  2616.                             break;
  2617.                         }
  2618.  
  2619.                         //Found the closing delimiter?
  2620.                         if (!$close_pos) {
  2621.                             // span till the end of this $part when no closing delimiter is found
  2622.                             $close_pos = $length;
  2623.                         }
  2624.  
  2625.                         //Get the actual string
  2626.                         $string = substr($part, $i, $close_pos - $i + 1);
  2627.                         $i = $close_pos;
  2628.  
  2629.                         // handle escape chars and encode html chars
  2630.                         // (special because when we have escape chars within our string they may not be escaped)
  2631.                         if ($this->lexic_permissions['ESCAPE_CHAR'] && $this->language_data['ESCAPE_CHAR']) {
  2632.                             $start = 0;
  2633.                             $new_string = '';
  2634.                             while ($es_pos = strpos($string, $this->language_data['ESCAPE_CHAR'], $start)) {
  2635.                                 // hmtl escape stuff before
  2636.                                 $new_string .= $this->hsc(substr($string, $start, $es_pos - $start));
  2637.                                 // check if this is a hard escape
  2638.                                 foreach ($this->language_data['HARDESCAPE'] as $hardescape) {
  2639.                                     if (substr($string, $es_pos, strlen($hardescape)) == $hardescape) {
  2640.                                         // indeed, this is a hardescape
  2641.                                         $new_string .= "<span$escape_char_attributes>.
  2642.                                             $this->hsc($hardescape) . '</span>';
  2643.                                         $start = $es_pos + strlen($hardescape);
  2644.                                         continue 2;
  2645.                                     }
  2646.                                 }
  2647.                                 // not a hard escape, but a normal escape
  2648.                                 // they come in pairs of two
  2649.                                 $c = 0;
  2650.                                 while (isset($string[$es_pos + $c]) && isset($string[$es_pos + $c + 1])
  2651.                                     && $string[$es_pos + $c] == $this->language_data['ESCAPE_CHAR']
  2652.                                     && $string[$es_pos + $c + 1] == $this->language_data['ESCAPE_CHAR']) {
  2653.                                     $c += 2;
  2654.                                 }
  2655.                                 if ($c) {
  2656.                                     $new_string .= "<span$escape_char_attributes>.
  2657.                                         str_repeat($escaped_escape_char, $c) .
  2658.                                         '</span>';
  2659.                                     $start = $es_pos + $c;
  2660.                                 } else {
  2661.                                     // this is just a single lonely escape char...
  2662.                                     $new_string .= $escaped_escape_char;
  2663.                                     $start = $es_pos + 1;
  2664.                                 }
  2665.                             }
  2666.                             $string = $new_string . $this->hsc(substr($string, $start));
  2667.                         } else {
  2668.                             $string = $this->hsc($string);
  2669.                         }
  2670.  
  2671.                         if ($check_linenumbers) {
  2672.                             // Are line numbers used? If, we should end the string before
  2673.                             // the newline and begin it again (so when <li>s are put in the source
  2674.                             // remains XHTML compliant)
  2675.                             // note to self: This opens up possibility of config files specifying
  2676.                             // that languages can/cannot have multiline strings???
  2677.                             $string = str_replace("\n", "</span>\n<span$string_attributes>", $string);
  2678.                         }
  2679.  
  2680.                         $result .= "<span$string_attributes>$string . '</span>';
  2681.                         $string = '';
  2682.                         continue;
  2683.                     } else {
  2684.                         //Have a look for regexp comments
  2685.                         if ($i == $next_comment_regexp_pos) {
  2686.                             $COMMENT_MATCHED = true;
  2687.                             $comment = $comment_regexp_cache_per_key[$next_comment_regexp_key];
  2688.                             $test_str = $this->hsc(substr($part, $i, $comment['length']));
  2689.  
  2690.                             //@todo If remove important do remove here
  2691.                             if ($this->lexic_permissions['COMMENTS']['MULTI']) {
  2692.                                 if (!$this->use_classes) {
  2693.                                     $attributes = ' style="' . $this->language_data['STYLES']['COMMENTS'][$comment['key']] . '"';
  2694.                                 } else {
  2695.                                     $attributes = ' class="co' . $comment['key'] . '"';
  2696.                                 }
  2697.  
  2698.                                 $test_str = "<span$attributes>$test_str . "</span>";
  2699.  
  2700.                                 // Short-cut through all the multiline code
  2701.                                 if ($check_linenumbers) {
  2702.                                     // strreplace to put close span and open span around multiline newlines
  2703.                                     $test_str = str_replace(
  2704.                                         "\n", "</span>\n<span$attributes>",
  2705.                                         str_replace("\n ", "\n&nbsp;", $test_str)
  2706.                                     );
  2707.                                 }
  2708.                             }
  2709.  
  2710.                             $i += $comment['length'] - 1;
  2711.  
  2712.                             // parse the rest
  2713.                             $result .= $this->parse_non_string_part($stuff_to_parse);
  2714.                             $stuff_to_parse = '';
  2715.                         }
  2716.  
  2717.                         // If we haven't matched a regexp comment, try multi-line comments
  2718.                         if (!$COMMENT_MATCHED) {
  2719.                             // Is this a multiline comment?
  2720.                             if (!empty($this->language_data['COMMENT_MULTI']) && $next_comment_multi_pos < $i) {
  2721.                                 $next_comment_multi_pos = $length;
  2722.                                 foreach ($this->language_data['COMMENT_MULTI'] as $open => $close) {
  2723.                                     $match_i = false;
  2724.                                     if (isset($comment_multi_cache_per_key[$open]) &&
  2725.                                         ($comment_multi_cache_per_key[$open] >= $i ||
  2726.                                          $comment_multi_cache_per_key[$open] === false)) {
  2727.                                         // we have already matched something
  2728.                                         if ($comment_multi_cache_per_key[$open] === false) {
  2729.                                             // this comment is never matched
  2730.                                             continue;
  2731.                                         }
  2732.                                         $match_i = $comment_multi_cache_per_key[$open];
  2733.                                     } else if (($match_i = stripos($part, $open, $i)) !== false) {
  2734.                                         $comment_multi_cache_per_key[$open] = $match_i;
  2735.                                     } else {
  2736.                                         $comment_multi_cache_per_key[$open] = false;
  2737.                                         continue;
  2738.                                     }
  2739.                                     if ($match_i !== false && $match_i < $next_comment_multi_pos) {
  2740.                                         $next_comment_multi_pos = $match_i;
  2741.                                         $next_open_comment_multi = $open;
  2742.                                         if ($match_i === $i) {
  2743.                                             break;
  2744.                                         }
  2745.                                     }
  2746.                                 }
  2747.                             }
  2748.                             if ($i == $next_comment_multi_pos) {
  2749.                                 $open = $next_open_comment_multi;
  2750.                                 $close = $this->language_data['COMMENT_MULTI'][$open];
  2751.                                 $open_strlen = strlen($open);
  2752.                                 $close_strlen = strlen($close);
  2753.                                 $COMMENT_MATCHED = true;
  2754.                                 $test_str_match = $open;
  2755.                                 //@todo If remove important do remove here
  2756.                                 if ($this->lexic_permissions['COMMENTS']['MULTI'] ||
  2757.                                     $open == <a href="../geshi/core/_geshi.php.html#defineGESHI_START_IMPORTANT">GESHI_START_IMPORTANT</a>) {
  2758.                                     if ($open != <a href="../geshi/core/_geshi.php.html#defineGESHI_START_IMPORTANT">GESHI_START_IMPORTANT</a>) {
  2759.                                         if (!$this->use_classes) {
  2760.                                             $attributes = ' style="' . $this->language_data['STYLES']['COMMENTS']['MULTI'] . '"';
  2761.                                         } else {
  2762.                                             $attributes = ' class="coMULTI"';
  2763.                                         }
  2764.                                         $test_str = "<span$attributes>$this->hsc($open);
  2765.                                     } else {
  2766.                                         if (!$this->use_classes) {
  2767.                                             $attributes = ' style="' . $this->important_styles . '"';
  2768.                                         } else {
  2769.                                             $attributes = ' class="imp"';
  2770.                                         }
  2771.  
  2772.                                         // We don't include the start of the comment if it's an
  2773.                                         // "important" part
  2774.                                         $test_str = "<span$attributes>";
  2775.                                     }
  2776.                                 } else {
  2777.                                     $test_str = $this->hsc($open);
  2778.                                 }
  2779.  
  2780.                                 $close_pos = strpos( $part, $close, $i + $open_strlen );
  2781.  
  2782.                                 if ($close_pos === false) {
  2783.                                     $close_pos = $length;
  2784.                                 }
  2785.  
  2786.                                 // Short-cut through all the multiline code
  2787.                                 $rest_of_comment = $this->hsc(substr($part, $i + $open_strlen, $close_pos - $i - $open_strlen + $close_strlen));
  2788.                                 if (($this->lexic_permissions['COMMENTS']['MULTI'] ||
  2789.                                     $test_str_match == <a href="../geshi/core/_geshi.php.html#defineGESHI_START_IMPORTANT">GESHI_START_IMPORTANT</a>) &&
  2790.                                     $check_linenumbers) {
  2791.  
  2792.                                     // strreplace to put close span and open span around multiline newlines
  2793.                                     $test_str .= str_replace(
  2794.                                         "\n", "</span>\n<span$attributes>",
  2795.                                         str_replace("\n ", "\n&nbsp;", $rest_of_comment)
  2796.                                     );
  2797.                                 } else {
  2798.                                     $test_str .= $rest_of_comment;
  2799.                                 }
  2800.  
  2801.                                 if ($this->lexic_permissions['COMMENTS']['MULTI'] ||
  2802.                                     $test_str_match == <a href="../geshi/core/_geshi.php.html#defineGESHI_START_IMPORTANT">GESHI_START_IMPORTANT</a>) {
  2803.                                     $test_str .= '</span>';
  2804.                                 }
  2805.  
  2806.                                 $i = $close_pos + $close_strlen - 1;
  2807.  
  2808.                                 // parse the rest
  2809.                                 $result .= $this->parse_non_string_part($stuff_to_parse);
  2810.                                 $stuff_to_parse = '';
  2811.                             }
  2812.                         }
  2813.  
  2814.                         // If we haven't matched a multiline comment, try single-line comments
  2815.                         if (!$COMMENT_MATCHED) {
  2816.                             // cache potential single line comment occurances
  2817.                             if (!empty($this->language_data['COMMENT_SINGLE']) && $next_comment_single_pos < $i) {
  2818.                                 $next_comment_single_pos = $length;
  2819.                                 foreach ($this->language_data['COMMENT_SINGLE'] as $comment_key => $comment_mark) {
  2820.                                     $match_i = false;
  2821.                                     if (isset($comment_single_cache_per_key[$comment_key]) &&
  2822.                                         ($comment_single_cache_per_key[$comment_key] >= $i ||
  2823.                                          $comment_single_cache_per_key[$comment_key] === false)) {
  2824.                                         // we have already matched something
  2825.                                         if ($comment_single_cache_per_key[$comment_key] === false) {
  2826.                                             // this comment is never matched
  2827.                                             continue;
  2828.                                         }
  2829.                                         $match_i = $comment_single_cache_per_key[$comment_key];
  2830.                                     } else if (
  2831.                                         // case sensitive comments
  2832.                                         ($this->language_data['CASE_SENSITIVE'][GESHI_COMMENTS] &&
  2833.                                         ($match_i = stripos($part, $comment_mark, $i)) !== false) ||
  2834.                                         // non case sensitive
  2835.                                         (!$this->language_data['CASE_SENSITIVE'][GESHI_COMMENTS] &&
  2836.                                           (($match_i = strpos($part, $comment_mark, $i)) !== false))) {
  2837.                                         $comment_single_cache_per_key[$comment_key] = $match_i;
  2838.                                     } else {
  2839.                                         $comment_single_cache_per_key[$comment_key] = false;
  2840.                                         continue;
  2841.                                     }
  2842.                                     if ($match_i !== false && $match_i < $next_comment_single_pos) {
  2843.                                         $next_comment_single_pos = $match_i;
  2844.                                         $next_comment_single_key = $comment_key;
  2845.                                         if ($match_i === $i) {
  2846.                                             break;
  2847.                                         }
  2848.                                     }
  2849.                                 }
  2850.                             }
  2851.                             if ($next_comment_single_pos == $i) {
  2852.                                 $comment_key = $next_comment_single_key;
  2853.                                 $comment_mark = $this->language_data['COMMENT_SINGLE'][$comment_key];
  2854.                                 $com_len = strlen($comment_mark);
  2855.  
  2856.                                 // This check will find special variables like $# in bash
  2857.                                 // or compiler directives of Delphi beginning {$
  2858.                                 if ((empty($sc_disallowed_before) || ($i == 0) ||
  2859.                                     (false === strpos($sc_disallowed_before, $part[$i-1]))) &&
  2860.                                     (empty($sc_disallowed_after) || ($length <= $i + $com_len) ||
  2861.                                     (false === strpos($sc_disallowed_after, $part[$i + $com_len]))))
  2862.                                 {
  2863.                                     // this is a valid comment
  2864.                                     $COMMENT_MATCHED = true;
  2865.                                     if ($this->lexic_permissions['COMMENTS'][$comment_key]) {
  2866.                                         if (!$this->use_classes) {
  2867.                                             $attributes = ' style="' . $this->language_data['STYLES']['COMMENTS'][$comment_key] . '"';
  2868.                                         } else {
  2869.                                             $attributes = ' class="co' . $comment_key . '"';
  2870.                                         }
  2871.                                         $test_str = "<span$attributes>$this->hsc($this->change_case($comment_mark));
  2872.                                     } else {
  2873.                                         $test_str = $this->hsc($comment_mark);
  2874.                                     }
  2875.  
  2876.                                     //Check if this comment is the last in the source
  2877.                                     $close_pos = strpos($part, "\n", $i);
  2878.                                     $oops = false;
  2879.                                     if ($close_pos === false) {
  2880.                                         $close_pos = $length;
  2881.                                         $oops = true;
  2882.                                     }
  2883.                                     $test_str .= $this->hsc(substr($part, $i + $com_len, $close_pos - $i - $com_len));
  2884.                                     if ($this->lexic_permissions['COMMENTS'][$comment_key]) {
  2885.                                         $test_str .= "</span>";
  2886.                                     }
  2887.  
  2888.                                     // Take into account that the comment might be the last in the source
  2889.                                     if (!$oops) {
  2890.                                       $test_str .= "\n";
  2891.                                     }
  2892.  
  2893.                                     $i = $close_pos;
  2894.  
  2895.                                     // parse the rest
  2896.                                     $result .= $this->parse_non_string_part($stuff_to_parse);
  2897.                                     $stuff_to_parse = '';
  2898.                                 }
  2899.                             }
  2900.                         }
  2901.                     }
  2902.  
  2903.                     // Where are we adding this char?
  2904.                     if (!$COMMENT_MATCHED) {
  2905.                         $stuff_to_parse .= $char;
  2906.                     } else {
  2907.                         $result .= $test_str;
  2908.                         unset($test_str);
  2909.                         $COMMENT_MATCHED = false;
  2910.                     }
  2911.                 }
  2912.                 // Parse the last bit
  2913.                 $result .= $this->parse_non_string_part($stuff_to_parse);
  2914.                 $stuff_to_parse = '';
  2915.             } else {
  2916.                 $result .= $this->hsc($part);
  2917.             }
  2918.             // Close the <span> that surrounds the block
  2919.             if ($STRICTATTRS != '') {
  2920.                 $result = str_replace("\n", "</span>\n<span$STRICTATTRS>", $result);
  2921.                 $result .= '</span>';
  2922.             }
  2923.  
  2924.             $endresult .= $result;
  2925.             unset($part, $parts[$key], $result);
  2926.         }
  2927.  
  2928.         //This fix is related to SF#1923020, but has to be applied regardless of
  2929.         //actually highlighting symbols.
  2930. /** NOTE: memorypeak #3 */        
  2931.         $endresult = str_replace(array('<SEMI>', '<PIPE>'), array(';', '|'), $endresult);
  2932.  
  2933. //        // Parse the last stuff (redundant?)
  2934. //        $result .= $this->parse_non_string_part($stuff_to_parse);
  2935.         // Lop off the very first and last spaces
  2936. //        $result = substr($result, 1, -1);
  2937.         // We're finished: stop timing
  2938.         $this->set_time($start_time, microtime());
  2939.  
  2940.         $this->finalise($endresult);
  2941.         return $endresult;
  2942.     }
  2943.  
  2944. /**    
  2945.      * Swaps out spaces and tabs for HTML indentation. Not needed if
  2946.      * the code is in a pre block...
  2947.      *
  2948.      * @param  string The source to indent (reference!)
  2949.      * @since  1.0.0
  2950.      * @access private
  2951.      */
  2952.     function indent(&$result) {
  2953.         /// Replace tabs with the correct number of spaces
  2954.         if (false !== strpos($result, "\t")) {
  2955.             $lines = explode("\n", $result);
  2956.             $result = null;//Save memory while we process the lines individually
  2957.             $tab_width = $this->get_real_tab_width();
  2958.             $tab_string = '&nbsp;' . str_repeat(' ', $tab_width);
  2959.  
  2960.             for ($key = 0, $n = count($lines); $key < $n; $key++) {
  2961.                 $line = $lines[$key];
  2962.                 if (false === strpos($line, "\t")) {
  2963.                     continue;
  2964.                 }
  2965.  
  2966.                 $pos = 0;
  2967.                 $length = strlen($line);
  2968.                 $lines[$key] = ''; // reduce memory
  2969.                 $IN_TAG = false;
  2970.                 for ($i = 0; $i < $length; ++$i) {
  2971.                     $char = $line[$i];
  2972.                     // Simple engine to work out whether we're in a tag.
  2973.                     // If we are we modify $pos. This is so we ignore HTML
  2974.                     // in the line and only workout the tab replacement
  2975.                     // via the actual content of the string
  2976.                     // This test could be improved to include strings in the
  2977.                     // html so that < or > would be allowed in user's styles
  2978.                     // (e.g. quotes: '<' '>'; or similar)
  2979.                     if ($IN_TAG) {
  2980.                         if ('>' == $char) {
  2981.                             $IN_TAG = false;
  2982.                         }
  2983.                         $lines[$key] .= $char;
  2984.                     } else if ('<' == $char) {
  2985.                         $IN_TAG = true;
  2986.                         $lines[$key] .= '<';
  2987.                     } else if ('&' == $char) {
  2988.                         $substr = substr($line, $i + 3, 5);
  2989.                         $posi = strpos($substr, ';');
  2990.                         if (false === $posi) {
  2991.                             ++$pos;
  2992.                         } else {
  2993.                             $pos -= $posi+2;
  2994.                         }
  2995.                         $lines[$key] .= $char;
  2996.                     } else if ("\t" == $char) {
  2997.                         $str = '';
  2998.                         // OPTIMISE - move $strs out. Make an array:
  2999.                         // $tabs = array(
  3000.                         //  1 => '&nbsp;',
  3001.                         //  2 => '&nbsp; ',
  3002.                         //  3 => '&nbsp; &nbsp;' etc etc
  3003.                         // to use instead of building a string every time
  3004.                         $tab_end_width = $tab_width - ($pos % $tab_width); //Moved out of the look as it doesn't change within the loop
  3005.                         if (($pos & 1) || 1 == $tab_end_width) {
  3006.                             $str .= substr($tab_string, 6, $tab_end_width);
  3007.                         } else {
  3008.                             $str .= substr($tab_string, 0, $tab_end_width+5);
  3009.                         }
  3010.                         $lines[$key] .= $str;
  3011.                         $pos += $tab_end_width;
  3012.  
  3013.                         if (false === strpos($line, "\t", $i + 1)) {
  3014.                             $lines[$key] .= substr($line, $i + 1);
  3015.                             break;
  3016.                         }
  3017.                     } else if (0 == $pos && ' ' == $char) {
  3018.                         $lines[$key] .= '&nbsp;';
  3019.                         ++$pos;
  3020.                     } else {
  3021.                         $lines[$key] .= $char;
  3022.                         ++$pos;
  3023.                     }
  3024.                 }
  3025.             }
  3026.             $result = implode("\n", $lines);
  3027.             unset($lines);//We don't need the lines separated beyond this --- free them!
  3028.         }
  3029.         // Other whitespace
  3030.         // BenBE: Fix to reduce the number of replacements to be done
  3031.         $result = preg_replace('/^ /m', '&nbsp;', $result);
  3032.         $result = str_replace('  ', ' &nbsp;', $result);
  3033.  
  3034.         if ($this->line_numbers == <a href="../geshi/core/_geshi.php.html#defineGESHI_NO_LINE_NUMBERS">GESHI_NO_LINE_NUMBERS</a>) {
  3035.             if ($this->line_ending === null) {
  3036.                 $result = nl2br($result);
  3037.             } else {
  3038.                 $result = str_replace("\n", $this->line_ending, $result);
  3039.             }
  3040.         }
  3041.     }
  3042.  
  3043. /**    
  3044.      * Changes the case of a keyword for those languages where a change is asked for
  3045.      *
  3046.      * @param  string The keyword to change the case of
  3047.      * @return string The keyword with its case changed
  3048.      * @since  1.0.0
  3049.      * @access private
  3050.      */
  3051.     function change_case($instr) {
  3052.         switch ($this->language_data['CASE_KEYWORDS']) {
  3053.             case <a href="../geshi/core/_geshi.php.html#defineGESHI_CAPS_UPPER">GESHI_CAPS_UPPER</a>:
  3054.                 return strtoupper($instr);
  3055.             case <a href="../geshi/core/_geshi.php.html#defineGESHI_CAPS_LOWER">GESHI_CAPS_LOWER</a>:
  3056.                 return strtolower($instr);
  3057.             default:
  3058.                 return $instr;
  3059.         }
  3060.     }
  3061.  
  3062. /**    
  3063.      * Handles replacements of keywords to include markup and links if requested
  3064.      *
  3065.      * @param  string The keyword to add the Markup to
  3066.      * @return The HTML for the match found
  3067.      * @since  1.0.8
  3068.      * @access private
  3069.      *
  3070.      * @todo   Get rid of ender in keyword links
  3071.      */
  3072.     function handle_keyword_replace($match) {
  3073.         $k = $this->_kw_replace_group;
  3074.         $keyword = $match[0];
  3075.  
  3076.         $before = '';
  3077.         $after = '';
  3078.  
  3079.         if ($this->keyword_links) {
  3080.             // Keyword links have been ebabled
  3081.             if (isset($this->language_data['URLS'][$k]) &&
  3082.                 $this->language_data['URLS'][$k] != '') {
  3083.                 // There is a base group for this keyword
  3084.                 // Old system: strtolower
  3085.                 //$keyword = ( $this->language_data['CASE_SENSITIVE'][$group] ) ? $keyword : strtolower($keyword);
  3086.                 // New system: get keyword from language file to get correct case
  3087.                 if (!$this->language_data['CASE_SENSITIVE'][$k] &&
  3088.                     strpos($this->language_data['URLS'][$k], '{FNAME}') !== false) {
  3089.                     foreach ($this->language_data['KEYWORDS'][$k] as $word) {
  3090.                         if (strcasecmp($word, $keyword) == 0) {
  3091.                             break;
  3092.                         }
  3093.                     }
  3094.                 } else {
  3095.                     $word = $keyword;
  3096.                 }
  3097.  
  3098.                 $before = '<|UR1|"' .
  3099.                     str_replace(
  3100.                         array(
  3101.                             '{FNAME}',
  3102.                             '{FNAMEL}',
  3103.                             '{FNAMEU}',
  3104.                             '.'),
  3105.                         array(
  3106.                             str_replace('+', '%20', urlencode($this->hsc($word))),
  3107.                             str_replace('+', '%20', urlencode($this->hsc(strtolower($word)))),
  3108.                             str_replace('+', '%20', urlencode($this->hsc(strtoupper($word)))),
  3109.                             '<DOT>'),
  3110.                         $this->language_data['URLS'][$k]
  3111.                     ) . '">';
  3112.                 $after = '</a>';
  3113.             }
  3114.         }
  3115.  
  3116.         return $before . '<|/'$k .'/>' . $this->change_case($keyword) . '|>' . $after;
  3117.     }
  3118.  
  3119. /**    
  3120.      * handles regular expressions highlighting-definitions with callback functions
  3121.      *
  3122.      * @note this is a callback, don't use it directly
  3123.      *
  3124.      * @param array the matches array
  3125.      * @return The highlighted string
  3126.      * @since 1.0.8
  3127.      * @access private
  3128.      */
  3129.     function handle_regexps_callback($matches) {
  3130.         // before: "' style=\"' . call_user_func(\"$func\", '\\1') . '\"\\1|>'",
  3131.         return  ' style="' . call_user_func($this->language_data['STYLES']['REGEXPS'][$this->_rx_key], $matches[1]) . '"'$matches[1] . '|>';
  3132.     }
  3133.  
  3134. /**    
  3135.      * handles newlines in REGEXPS matches. Set the _hmr_* vars before calling this
  3136.      *
  3137.      * @note this is a callback, don't use it directly
  3138.      *
  3139.      * @param array the matches array
  3140.      * @return string 
  3141.      * @since 1.0.8
  3142.      * @access private
  3143.      */
  3144.     function handle_multiline_regexps($matches) {
  3145.         $before = $this->_hmr_before;
  3146.         $after = $this->_hmr_after;
  3147.         if ($this->_hmr_replace) {
  3148.             $replace = $this->_hmr_replace;
  3149.             $search = array();
  3150.  
  3151.             foreach (array_keys($matches) as $k) {
  3152.                 $search[] = '\\' . $k;
  3153.             }
  3154.  
  3155.             $before = str_replace($search, $matches, $before);
  3156.             $after = str_replace($search, $matches, $after);
  3157.             $replace = str_replace($search, $matches, $replace);
  3158.         } else {
  3159.             $replace = $matches[0];
  3160.         }
  3161.         return $before
  3162.                     . '<|!REG3XP' . $this->_hmr_key .'!>'
  3163.                         . str_replace("\n", "|>\n<|!REG3XP" . $this->_hmr_key . '!>', $replace)
  3164.                     . '|>'
  3165.               . $after;
  3166.     }
  3167.  
  3168. /**    
  3169.      * Takes a string that has no strings or comments in it, and highlights
  3170.      * stuff like keywords, numbers and methods.
  3171.      *
  3172.      * @param string The string to parse for keyword, numbers etc.
  3173.      * @since 1.0.0
  3174.      * @access private
  3175.      * @todo BUGGY! Why? Why not build string and return?
  3176.      */
  3177.     function parse_non_string_part($stuff_to_parse) {
  3178.         $stuff_to_parse = ' ' . $this->hsc($stuff_to_parse);
  3179.  
  3180.         // Regular expressions
  3181.         foreach ($this->language_data['REGEXPS'] as $key => $regexp) {
  3182.             if ($this->lexic_permissions['REGEXPS'][$key]) {
  3183.                 if (is_array($regexp)) {
  3184.                     if ($this->line_numbers != <a href="../geshi/core/_geshi.php.html#defineGESHI_NO_LINE_NUMBERS">GESHI_NO_LINE_NUMBERS</a>) {
  3185.                         // produce valid HTML when we match multiple lines
  3186.                         $this->_hmr_replace = $regexp[GESHI_REPLACE];
  3187.                         $this->_hmr_before = $regexp[GESHI_BEFORE];
  3188.                         $this->_hmr_key = $key;
  3189.                         $this->_hmr_after = $regexp[GESHI_AFTER];
  3190.                         $stuff_to_parse = preg_replace_callback(
  3191.                             "/" . $regexp[GESHI_SEARCH] . "/{$regexp[GESHI_MODIFIERS]}",
  3192.                             array($this, 'handle_multiline_regexps'),
  3193.                             $stuff_to_parse);
  3194.                         $this->_hmr_replace = false;
  3195.                         $this->_hmr_before = '';
  3196.                         $this->_hmr_after = '';
  3197.                     } else {
  3198.                         $stuff_to_parse = preg_replace(
  3199.                             '/' . $regexp[GESHI_SEARCH] . '/' . $regexp[GESHI_MODIFIERS],
  3200.                             $regexp[GESHI_BEFORE] . '<|!REG3XP'$key .'!>' . $regexp[GESHI_REPLACE] . '|>' . $regexp[GESHI_AFTER],
  3201.                             $stuff_to_parse);
  3202.                     }
  3203.                 } else {
  3204.                     if ($this->line_numbers != <a href="../geshi/core/_geshi.php.html#defineGESHI_NO_LINE_NUMBERS">GESHI_NO_LINE_NUMBERS</a>) {
  3205.                         // produce valid HTML when we match multiple lines
  3206.                         $this->_hmr_key = $key;
  3207.                         $stuff_to_parse = preg_replace_callback( "/(" . $regexp . ")/",
  3208.                                               array($this, 'handle_multiline_regexps'), $stuff_to_parse);
  3209.                         $this->_hmr_key = '';
  3210.                     } else {
  3211.                         $stuff_to_parse = preg_replace( "/(" . $regexp . ")/", "<|!REG3XP$key!>\\1|>", $stuff_to_parse);
  3212.                     }
  3213.                 }
  3214.             }
  3215.         }
  3216.  
  3217.         // Highlight numbers. As of 1.0.8 we support diffent types of numbers
  3218.         $numbers_found = false;
  3219.         if ($this->lexic_permissions['NUMBERS'] && preg_match('#\d#', $stuff_to_parse )) {
  3220.             $numbers_found = true;
  3221.  
  3222.             //For each of the formats ...
  3223.             foreach($this->language_data['NUMBERS_RXCACHE'] as $id => $regexp) {
  3224.                 //Check if it should be highlighted ...
  3225.                 $stuff_to_parse = preg_replace($regexp, "<|/NUM!$id/>\\1|>", $stuff_to_parse);
  3226.             }
  3227.         }
  3228.  
  3229.         // Highlight keywords
  3230.         $disallowed_before = "(?<![a-zA-Z0-9\$_\|\#;>|^&";
  3231.         $disallowed_after = "(?![a-zA-Z0-9_\|%\\-&;";
  3232.         if ($this->lexic_permissions['STRINGS']) {
  3233.             $quotemarks = preg_quote(implode($this->language_data['QUOTEMARKS']), '/');
  3234.             $disallowed_before .= $quotemarks;
  3235.             $disallowed_after .= $quotemarks;
  3236.         }
  3237.         $disallowed_before .= "])";
  3238.         $disallowed_after .= "])";
  3239.  
  3240.         $parser_control_pergroup = false;
  3241.         if (isset($this->language_data['PARSER_CONTROL'])) {
  3242.             if (isset($this->language_data['PARSER_CONTROL']['KEYWORDS'])) {
  3243.                 $x = 0; // check wether per-keyword-group parser_control is enabled
  3244.                 if (isset($this->language_data['PARSER_CONTROL']['KEYWORDS']['DISALLOWED_BEFORE'])) {
  3245.                     $disallowed_before = $this->language_data['PARSER_CONTROL']['KEYWORDS']['DISALLOWED_BEFORE'];
  3246.                     ++$x;
  3247.                 }
  3248.                 if (isset($this->language_data['PARSER_CONTROL']['KEYWORDS']['DISALLOWED_AFTER'])) {
  3249.                     $disallowed_after = $this->language_data['PARSER_CONTROL']['KEYWORDS']['DISALLOWED_AFTER'];
  3250.                     ++$x;
  3251.                 }
  3252.                 $parser_control_pergroup = (count($this->language_data['PARSER_CONTROL']['KEYWORDS']) - $x) > 0;
  3253.             }
  3254.         }
  3255.  
  3256.         // if this is changed, don't forget to change it below
  3257. //        if (!empty($disallowed_before)) {
  3258. //            $disallowed_before = "(?<![$disallowed_before])";
  3259. //        }
  3260. //        if (!empty($disallowed_after)) {
  3261. //            $disallowed_after = "(?![$disallowed_after])";
  3262. //        }
  3263.         foreach (array_keys($this->language_data['KEYWORDS']) as $k) {
  3264.             if (!isset($this->lexic_permissions['KEYWORDS'][$k]) ||
  3265.                 $this->lexic_permissions['KEYWORDS'][$k]) {
  3266.  
  3267.                 $case_sensitive = $this->language_data['CASE_SENSITIVE'][$k];
  3268.                 $modifiers = $case_sensitive ? '' : 'i';
  3269.  
  3270.                 // NEW in 1.0.8 - per-keyword-group parser control
  3271.                 $disallowed_before_local = $disallowed_before;
  3272.                 $disallowed_after_local = $disallowed_after;
  3273.                 if ($parser_control_pergroup && isset($this->language_data['PARSER_CONTROL']['KEYWORDS'][$k])) {
  3274.                     if (isset($this->language_data['PARSER_CONTROL']['KEYWORDS'][$k]['DISALLOWED_BEFORE'])) {
  3275.                         $disallowed_before_local =
  3276.                             $this->language_data['PARSER_CONTROL']['KEYWORDS'][$k]['DISALLOWED_BEFORE'];
  3277.                     }
  3278.  
  3279.                     if (isset($this->language_data['PARSER_CONTROL']['KEYWORDS'][$k]['DISALLOWED_AFTER'])) {
  3280.                         $disallowed_after_local =
  3281.                             $this->language_data['PARSER_CONTROL']['KEYWORDS'][$k]['DISALLOWED_AFTER'];
  3282.                     }
  3283.                 }
  3284.  
  3285.                 $this->_kw_replace_group = $k;
  3286.  
  3287.                 //NEW in 1.0.8, the cached regexp list
  3288.                 // since we don't want PHP / PCRE to crash due to too large patterns we split them into smaller chunks
  3289.                 for ($set = 0, $set_length = count($this->language_data['CACHED_KEYWORD_LISTS'][$k]); $set <  $set_length; ++$set) {
  3290.                     $keywordset =& $this->language_data['CACHED_KEYWORD_LISTS'][$k][$set];
  3291.                     // Might make a more unique string for putting the number in soon
  3292.                     // Basically, we don't put the styles in yet because then the styles themselves will
  3293.                     // get highlighted if the language has a CSS keyword in it (like CSS, for example ;))
  3294.                     $stuff_to_parse = preg_replace_callback(
  3295.                         "/$disallowed_before_local({$keywordset})(?!\<DOT\>(?:htm|php))$disallowed_after_local/$modifiers",
  3296.                         array($this, 'handle_keyword_replace'),
  3297.                         $stuff_to_parse
  3298.                         );
  3299.                 }
  3300.             }
  3301.         }
  3302.  
  3303.         //
  3304.         // Now that's all done, replace /[number]/ with the correct styles
  3305.         //
  3306.         foreach (array_keys($this->language_data['KEYWORDS']) as $k) {
  3307.             if (!$this->use_classes) {
  3308.                 $attributes = ' style="' .
  3309.                     (isset($this->language_data['STYLES']['KEYWORDS'][$k]) ?
  3310.                     $this->language_data['STYLES']['KEYWORDS'][$k] : "") . '"';
  3311.             } else {
  3312.                 $attributes = ' class="kw' . $k . '"';
  3313.             }
  3314.             $stuff_to_parse = str_replace("<|/$k/>", "<|$attributes>", $stuff_to_parse);
  3315.         }
  3316.  
  3317.         if ($numbers_found) {
  3318.             // Put number styles in
  3319.             foreach($this->language_data['NUMBERS_RXCACHE'] as $id => $regexp) {
  3320. //Commented out for now, as this needs some review ...
  3321. //                if ($numbers_permissions & $id) {
  3322.                     //Get the appropriate style ...
  3323.                         //Checking for unset styles is done by the style cache builder ...
  3324.                     if (!$this->use_classes) {
  3325.                         $attributes = ' style="' . $this->language_data['STYLES']['NUMBERS'][$id] . '"';
  3326.                     } else {
  3327.                         $attributes = ' class="nu'.$id.'"';
  3328.                     }
  3329.  
  3330.                     //Set in the correct styles ...
  3331.                     $stuff_to_parse = str_replace("/NUM!$id/", $attributes, $stuff_to_parse);
  3332. //                }
  3333.             }
  3334.         }
  3335.  
  3336.         // Highlight methods and fields in objects
  3337.         if ($this->lexic_permissions['METHODS'] && $this->language_data['OOLANG']) {
  3338.             $oolang_spaces = "[\s]*";
  3339.             $oolang_before = "";
  3340.             $oolang_after = "[a-zA-Z][a-zA-Z0-9_]*";
  3341.             if (isset($this->language_data['PARSER_CONTROL'])) {
  3342.                 if (isset($this->language_data['PARSER_CONTROL']['OOLANG'])) {
  3343.                     if (isset($this->language_data['PARSER_CONTROL']['OOLANG']['MATCH_BEFORE'])) {
  3344.                         $oolang_before = $this->language_data['PARSER_CONTROL']['OOLANG']['MATCH_BEFORE'];
  3345.                     }
  3346.                     if (isset($this->language_data['PARSER_CONTROL']['OOLANG']['MATCH_AFTER'])) {
  3347.                         $oolang_after = $this->language_data['PARSER_CONTROL']['OOLANG']['MATCH_AFTER'];
  3348.                     }
  3349.                     if (isset($this->language_data['PARSER_CONTROL']['OOLANG']['MATCH_SPACES'])) {
  3350.                         $oolang_spaces = $this->language_data['PARSER_CONTROL']['OOLANG']['MATCH_SPACES'];
  3351.                     }
  3352.                 }
  3353.             }
  3354.  
  3355.             foreach ($this->language_data['OBJECT_SPLITTERS'] as $key => $splitter) {
  3356.                 if (false !== strpos($stuff_to_parse, $splitter)) {
  3357.                     if (!$this->use_classes) {
  3358.                         $attributes = ' style="' . $this->language_data['STYLES']['METHODS'][$key] . '"';
  3359.                     } else {
  3360.                         $attributes = ' class="me' . $key . '"';
  3361.                     }
  3362.                     $stuff_to_parse = preg_replace("/($oolang_before)(preg_quote($this->language_data['OBJECT_SPLITTERS'][$key], '/') . ")($oolang_spaces)($oolang_after)/", "\\1\\2\\3<|$attributes>\\4|>", $stuff_to_parse);
  3363.                 }
  3364.             }
  3365.         }
  3366.  
  3367.         //
  3368.         // Highlight brackets. Yes, I've tried adding a semi-colon to this list.
  3369.         // You try it, and see what happens ;)
  3370.         // TODO: Fix lexic permissions not converting entities if shouldn't
  3371.         // be highlighting regardless
  3372.         //
  3373.         if ($this->lexic_permissions['BRACKETS']) {
  3374.             $stuff_to_parse = str_replace( $this->language_data['CACHE_BRACKET_MATCH'],
  3375.                               $this->language_data['CACHE_BRACKET_REPLACE'], $stuff_to_parse );
  3376.         }
  3377.  
  3378.  
  3379.         //FIX for symbol highlighting ...
  3380.         if ($this->lexic_permissions['SYMBOLS'] && !empty($this->language_data['SYMBOLS'])) {
  3381.             //Get all matches and throw away those witin a block that is already highlighted... (i.e. matched by a regexp)
  3382.             $n_symbols = preg_match_all("/<\|(?:<DOT>|[^>])+>(?:(?!\|>).*?)\|>|<\/a>|(?:" . $this->language_data['SYMBOL_SEARCH'] . ")+/", $stuff_to_parse, $pot_symbols, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  3383.             $global_offset = 0;
  3384.             for ($s_id = 0; $s_id < $n_symbols; ++$s_id) {
  3385.                 $symbol_match = $pot_symbols[$s_id][0][0];
  3386.                 if (strpos($symbol_match, '<') !== false || strpos($symbol_match, '>') !== false) {
  3387.                     // already highlighted blocks _must_ include either < or >
  3388.                     // so if this conditional applies, we have to skip this match
  3389.                     // BenBE: UNLESS the block contains <SEMI> or <PIPE>
  3390.                     if(strpos($symbol_match, '<SEMI>') === false &&
  3391.                         strpos($symbol_match, '<PIPE>') === false) {
  3392.                         continue;
  3393.                     }
  3394.                 }
  3395.  
  3396.                 // if we reach this point, we have a valid match which needs to be highlighted
  3397.                 $symbol_length = strlen($symbol_match);
  3398.                 $symbol_offset = $pot_symbols[$s_id][0][1];
  3399.                 unset($pot_symbols[$s_id]);
  3400.                 $symbol_end = $symbol_length + $symbol_offset;
  3401.                 $symbol_hl = "";
  3402.  
  3403.                 // if we have multiple styles, we have to handle them properly
  3404.                 if ($this->language_data['MULTIPLE_SYMBOL_GROUPS']) {
  3405.                     $old_sym = -1;
  3406.                     // Split the current stuff to replace into its atomic symbols ...
  3407.                     preg_match_all("/" . $this->language_data['SYMBOL_SEARCH'] . "/", $symbol_match, $sym_match_syms, PREG_PATTERN_ORDER);
  3408.                     foreach ($sym_match_syms[0] as $sym_ms) {
  3409.                         //Check if consequtive symbols belong to the same group to save output ...
  3410.                         if (isset($this->language_data['SYMBOL_DATA'][$sym_ms])
  3411.                             && ($this->language_data['SYMBOL_DATA'][$sym_ms] != $old_sym)) {
  3412.                             if (-1 != $old_sym) {
  3413.                                 $symbol_hl .= "|>";
  3414.                             }
  3415.                             $old_sym = $this->language_data['SYMBOL_DATA'][$sym_ms];
  3416.                             if (!$this->use_classes) {
  3417.                                 $symbol_hl .= '<| style="' . $this->language_data['STYLES']['SYMBOLS'][$old_sym] . '">';
  3418.                             } else {
  3419.                                 $symbol_hl .= '<| class="sy' . $old_sym . '">';
  3420.                             }
  3421.                         }
  3422.                         $symbol_hl .= $sym_ms;
  3423.                     }
  3424.                     unset($sym_match_syms);
  3425.  
  3426.                     //Close remaining tags and insert the replacement at the right position ...
  3427.                     //Take caution if symbol_hl is empty to avoid doubled closing spans.
  3428.                     if (-1 != $old_sym) {
  3429.                         $symbol_hl .= "|>";
  3430.                     }
  3431.                 } else {
  3432.                     if (!$this->use_classes) {
  3433.                         $symbol_hl = '<| style="' . $this->language_data['STYLES']['SYMBOLS'][0] . '">';
  3434.                     } else {
  3435.                         $symbol_hl = '<| class="sy0">';
  3436.                     }
  3437.                     $symbol_hl .= $symbol_match . '|>';
  3438.                 }
  3439.  
  3440.                 $stuff_to_parse = substr_replace($stuff_to_parse, $symbol_hl, $symbol_offset + $global_offset, $symbol_length);
  3441.  
  3442.                 // since we replace old text with something of different size,
  3443.                 // we'll have to keep track of the differences
  3444.                 $global_offset += strlen($symbol_hl) - $symbol_length;
  3445.             }
  3446.         }
  3447.         //FIX for symbol highlighting ...
  3448.         // Add class/style for regexps
  3449.         foreach (array_keys($this->language_data['REGEXPS']) as $key) {
  3450.             if ($this->lexic_permissions['REGEXPS'][$key]) {
  3451.                 if (is_callable($this->language_data['STYLES']['REGEXPS'][$key])) {
  3452.                     $this->_rx_key = $key;
  3453.                     $stuff_to_parse = preg_replace_callback("/!REG3XP$key!(.*)\|>/U",
  3454.                         array($this, 'handle_regexps_callback'),
  3455.                         $stuff_to_parse);
  3456.                 } else {
  3457.                     if (!$this->use_classes) {
  3458.                         $attributes = ' style="' . $this->language_data['STYLES']['REGEXPS'][$key] . '"';
  3459.                     } else {
  3460.                         if (is_array($this->language_data['REGEXPS'][$key]) &&
  3461.                             array_key_exists(GESHI_CLASS, $this->language_data['REGEXPS'][$key])) {
  3462.                             $attributes = ' class="' .
  3463.                                 $this->language_data['REGEXPS'][$key][GESHI_CLASS] . '"';
  3464.                         } else {
  3465.                            $attributes = ' class="re' . $key . '"';
  3466.                         }
  3467.                     }
  3468.                     $stuff_to_parse = str_replace("!REG3XP$key!", "$attributes", $stuff_to_parse);
  3469.                 }
  3470.             }
  3471.         }
  3472.  
  3473.         // Replace <DOT> with . for urls
  3474.         $stuff_to_parse = str_replace('<DOT>', '.', $stuff_to_parse);
  3475.         // Replace <|UR1| with <a href= for urls also
  3476.         if (isset($this->link_styles[<a href="../geshi/core/_geshi.php.html#defineGESHI_LINK">GESHI_LINK</a>])) {
  3477.             if ($this->use_classes) {
  3478.                 $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target . ' href=', $stuff_to_parse);
  3479.             } else {
  3480.                 $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target . ' style="' . $this->link_styles[<a href="../geshi/core/_geshi.php.html#defineGESHI_LINK">GESHI_LINK</a>] . '" href=', $stuff_to_parse);
  3481.             }
  3482.         } else {
  3483.             $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target . ' href=', $stuff_to_parse);
  3484.         }
  3485.  
  3486.         //
  3487.         // NOW we add the span thingy ;)
  3488.         //
  3489.         $stuff_to_parse = str_replace('<|', '<span', $stuff_to_parse);
  3490.         $stuff_to_parse = str_replace ( '|>', '</span>', $stuff_to_parse );
  3491.         return substr($stuff_to_parse, 1);
  3492.     }
  3493.  
  3494.     /**
  3495.      * Sets the time taken to parse the code
  3496.      *
  3497.      * @param microtime The time when parsing started
  3498.      * @param microtime The time when parsing ended
  3499.      * @since 1.0.2
  3500.      * @access private
  3501.      */
  3502.     function set_time($start_time, $end_time) {
  3503.         $start = explode(' ', $start_time);
  3504.         $end = explode(' ', $end_time);
  3505.         $this->time = $end[0] + $end[1] - $start[0] - $start[1];
  3506.     }
  3507.  
  3508.     /**
  3509.      * Gets the time taken to parse the code
  3510.      *
  3511.      * @return double The time taken to parse the code
  3512.      * @since  1.0.2
  3513.      */
  3514.     function get_time() {
  3515.         return $this->time;
  3516.     }
  3517.  
  3518.     /**
  3519.      * Merges arrays recursively, overwriting values of the first array with values of later arrays
  3520.      *
  3521.      * @since 1.0.8
  3522.      * @access private
  3523.      */
  3524.     function merge_arrays() {
  3525.         $arrays = func_get_args();
  3526.         $narrays = count($arrays);
  3527.  
  3528.         // check arguments
  3529.         // comment out if more performance is necessary (in this case the foreach loop will trigger a warning if the argument is not an array)
  3530.         for ($i = 0; $i < $narrays; $i ++) {
  3531.             if (!is_array($arrays[$i])) {
  3532.                 // also array_merge_recursive returns nothing in this case
  3533.                 trigger_error('Argument #' . ($i+1) . ' is not an array - trying to merge array with scalar! Returning false!', E_USER_WARNING);
  3534.                 return false;
  3535.             }
  3536.         }
  3537.  
  3538.         // the first array is in the output set in every case
  3539.         $ret = $arrays[0];
  3540.  
  3541.         // merege $ret with the remaining arrays
  3542.         for ($i = 1; $i < $narrays; $i ++) {
  3543.             foreach ($arrays[$i] as $key => $value) {
  3544.                 if (is_array($value) && isset($ret[$key])) {
  3545.                     // if $ret[$key] is not an array you try to merge an scalar value with an array - the result is not defined (incompatible arrays)
  3546.                     // in this case the call will trigger an E_USER_WARNING and the $ret[$key] will be false.
  3547.                     $ret[$key] = $this->merge_arrays($ret[$key], $value);
  3548.                 } else {
  3549.                     $ret[$key] = $value;
  3550.                 }
  3551.             }
  3552.         }
  3553.  
  3554.         return $ret;
  3555.     }
  3556.  
  3557.     /**
  3558.      * Gets language information and stores it for later use
  3559.      *
  3560.      * @param string The filename of the language file you want to load
  3561.      * @since 1.0.0
  3562.      * @access private
  3563.      * @todo Needs to load keys for lexic permissions for keywords, regexps etc
  3564.      */
  3565.     function load_language($file_name) {
  3566.         if ($file_name == $this->loaded_language) {
  3567.             // this file is already loaded!
  3568.             return;
  3569.         }
  3570.  
  3571.         //Prepare some stuff before actually loading the language file
  3572.         $this->loaded_language = $file_name;
  3573.         $this->parse_cache_built = false;
  3574.         $this->enable_highlighting();
  3575.         $language_data = array();
  3576.  
  3577.         //Load the language file
  3578.         require $file_name;
  3579.  
  3580.         // Perhaps some checking might be added here later to check that
  3581.         // $language data is a valid thing but maybe not
  3582.         $this->language_data = $language_data;
  3583.  
  3584.         // Set strict mode if should be set
  3585.         $this->strict_mode = $this->language_data['STRICT_MODE_APPLIES'];
  3586.  
  3587.         // Set permissions for all lexics to true
  3588.         // so they'll be highlighted by default
  3589.         foreach (array_keys($this->language_data['KEYWORDS']) as $key) {
  3590.             if (!empty($this->language_data['KEYWORDS'][$key])) {
  3591.                 $this->lexic_permissions['KEYWORDS'][$key] = true;
  3592.             } else {
  3593.                 $this->lexic_permissions['KEYWORDS'][$key] = false;
  3594.             }
  3595.         }
  3596.  
  3597.         foreach (array_keys($this->language_data['COMMENT_SINGLE']) as $key) {
  3598.             $this->lexic_permissions['COMMENTS'][$key] = true;
  3599.         }
  3600.         foreach (array_keys($this->language_data['REGEXPS']) as $key) {
  3601.             $this->lexic_permissions['REGEXPS'][$key] = true;
  3602.         }
  3603.  
  3604.         // for BenBE and future code reviews:
  3605.         // we can use empty here since we only check for existance and emptiness of an array
  3606.         // if it is not an array at all but rather false or null this will work as intended as well
  3607.         // even if $this->language_data['PARSER_CONTROL'] is undefined this won't trigger a notice
  3608.         if (!empty($this->language_data['PARSER_CONTROL']['ENABLE_FLAGS'])) {
  3609.             foreach ($this->language_data['PARSER_CONTROL']['ENABLE_FLAGS'] as $flag => $value) {
  3610.                 // it's either true or false and maybe is true as well
  3611.                 $perm = $value !== GESHI_NEVER;
  3612.                 if ($flag == 'ALL') {
  3613.                     $this->enable_highlighting($perm);
  3614.                     continue;
  3615.                 }
  3616.                 if (!isset($this->lexic_permissions[$flag])) {
  3617.                     // unknown lexic permission
  3618.                     continue;
  3619.                 }
  3620.                 if (is_array($this->lexic_permissions[$flag])) {
  3621.                     foreach ($this->lexic_permissions[$flag] as $key => $val) {
  3622.                         $this->lexic_permissions[$flag][$key] = $perm;
  3623.                     }
  3624.                 } else {
  3625.                     $this->lexic_permissions[$flag] = $perm;
  3626.                 }
  3627.             }
  3628.             unset($this->language_data['PARSER_CONTROL']['ENABLE_FLAGS']);
  3629.         }
  3630.  
  3631.         //NEW in 1.0.8: Allow styles to be loaded from a separate file to override defaults
  3632.         $style_filename = substr($file_name, 0, -4) . '.style.php';
  3633.         if (is_readable($style_filename)) {
  3634.             //Clear any style_data that could have been set before ...
  3635.             if (isset($style_data)) {
  3636.                 unset($style_data);
  3637.             }
  3638.  
  3639.             //Read the Style Information from the style file
  3640.             include $style_filename;
  3641.  
  3642.             //Apply the new styles to our current language styles
  3643.             if (isset($style_data) && is_array($style_data)) {
  3644.                 $this->language_data['STYLES'] =
  3645.                     $this->merge_arrays($this->language_data['STYLES'], $style_data);
  3646.             }
  3647.         }
  3648.     }
  3649.  
  3650.     /**
  3651.      * Takes the parsed code and various options, and creates the HTML
  3652.      * surrounding it to make it look nice.
  3653.      *
  3654.      * @param  string The code already parsed (reference!)
  3655.      * @since  1.0.0
  3656.      * @access private
  3657.      */
  3658.     function finalise(&$parsed_code) {
  3659.         // Remove end parts of important declarations
  3660.         // This is BUGGY!! My fault for bad code: fix coming in 1.2
  3661.         // @todo Remove this crap
  3662.         if ($this->enable_important_blocks &&
  3663.             (strpos($parsed_code, $this->hsc(<a href="../geshi/core/_geshi.php.html#defineGESHI_START_IMPORTANT">GESHI_START_IMPORTANT</a>)) === false)) {
  3664.             $parsed_code = str_replace($this->hsc(<a href="../geshi/core/_geshi.php.html#defineGESHI_END_IMPORTANT">GESHI_END_IMPORTANT</a>), '', $parsed_code);
  3665.         }
  3666.  
  3667.         // Add HTML whitespace stuff if we're using the <div> header
  3668.         if ($this->header_type != <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE">GESHI_HEADER_PRE</a> && $this->header_type != <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_VALID">GESHI_HEADER_PRE_VALID</a>) {
  3669.             $this->indent($parsed_code);
  3670.         }
  3671.  
  3672.         // purge some unnecessary stuff
  3673.         /** NOTE: memorypeak #1 */
  3674.         $parsed_code = preg_replace('#<span[^>]+>(\s*)</span>#', '\\1', $parsed_code);
  3675.  
  3676.         // If we are using IDs for line numbers, there needs to be an overall
  3677.         // ID set to prevent collisions.
  3678.         if ($this->add_ids && !$this->overall_id) {
  3679.             $this->overall_id = 'geshi-' . substr(md5(microtime()), 0, 4);
  3680.         }
  3681.  
  3682.         // Get code into lines
  3683.         /** NOTE: memorypeak #2 */
  3684.         $code = explode("\n", $parsed_code);
  3685.         $parsed_code = $this->header();
  3686.  
  3687.         // If we're using line numbers, we insert <li>s and appropriate
  3688.         // markup to style them (otherwise we don't need to do anything)
  3689.         if ($this->line_numbers != <a href="../geshi/core/_geshi.php.html#defineGESHI_NO_LINE_NUMBERS">GESHI_NO_LINE_NUMBERS</a> && $this->header_type != <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_TABLE">GESHI_HEADER_PRE_TABLE</a>) {
  3690.             // If we're using the <pre> header, we shouldn't add newlines because
  3691.             // the <pre> will line-break them (and the <li>s already do this for us)
  3692.             $ls = ($this->header_type != <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE">GESHI_HEADER_PRE</a> && $this->header_type != <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_VALID">GESHI_HEADER_PRE_VALID</a>) ? "\n" : '';
  3693.  
  3694.             // Set vars to defaults for following loop
  3695.             $i = 0;
  3696.  
  3697.             // Foreach line...
  3698.             for ($i = 0, $n = count($code); $i < $n;) {
  3699.                 //Reset the attributes for a new line ...
  3700.                 $attrs = array();
  3701.  
  3702.                 // Make lines have at least one space in them if they're empty
  3703.                 // BenBE: Checking emptiness using trim instead of relying on blanks
  3704.                 if ('' == trim($code[$i])) {
  3705.                     $code[$i] = '&nbsp;';
  3706.                 }
  3707.  
  3708.                 // If this is a "special line"...
  3709.                 if ($this->line_numbers == <a href="../geshi/core/_geshi.php.html#defineGESHI_FANCY_LINE_NUMBERS">GESHI_FANCY_LINE_NUMBERS</a> &&
  3710.                     $i % $this->line_nth_row == ($this->line_nth_row - 1)) {
  3711.                     // Set the attributes to style the line
  3712.                     if ($this->use_classes) {
  3713.                         //$attr = ' class="li2"';
  3714.                         $attrs['class'][] = 'li2';
  3715.                         $def_attr = ' class="de2"';
  3716.                     } else {
  3717.                         //$attr = ' style="' . $this->line_style2 . '"';
  3718.                         $attrs['style'][] = $this->line_style2;
  3719.                         // This style "covers up" the special styles set for special lines
  3720.                         // so that styles applied to special lines don't apply to the actual
  3721.                         // code on that line
  3722.                         $def_attr = ' style="' . $this->code_style . '"';
  3723.                     }
  3724.                 } else {
  3725.                     if ($this->use_classes) {
  3726.                         //$attr = ' class="li1"';
  3727.                         $attrs['class'][] = 'li1';
  3728.                         $def_attr = ' class="de1"';
  3729.                     } else {
  3730.                         //$attr = ' style="' . $this->line_style1 . '"';
  3731.                         $attrs['style'][] = $this->line_style1;
  3732.                         $def_attr = ' style="' . $this->code_style . '"';
  3733.                     }
  3734.                 }
  3735.  
  3736.                 //Check which type of tag to insert for this line
  3737.                 if ($this->header_type == <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_VALID">GESHI_HEADER_PRE_VALID</a>) {
  3738.                     $start = "<pre$def_attr>";
  3739.                     $end = '</pre>';
  3740.                 } else {
  3741.                     // Span or div?
  3742.                     $start = "<div$def_attr>";
  3743.                     $end = '</div>';
  3744.                 }
  3745.  
  3746.                 ++$i;
  3747.  
  3748.                 // Are we supposed to use ids? If so, add them
  3749.                 if ($this->add_ids) {
  3750.                     $attrs['id'][] = "$this->overall_id-$i";
  3751.                 }
  3752.  
  3753.                 //Is this some line with extra styles???
  3754.                 if (in_array($i, $this->highlight_extra_lines)) {
  3755.                     if ($this->use_classes) {
  3756.                         if (isset($this->highlight_extra_lines_styles[$i])) {
  3757.                             $attrs['class'][] = "lx$i";
  3758.                         } else {
  3759.                             $attrs['class'][] = "ln-xtra";
  3760.                         }
  3761.                     } else {
  3762.                         array_push($attrs['style'], $this->get_line_style($i));
  3763.                     }
  3764.                 }
  3765.  
  3766.                 // Add in the line surrounded by appropriate list HTML
  3767.                 $attr_string = '';
  3768.                 foreach ($attrs as $key => $attr) {
  3769.                     $attr_string .= ' ' . $key . '="' . implode(' ', $attr) . '"';
  3770.                 }
  3771.  
  3772.                 $parsed_code .= "<li$attr_string>$start{$code[$i-1]}$end</li>$ls";
  3773.                 unset($code[$i - 1]);
  3774.             }
  3775.         } else {
  3776.             $n = count($code);
  3777.             if ($this->use_classes) {
  3778.                 $attributes = ' class="de1"';
  3779.             } else {
  3780.                 $attributes = ' style="'$this->code_style .'"';
  3781.             }
  3782.             if ($this->header_type == <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_VALID">GESHI_HEADER_PRE_VALID</a>) {
  3783.                 $parsed_code .= '<pre'$attributes .'>';
  3784.             } elseif ($this->header_type == <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_TABLE">GESHI_HEADER_PRE_TABLE</a>) {
  3785.                 if ($this->line_numbers != <a href="../geshi/core/_geshi.php.html#defineGESHI_NO_LINE_NUMBERS">GESHI_NO_LINE_NUMBERS</a>) {
  3786.                     if ($this->use_classes) {
  3787.                         $attrs = ' class="ln"';
  3788.                     } else {
  3789.                         $attrs = ' style="'$this->table_linenumber_style .'"';
  3790.                     }
  3791.                     $parsed_code .= '<td'.$attrs.'><pre'.$attributes.'>';
  3792.                     // get linenumbers
  3793.                     // we don't merge it with the for below, since it should be better for
  3794.                     // memory consumption this way
  3795.                     // @todo: but... actually it would still be somewhat nice to merge the two loops
  3796.                     //        the mem peaks are at different positions
  3797.                     for ($i = 0; $i < $n; ++$i) {
  3798.                         $close = 0;
  3799.                         // fancy lines
  3800.                         if ($this->line_numbers == <a href="../geshi/core/_geshi.php.html#defineGESHI_FANCY_LINE_NUMBERS">GESHI_FANCY_LINE_NUMBERS</a> &&
  3801.                             $i % $this->line_nth_row == ($this->line_nth_row - 1)) {
  3802.                             // Set the attributes to style the line
  3803.                             if ($this->use_classes) {
  3804.                                 $parsed_code .= '<span class="xtra li2"><span class="de2">';
  3805.                             } else {
  3806.                                 // This style "covers up" the special styles set for special lines
  3807.                                 // so that styles applied to special lines don't apply to the actual
  3808.                                 // code on that line
  3809.                                 $parsed_code .= '<span style="display:block;' . $this->line_style2 . '">'
  3810.                                                   .'<span style="' . $this->code_style .'">';
  3811.                             }
  3812.                             $close += 2;
  3813.                         }
  3814.                         //Is this some line with extra styles???
  3815.                         if (in_array($i + 1, $this->highlight_extra_lines)) {
  3816.                             if ($this->use_classes) {
  3817.                                 if (isset($this->highlight_extra_lines_styles[$i])) {
  3818.                                     $parsed_code .= "<span class=\"xtra lx$i\">";
  3819.                                 } else {
  3820.                                     $parsed_code .= "<span class=\"xtra ln-xtra\">";
  3821.                                 }
  3822.                             } else {
  3823.                                 $parsed_code .= "<span style=\"display:block;" . $this->get_line_style($i) . "\">";
  3824.                             }
  3825.                             ++$close;
  3826.                         }
  3827.                         $parsed_code .= $this->line_numbers_start + $i;
  3828.                         if ($close) {
  3829.                             $parsed_code .= str_repeat('</span>', $close);
  3830.                         } else if ($i != $n) {
  3831.                             $parsed_code .= "\n";
  3832.                         }
  3833.                     }
  3834.                     $parsed_code .= '</pre></td><td'.$attributes.'>';
  3835.                 }
  3836.                 $parsed_code .= '<pre'$attributes .'>';
  3837.             }
  3838.             // No line numbers, but still need to handle highlighting lines extra.
  3839.             // Have to use divs so the full width of the code is highlighted
  3840.             $close = 0;
  3841.             for ($i = 0; $i < $n; ++$i) {
  3842.                 // Make lines have at least one space in them if they're empty
  3843.                 // BenBE: Checking emptiness using trim instead of relying on blanks
  3844.                 if ('' == trim($code[$i])) {
  3845.                     $code[$i] = '&nbsp;';
  3846.                 }
  3847.                 // fancy lines
  3848.                 if ($this->line_numbers == <a href="../geshi/core/_geshi.php.html#defineGESHI_FANCY_LINE_NUMBERS">GESHI_FANCY_LINE_NUMBERS</a> &&
  3849.                     $i % $this->line_nth_row == ($this->line_nth_row - 1)) {
  3850.                     // Set the attributes to style the line
  3851.                     if ($this->use_classes) {
  3852.                         $parsed_code .= '<span class="xtra li2"><span class="de2">';
  3853.                     } else {
  3854.                         // This style "covers up" the special styles set for special lines
  3855.                         // so that styles applied to special lines don't apply to the actual
  3856.                         // code on that line
  3857.                         $parsed_code .= '<span style="display:block;' . $this->line_style2 . '">'
  3858.                                           .'<span style="' . $this->code_style .'">';
  3859.                     }
  3860.                     $close += 2;
  3861.                 }
  3862.                 //Is this some line with extra styles???
  3863.                 if (in_array($i + 1, $this->highlight_extra_lines)) {
  3864.                     if ($this->use_classes) {
  3865.                         if (isset($this->highlight_extra_lines_styles[$i])) {
  3866.                             $parsed_code .= "<span class=\"xtra lx$i\">";
  3867.                         } else {
  3868.                             $parsed_code .= "<span class=\"xtra ln-xtra\">";
  3869.                         }
  3870.                     } else {
  3871.                         $parsed_code .= "<span style=\"display:block;" . $this->get_line_style($i) . "\">";
  3872.                     }
  3873.                     ++$close;
  3874.                 }
  3875.  
  3876.                 $parsed_code .= $code[$i];
  3877.  
  3878.                 if ($close) {
  3879.                   $parsed_code .= str_repeat('</span>', $close);
  3880.                   $close = 0;
  3881.                 }
  3882.                 elseif ($i + 1 < $n) {
  3883.                     $parsed_code .= "\n";
  3884.                 }
  3885.                 unset($code[$i]);
  3886.             }
  3887.  
  3888.             if ($this->header_type == <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_VALID">GESHI_HEADER_PRE_VALID</a> || $this->header_type == <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_TABLE">GESHI_HEADER_PRE_TABLE</a>) {
  3889.                 $parsed_code .= '</pre>';
  3890.             }
  3891.             if ($this->header_type == <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_TABLE">GESHI_HEADER_PRE_TABLE</a> && $this->line_numbers != <a href="../geshi/core/_geshi.php.html#defineGESHI_NO_LINE_NUMBERS">GESHI_NO_LINE_NUMBERS</a>) {
  3892.                 $parsed_code .= '</td>';
  3893.             }
  3894.         }
  3895.  
  3896.         $parsed_code .= $this->footer();
  3897.     }
  3898.  
  3899.     /**
  3900.      * Creates the header for the code block (with correct attributes)
  3901.      *
  3902.      * @return string The header for the code block
  3903.      * @since  1.0.0
  3904.      * @access private
  3905.      */
  3906.     function header() {
  3907.         // Get attributes needed
  3908.         /**
  3909.          * @todo   Document behaviour change - class is outputted regardless of whether
  3910.          *         we're using classes or not. Same with style
  3911.          */
  3912.         $attributes = ' class="' . $this->language;
  3913.         if ($this->overall_class != '') {
  3914.             $attributes .= " ".$this->overall_class;
  3915.         }
  3916.         $attributes .= '"';
  3917.  
  3918.         if ($this->overall_id != '') {
  3919.             $attributes .= " id=\"{$this->overall_id}\"";
  3920.         }
  3921.         if ($this->overall_style != '') {
  3922.             $attributes .= ' style="' . $this->overall_style . '"';
  3923.         }
  3924.  
  3925.         $ol_attributes = '';
  3926.  
  3927.         if ($this->line_numbers_start != 1) {
  3928.             $ol_attributes .= ' start="' . $this->line_numbers_start . '"';
  3929.         }
  3930.  
  3931.         // Get the header HTML
  3932.         $header = $this->header_content;
  3933.         if ($header) {
  3934.             if ($this->header_type == <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE">GESHI_HEADER_PRE</a> || $this->header_type == <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_VALID">GESHI_HEADER_PRE_VALID</a>) {
  3935.                 $header = str_replace("\n", '', $header);
  3936.             }
  3937.             $header = $this->replace_keywords($header);
  3938.  
  3939.             if ($this->use_classes) {
  3940.                 $attr = ' class="head"';
  3941.             } else {
  3942.                 $attr = " style=\"{$this->header_content_style}\"";
  3943.             }
  3944.             if ($this->header_type == GESHI_HEADER_PRE_TABLE && $this->line_numbers != GESHI_NO_LINE_NUMBERS) {
  3945.                 $header = "<thead><tr><td colspan=\"2\" $attr>$header</td></tr></thead>";
  3946.             } else {
  3947.                 $header = "<div$attr>$header</div>";
  3948.             }
  3949.         }
  3950.  
  3951.         if (GESHI_HEADER_NONE == $this->header_type) {
  3952.             if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
  3953.                 return "$header<ol$attributes$ol_attributes>";
  3954.             }
  3955.             return $header . ($this->force_code_block ? '<div>' : '');
  3956.         }
  3957.  
  3958.         // Work out what to return and do it
  3959.         if ($this->line_numbers != <a href="../geshi/core/_geshi.php.html#defineGESHI_NO_LINE_NUMBERS">GESHI_NO_LINE_NUMBERS</a>) {
  3960.             if ($this->header_type == <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE">GESHI_HEADER_PRE</a>) {
  3961.                 return "<pre$attributes>$header<ol$ol_attributes>";
  3962.             } else if ($this->header_type == GESHI_HEADER_DIV ||
  3963.                 $this->header_type == GESHI_HEADER_PRE_VALID) {
  3964.                 return "<div$attributes>$header<ol$ol_attributes>";
  3965.             } else if ($this->header_type == GESHI_HEADER_PRE_TABLE) {
  3966.                 return "<table$attributes>$header<tbody><tr class=\"li1\">";
  3967.             }
  3968.         } else {
  3969.             if ($this->header_type == GESHI_HEADER_PRE) {
  3970.                 return "<pre$attributes>$header"  .
  3971.                     ($this->force_code_block ? '<div>' : '');
  3972.             } else {
  3973.                 return "<div$attributes>$header.
  3974.                     ($this->force_code_block ? '<div>' : '');
  3975.             }
  3976.         }
  3977.     }
  3978.  
  3979.     /**
  3980.      * Returns the footer for the code block.
  3981.      *
  3982.      * @return string The footer for the code block
  3983.      * @since  1.0.0
  3984.      * @access private
  3985.      */
  3986.     function footer() {
  3987.         $footer = $this->footer_content;
  3988.         if ($footer) {
  3989.             if ($this->header_type == <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE">GESHI_HEADER_PRE</a>) {
  3990.                 $footer = str_replace("\n", '', $footer);;
  3991.             }
  3992.             $footer = $this->replace_keywords($footer);
  3993.  
  3994.             if ($this->use_classes) {
  3995.                 $attr = ' class="foot"';
  3996.             } else {
  3997.                 $attr = " style=\"{$this->footer_content_style}\"";
  3998.             }
  3999.             if ($this->header_type == GESHI_HEADER_PRE_TABLE && $this->linenumbers != GESHI_NO_LINE_NUMBERS) {
  4000.                 $footer = "<tfoot><tr><td colspan=\"2\">$footer</td></tr></tfoot>";
  4001.             } else {
  4002.                 $footer = "<div$attr>$footer</div>";
  4003.             }
  4004.         }
  4005.  
  4006.         if (GESHI_HEADER_NONE == $this->header_type) {
  4007.             return ($this->line_numbers != GESHI_NO_LINE_NUMBERS) ? '</ol>' . $footer : $footer;
  4008.         }
  4009.  
  4010.         if ($this->header_type == <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_DIV">GESHI_HEADER_DIV</a> || $this->header_type == <a href="../geshi/core/_geshi.php.html#defineGESHI_HEADER_PRE_VALID">GESHI_HEADER_PRE_VALID</a>) {
  4011.             if ($this->line_numbers != <a href="../geshi/core/_geshi.php.html#defineGESHI_NO_LINE_NUMBERS">GESHI_NO_LINE_NUMBERS</a>) {
  4012.                 return "</ol>$footer</div>";
  4013.             }
  4014.             return ($this->force_code_block ? '</div>' : '') .
  4015.                 "$footer</div>";
  4016.         }
  4017.         elseif ($this->header_type == GESHI_HEADER_PRE_TABLE) {
  4018.             if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
  4019.                 return "</tr></tbody>$footer</table>";
  4020.             }
  4021.             return ($this->force_code_block ? '</div>' : '') .
  4022.                 "$footer</div>";
  4023.         }
  4024.         else {
  4025.             if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
  4026.                 return "</ol>$footer</pre>";
  4027.             }
  4028.             return ($this->force_code_block ? '</div>' : '') .
  4029.                 "$footer</pre>";
  4030.         }
  4031.     }
  4032.  
  4033.     /**
  4034.      * Replaces certain keywords in the header and footer with
  4035.      * certain configuration values
  4036.      *
  4037.      * @param  string The header or footer content to do replacement on
  4038.      * @return string The header or footer with replaced keywords
  4039.      * @since  1.0.2
  4040.      * @access private
  4041.      */
  4042.     function replace_keywords($instr) {
  4043.         $keywords = $replacements = array();
  4044.  
  4045.         $keywords[] = '<TIME>';
  4046.         $keywords[] = '{TIME}';
  4047.         $replacements[] = $replacements[] = number_format($time = $this->get_time(), 3);
  4048.  
  4049.         $keywords[] = '<LANGUAGE>';
  4050.         $keywords[] = '{LANGUAGE}';
  4051.         $replacements[] = $replacements[] = $this->language_data['LANG_NAME'];
  4052.  
  4053.         $keywords[] = '<VERSION>';
  4054.         $keywords[] = '{VERSION}';
  4055.         $replacements[] = $replacements[] = <a href="../geshi/core/_geshi.php.html#defineGESHI_VERSION">GESHI_VERSION</a>;
  4056.  
  4057.         $keywords[] = '<SPEED>';
  4058.         $keywords[] = '{SPEED}';
  4059.         if ($time <= 0) {
  4060.             $speed = 'N/A';
  4061.         } else {
  4062.             $speed = strlen($this->source) / $time;
  4063.             if ($speed >= 1024) {
  4064.                 $speed = sprintf("%.2f KB/s", $speed / 1024.0);
  4065.             } else {
  4066.                 $speed = sprintf("%.0f B/s", $speed);
  4067.             }
  4068.         }
  4069.         $replacements[] = $replacements[] = $speed;
  4070.  
  4071.         return str_replace($keywords, $replacements, $instr);
  4072.     }
  4073.  
  4074.     /**
  4075.      * Secure replacement for PHP built-in function htmlspecialchars().
  4076.      *
  4077.      * See ticket #427 (http://wush.net/trac/wikka/ticket/427) for the rationale
  4078.      * for this replacement function.
  4079.      *
  4080.      * The INTERFACE for this function is almost the same as that for
  4081.      * htmlspecialchars(), with the same default for quote style; however, there
  4082.      * is no 'charset' parameter. The reason for this is as follows:
  4083.      *
  4084.      * The PHP docs say:
  4085.      *      "The third argument charset defines character set used in conversion."
  4086.      *
  4087.      * I suspect PHP's htmlspecialchars() is working at the byte-value level and
  4088.      * thus _needs_ to know (or asssume) a character set because the special
  4089.      * characters to be replaced could exist at different code points in
  4090.      * different character sets. (If indeed htmlspecialchars() works at
  4091.      * byte-value level that goes some  way towards explaining why the
  4092.      * vulnerability would exist in this function, too, and not only in
  4093.      * htmlentities() which certainly is working at byte-value level.)
  4094.      *
  4095.      * This replacement function however works at character level and should
  4096.      * therefore be "immune" to character set differences - so no charset
  4097.      * parameter is needed or provided. If a third parameter is passed, it will
  4098.      * be silently ignored.
  4099.      *
  4100.      * In the OUTPUT there is a minor difference in that we use '&#39;' instead
  4101.      * of PHP's '&#039;' for a single quote: this provides compatibility with
  4102.      *      get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES)
  4103.      * (see comment by mikiwoz at yahoo dot co dot uk on
  4104.      * http://php.net/htmlspecialchars); it also matches the entity definition
  4105.      * for XML 1.0
  4106.      * (http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_Special_characters).
  4107.      * Like PHP we use a numeric character reference instead of '&apos;' for the
  4108.      * single quote. For the other special characters we use the named entity
  4109.      * references, as PHP is doing.
  4110.      *
  4111.      * @author      {@link http://wikkawiki.org/JavaWoman Marjolein Katsma}
  4112.      *
  4113.      * @license     http://www.gnu.org/copyleft/lgpl.html
  4114.      *              GNU Lesser General Public License
  4115.      * @copyright   Copyright 2007, {@link http://wikkawiki.org/CreditsPage
  4116.      *              Wikka Development Team}
  4117.      *
  4118.      * @access      private
  4119.      * @param       string  $string string to be converted
  4120.      * @param       integer $quote_style
  4121.      *                      - ENT_COMPAT:   escapes &, <, > and double quote (default)
  4122.      *                      - ENT_NOQUOTES: escapes only &, < and >
  4123.      *                      - ENT_QUOTES:   escapes &, <, >, double and single quotes
  4124.      * @return      string  converted string
  4125.      * @since       1.0.7.18
  4126.      */
  4127.     function hsc($string, $quote_style = ENT_COMPAT) {
  4128.         // init
  4129.         static $aTransSpecchar = array(
  4130.             '&' => '&amp;',
  4131.             '"' => '&quot;',
  4132.             '<' => '&lt;',
  4133.             '>' => '&gt;',
  4134.  
  4135.             //This fix is related to SF#1923020, but has to be applied
  4136.             //regardless of actually highlighting symbols.
  4137.             //Circumvent a bug with symbol highlighting
  4138.             //This is required as ; would produce undesirable side-effects if it
  4139.             //was not to be processed as an entity.
  4140.             ';' => '<SEMI>', // Force ; to be processed as entity
  4141.             '|' => '<PIPE>' // Force | to be processed as entity
  4142.             );                      // ENT_COMPAT set
  4143.         switch ($quote_style) {
  4144.             case ENT_NOQUOTES// don't convert double quotes
  4145.                 unset($aTransSpecchar['"']);
  4146.                 break;
  4147.             case ENT_QUOTES// convert single quotes as well
  4148.                 $aTransSpecchar["'"] = '&#39;'; // (apos) htmlspecialchars() uses '&#039;'
  4149.                 break;
  4150.         }
  4151.  
  4152.         // return translated string
  4153.         return strtr($string, $aTransSpecchar);
  4154.     }
  4155.  
  4156.     /**
  4157.      * Returns a stylesheet for the highlighted code. If $economy mode
  4158.      * is true, we only return the stylesheet declarations that matter for
  4159.      * this code block instead of the whole thing
  4160.      *
  4161.      * @param  boolean Whether to use economy mode or not
  4162.      * @return string A stylesheet built on the data for the current language
  4163.      * @since  1.0.0
  4164.      */
  4165.     function get_stylesheet($economy_mode = true) {
  4166.         // If there's an error, chances are that the language file
  4167.         // won't have populated the language data file, so we can't
  4168.         // risk getting a stylesheet...
  4169.         if ($this->error) {
  4170.             return '';
  4171.         }
  4172.  
  4173.         //Check if the style rearrangements have been processed ...
  4174.         //This also does some preprocessing to check which style groups are useable ...
  4175.         if(!isset($this->language_data['NUMBERS_CACHE'])) {
  4176.             $this->build_style_cache();
  4177.         }
  4178.  
  4179.         // First, work out what the selector should be. If there's an ID,
  4180.         // that should be used, the same for a class. Otherwise, a selector
  4181.         // of '' means that these styles will be applied anywhere
  4182.         if ($this->overall_id) {
  4183.             $selector = '#' . $this->overall_id;
  4184.         } else {
  4185.             $selector = '.' . $this->language;
  4186.             if ($this->overall_class) {
  4187.                 $selector .= '.' . $this->overall_class;
  4188.             }
  4189.         }
  4190.         $selector .= ' ';
  4191.  
  4192.         // Header of the stylesheet
  4193.         if (!$economy_mode) {
  4194.             $stylesheet = "/**\n".
  4195.                 " * GeSHi Dynamically Generated Stylesheet\n".
  4196.                 " * --------------------------------------\n".
  4197.                 " * Dynamically generated stylesheet for {$this->language}\n".
  4198.                 " * CSS class: {$this->overall_class}, CSS id: {$this->overall_id}\n".
  4199.                 " * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann\n" .
  4200.                 " * (http://qbnz.com/highlighter/ and http://geshi.org/)\n".
  4201.                 " * --------------------------------------\n".
  4202.                 " */\n";
  4203.         } else {
  4204.             $stylesheet = "/**\n".
  4205.                 " * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann\n" .
  4206.                 " * (http://qbnz.com/highlighter/ and http://geshi.org/)\n".
  4207.                 " */\n";
  4208.         }
  4209.  
  4210.         // Set the <ol> to have no effect at all if there are line numbers
  4211.         // (<ol>s have margins that should be destroyed so all layout is
  4212.         // controlled by the set_overall_style method, which works on the
  4213.         // <pre> or <div> container). Additionally, set default styles for lines
  4214.         if (!$economy_mode || $this->line_numbers != <a href="../geshi/core/_geshi.php.html#defineGESHI_NO_LINE_NUMBERS">GESHI_NO_LINE_NUMBERS</a>) {
  4215.             //$stylesheet .= "$selector, {$selector}ol, {$selector}ol li {margin: 0;}\n";
  4216.             $stylesheet .= "$selector.de1, $selector.de2 {{$this->code_style}}\n";
  4217.         }
  4218.  
  4219.         // Add overall styles
  4220.         // note: neglect economy_mode, empty styles are meaningless
  4221.         if ($this->overall_style != '') {
  4222.             $stylesheet .= "$selector {{$this->overall_style}}\n";
  4223.         }
  4224.  
  4225.         // Add styles for links
  4226.         // note: economy mode does not make _any_ sense here
  4227.         //       either the style is empty and thus no selector is needed
  4228.         //       or the appropriate key is given.
  4229.         foreach ($this->link_styles as $key => $style) {
  4230.             if ($style != '') {
  4231.                 switch ($key) {
  4232.                     case <a href="../geshi/core/_geshi.php.html#defineGESHI_LINK">GESHI_LINK</a>:
  4233.                         $stylesheet .= "{$selector}a:link {{$style}}\n";
  4234.                         break;
  4235.                     case GESHI_HOVER:
  4236.                         $stylesheet .= "{$selector}a:hover {{$style}}\n";
  4237.                         break;
  4238.                     case GESHI_ACTIVE:
  4239.                         $stylesheet .= "{$selector}a:active {{$style}}\n";
  4240.                         break;
  4241.                     case GESHI_VISITED:
  4242.                         $stylesheet .= "{$selector}a:visited {{$style}}\n";
  4243.                         break;
  4244.                 }
  4245.             }
  4246.         }
  4247.  
  4248.         // Header and footer
  4249.         // note: neglect economy_mode, empty styles are meaningless
  4250.         if ($this->header_content_style != ''{
  4251.             $stylesheet .= "$selector.head {{$this->header_content_style}}\n";
  4252.         }
  4253.         if ($this->footer_content_style != ''{
  4254.             $stylesheet .= "$selector.foot {{$this->footer_content_style}}\n";
  4255.         }
  4256.  
  4257.         // Styles for important stuff
  4258.         // note: neglect economy_mode, empty styles are meaningless
  4259.         if ($this->important_styles != ''{
  4260.             $stylesheet .= "$selector.imp {{$this->important_styles}}\n";
  4261.         }
  4262.  
  4263.         // Simple line number styles
  4264.         if ((!$economy_mode || $this->line_numbers != GESHI_NO_LINE_NUMBERS&& $this->line_style1 != ''{
  4265.             $stylesheet .= "{$selector}li, {$selector}.li1 {{$this->line_style1}}\n";
  4266.         }
  4267.         if ((!$economy_mode || $this->line_numbers != GESHI_NO_LINE_NUMBERS&& $this->table_linenumber_style != ''{
  4268.             $stylesheet .= "{$selector}.ln {{$this->table_linenumber_style}}\n";
  4269.         }
  4270.         // If there is a style set for fancy line numbers, echo it out
  4271.         if ((!$economy_mode || $this->line_numbers == GESHI_FANCY_LINE_NUMBERS&& $this->line_style2 != ''{
  4272.             $stylesheet .= "{$selector}.li2 {{$this->line_style2}}\n";
  4273.         }
  4274.  
  4275.         // note: empty styles are meaningless
  4276.         foreach ($this->language_data['STYLES']['KEYWORDS'as $group => $styles{
  4277.             if ($styles != '' && (!$economy_mode ||
  4278.                 (isset($this->lexic_permissions['KEYWORDS'][$group]&&
  4279.                 $this->lexic_permissions['KEYWORDS'][$group]))) {
  4280.                 $stylesheet .= "$selector.kw$group {{$styles}}\n";
  4281.             }
  4282.         }
  4283.         foreach ($this->language_data['STYLES']['COMMENTS'as $group => $styles{
  4284.             if ($styles != '' && (!$economy_mode ||
  4285.                 (isset($this->lexic_permissions['COMMENTS'][$group]&&
  4286.                 $this->lexic_permissions['COMMENTS'][$group]||
  4287.                 (!empty($this->language_data['COMMENT_REGEXP']&&
  4288.                 !empty($this->language_data['COMMENT_REGEXP'][$group])))) {
  4289.                 $stylesheet .= "$selector.co$group {{$styles}}\n";
  4290.             }
  4291.         }
  4292.         foreach ($this->language_data['STYLES']['ESCAPE_CHAR'as $group => $styles{
  4293.             if ($styles != '' && (!$economy_mode || $this->lexic_permissions['ESCAPE_CHAR'])) {
  4294.                 // NEW: since 1.0.8 we have to handle hardescapes
  4295.                 if ($group === 'HARD'{
  4296.                     $group '_h';
  4297.                 }
  4298.                 $stylesheet .= "$selector.es$group {{$styles}}\n";
  4299.             }
  4300.         }
  4301.         foreach ($this->language_data['STYLES']['BRACKETS'as $group => $styles{
  4302.             if ($styles != '' && (!$economy_mode || $this->lexic_permissions['BRACKETS'])) {
  4303.                 $stylesheet .= "$selector.br$group {{$styles}}\n";
  4304.             }
  4305.         }
  4306.         foreach ($this->language_data['STYLES']['SYMBOLS'as $group => $styles{
  4307.             if ($styles != '' && (!$economy_mode || $this->lexic_permissions['SYMBOLS'])) {
  4308.                 $stylesheet .= "$selector.sy$group {{$styles}}\n";
  4309.             }
  4310.         }
  4311.         foreach ($this->language_data['STYLES']['STRINGS'as $group => $styles{
  4312.             if ($styles != '' && (!$economy_mode || $this->lexic_permissions['STRINGS'])) {
  4313.                 // NEW: since 1.0.8 we have to handle hardquotes
  4314.                 if ($group === 'HARD'{
  4315.                     $group '_h';
  4316.                 }
  4317.                 $stylesheet .= "$selector.st$group {{$styles}}\n";
  4318.             }
  4319.         }
  4320.         foreach ($this->language_data['STYLES']['NUMBERS'as $group => $styles{
  4321.             if ($styles != '' && (!$economy_mode || $this->lexic_permissions['NUMBERS'])) {
  4322.                 $stylesheet .= "$selector.nu$group {{$styles}}\n";
  4323.             }
  4324.         }
  4325.         foreach ($this->language_data['STYLES']['METHODS'as $group => $styles{
  4326.             if ($styles != '' && (!$economy_mode || $this->lexic_permissions['METHODS'])) {
  4327.                 $stylesheet .= "$selector.me$group {{$styles}}\n";
  4328.             }
  4329.         }
  4330.         // note: neglect economy_mode, empty styles are meaningless
  4331.         foreach ($this->language_data['STYLES']['SCRIPT'as $group => $styles{
  4332.             if ($styles != ''{
  4333.                 $stylesheet .= "$selector.sc$group {{$styles}}\n";
  4334.             }
  4335.         }
  4336.         foreach ($this->language_data['STYLES']['REGEXPS'as $group => $styles{
  4337.             if ($styles != '' && (!$economy_mode ||
  4338.                 (isset($this->lexic_permissions['REGEXPS'][$group]&&
  4339.                 $this->lexic_permissions['REGEXPS'][$group]))) {
  4340.                 if (is_array($this->language_data['REGEXPS'][$group]&&
  4341.                     array_key_exists(GESHI_CLASS$this->language_data['REGEXPS'][$group])) {
  4342.                     $stylesheet .= "$selector.";
  4343.                     $stylesheet .= $this->language_data['REGEXPS'][$group][GESHI_CLASS];
  4344.                     $stylesheet .= " {{$styles}}\n";
  4345.                 else {
  4346.                     $stylesheet .= "$selector.re$group {{$styles}}\n";
  4347.                 }
  4348.             }
  4349.         }
  4350.         // Styles for lines being highlighted extra
  4351.         if (!$economy_mode || (count($this->highlight_extra_lines)!=count($this->highlight_extra_lines_styles))) {
  4352.             $stylesheet .= "{$selector}.ln-xtra, {$selector}li.ln-xtra, {$selector}div.ln-xtra {{$this->highlight_extra_lines_style}}\n";
  4353.         }
  4354.         $stylesheet .= "{$selector}span.xtra { display:block; }\n";
  4355.         foreach ($this->highlight_extra_lines_styles as $lineid => $linestyle{
  4356.             $stylesheet .= "{$selector}.lx$lineid, {$selector}li.lx$lineid, {$selector}div.lx$lineid {{$linestyle}}\n";
  4357.         }
  4358.  
  4359.         return $stylesheet;
  4360.     }
  4361.  
  4362.     /**
  4363.      * Get's the style that is used for the specified line
  4364.      *
  4365.      * @param int The line number information is requested for
  4366.      * @access private
  4367.      * @since 1.0.7.21
  4368.      */
  4369.     function get_line_style($line{
  4370.         //$style = null;
  4371.         $style null;
  4372.         if (isset($this->highlight_extra_lines_styles[$line])) {
  4373.             $style $this->highlight_extra_lines_styles[$line];
  4374.         else // if no "extra" style assigned
  4375.             $style $this->highlight_extra_lines_style;
  4376.         }
  4377.  
  4378.         return $style;
  4379.     }
  4380.  
  4381.     /**
  4382.     * this functions creates an optimized regular expression list
  4383.     * of an array of strings.
  4384.     *
  4385.     * Example:
  4386.     * <code>$list = array('faa', 'foo', 'foobar');
  4387.     *          => string 'f(aa|oo(bar)?)'</code>
  4388.     *
  4389.     * @param $list array of (unquoted) strings
  4390.     * @param $regexp_delimiter your regular expression delimiter, @see preg_quote()
  4391.     * @return string for regular expression
  4392.     * @author Milian Wolff <mail@milianw.de>
  4393.     * @since 1.0.8
  4394.     * @access private
  4395.     */
  4396.     function optimize_regexp_list($list$regexp_delimiter '/'{
  4397.         $regex_chars array('.''\\''+''*''?''[''^'']''$',
  4398.             '('')''{''}''=''!''<''>''|'':'$regexp_delimiter);
  4399.         sort($list);
  4400.         $regexp_list array('');
  4401.         $num_subpatterns 0;
  4402.         $list_key 0;
  4403.  
  4404.         // the tokens which we will use to generate the regexp list
  4405.         $tokens array();
  4406.         $prev_keys array();
  4407.         // go through all entries of the list and generate the token list
  4408.         $cur_len 0;
  4409.         for ($i 0$i_max count($list)$i $i_max++$i{
  4410.             if ($cur_len GESHI_MAX_PCRE_LENGTH{
  4411.                 // seems like the length of this pcre is growing exorbitantly
  4412.                 $regexp_list[++$list_key$this->_optimize_regexp_list_tokens_to_string($tokens);
  4413.                 $num_subpatterns substr_count($regexp_list[$list_key]'(?:');
  4414.                 $tokens array();
  4415.                 $cur_len 0;
  4416.             }
  4417.             $level 0;
  4418.             $entry preg_quote((string) $list[$i]$regexp_delimiter);
  4419.             $pointer &$tokens;
  4420.             // properly assign the new entry to the correct position in the token array
  4421.             // possibly generate smaller common denominator keys
  4422.             while (true{
  4423.                 // get the common denominator
  4424.                 if (isset($prev_keys[$level])) {
  4425.                     if ($prev_keys[$level== $entry{
  4426.                         // this is a duplicate entry, skip it
  4427.                         continue 2;
  4428.                     }
  4429.                     $char 0;
  4430.                     while (isset($entry[$char]&& isset($prev_keys[$level][$char])
  4431.                             && $entry[$char== $prev_keys[$level][$char]{
  4432.                         ++$char;
  4433.                     }
  4434.                     if ($char 0{
  4435.                         // this entry has at least some chars in common with the current key
  4436.                         if ($char == strlen($prev_keys[$level])) {
  4437.                             // current key is totally matched, i.e. this entry has just some bits appended
  4438.                             $pointer &$pointer[$prev_keys[$level]];
  4439.                         else {
  4440.                             // only part of the keys match
  4441.                             $new_key_part1 substr($prev_keys[$level]0$char);
  4442.                             $new_key_part2 substr($prev_keys[$level]$char);
  4443.  
  4444.                             if (in_array($new_key_part1[0]$regex_chars)
  4445.                                 || in_array($new_key_part2[0]$regex_chars)) {
  4446.                                 // this is bad, a regex char as first character
  4447.                                 $pointer[$entryarray('' => true);
  4448.                                 array_splice($prev_keys$levelcount($prev_keys)$entry);
  4449.                                 $cur_len += strlen($entry);
  4450.                                 continue;
  4451.                             else {
  4452.                                 // relocate previous tokens
  4453.                                 $pointer[$new_key_part1array($new_key_part2 => $pointer[$prev_keys[$level]]);
  4454.                                 unset($pointer[$prev_keys[$level]]);
  4455.                                 $pointer &$pointer[$new_key_part1];
  4456.                                 // recreate key index
  4457.                                 array_splice($prev_keys$levelcount($prev_keys)array($new_key_part1$new_key_part2));
  4458.                                 $cur_len += strlen($new_key_part2);
  4459.                             }
  4460.                         }
  4461.                         ++$level;
  4462.                         $entry substr($entry$char);
  4463.                         continue;
  4464.                     }
  4465.                     // else: fall trough, i.e. no common denominator was found
  4466.                 }
  4467.                 if ($level == && !empty($tokens)) {
  4468.                     // we can dump current tokens into the string and throw them away afterwards
  4469.                     $new_entry $this->_optimize_regexp_list_tokens_to_string($tokens);
  4470.                     $new_subpatterns substr_count($new_entry'(?:');
  4471.                     if (GESHI_MAX_PCRE_SUBPATTERNS && $num_subpatterns $new_subpatterns GESHI_MAX_PCRE_SUBPATTERNS{
  4472.                         $regexp_list[++$list_key$new_entry;
  4473.                         $num_subpatterns $new_subpatterns;
  4474.                     else {
  4475.                         if (!empty($regexp_list[$list_key])) {
  4476.                             $new_entry '|' $new_entry;
  4477.                         }
  4478.                         $regexp_list[$list_key.= $new_entry;
  4479.                         $num_subpatterns += $new_subpatterns;
  4480.                     }
  4481.                     $tokens array();
  4482.                     $cur_len 0;
  4483.                 }
  4484.                 // no further common denominator found
  4485.                 $pointer[$entryarray('' => true);
  4486.                 array_splice($prev_keys$levelcount($prev_keys)$entry);
  4487.  
  4488.                 $cur_len += strlen($entry);
  4489.                 break;
  4490.             }
  4491.             unset($list[$i]);
  4492.         }
  4493.         // make sure the last tokens get converted as well
  4494.         $new_entry $this->_optimize_regexp_list_tokens_to_string($tokens);
  4495.         if (GESHI_MAX_PCRE_SUBPATTERNS && $num_subpatterns substr_count($new_entry'(?:'GESHI_MAX_PCRE_SUBPATTERNS{
  4496.             $regexp_list[++$list_key$new_entry;
  4497.         else {
  4498.             if (!empty($regexp_list[$list_key])) {
  4499.                 $new_entry '|' $new_entry;
  4500.             }
  4501.             $regexp_list[$list_key.= $new_entry;
  4502.         }
  4503.         return $regexp_list;
  4504.     }
  4505.     /**
  4506.     * this function creates the appropriate regexp string of an token array
  4507.     * you should not call this function directly, @see $this->optimize_regexp_list().
  4508.     *
  4509.     * @param &$tokens array of tokens
  4510.     * @param $recursed bool to know wether we recursed or not
  4511.     * @return string
  4512.     * @author Milian Wolff <mail@milianw.de>
  4513.     * @since 1.0.8
  4514.     * @access private
  4515.     */
  4516.     function _optimize_regexp_list_tokens_to_string(&$tokens$recursed false{
  4517.         $list '';
  4518.         foreach ($tokens as $token => $sub_tokens{
  4519.             $list .= $token;
  4520.             $close_entry = isset($sub_tokens['']);
  4521.             unset($sub_tokens['']);
  4522.             if (!empty($sub_tokens)) {
  4523.                 $list .= '(?:' $this->_optimize_regexp_list_tokens_to_string($sub_tokenstrue')';
  4524.                 if ($close_entry{
  4525.                     // make sub_tokens optional
  4526.                     $list .= '?';
  4527.                 }
  4528.             }
  4529.             $list .= '|';
  4530.         }
  4531.         if (!$recursed{
  4532.             // do some optimizations
  4533.             // common trailing strings
  4534.             // BUGGY!
  4535.             //$list = preg_replace_callback('#(?<=^|\:|\|)\w+?(\w+)(?:\|.+\1)+(?=\|)#', create_function(
  4536.             //    '$matches', 'return "(?:" . preg_replace("#" . preg_quote($matches[1], "#") . "(?=\||$)#", "", $matches[0]) . ")" . $matches[1];'), $list);
  4537.             // (?:p)? => p?
  4538.             $list preg_replace('#\(\?\:(.)\)\?#''\1?'$list);
  4539.             // (?:a|b|c|d|...)? => [abcd...]?
  4540.             // TODO: a|bb|c => [ac]|bb
  4541.             static $callback_2;
  4542.             if (!isset($callback_2)) {
  4543.                 $callback_2 create_function('$matches''return "[" . str_replace("|", "", $matches[1]) . "]";');
  4544.             }
  4545.             $list preg_replace_callback('#\(\?\:((?:.\|)+.)\)#'$callback_2$list);
  4546.         }
  4547.         // return $list without trailing pipe
  4548.         return substr($list0-1);
  4549.     }
  4550. // End Class GeSHi
  4551.  
  4552.  
  4553. if (!function_exists('geshi_highlight')) {
  4554.     /**
  4555.      * Easy way to highlight stuff. Behaves just like highlight_string
  4556.      *
  4557.      * @param string The code to highlight
  4558.      * @param string The language to highlight the code in
  4559.      * @param string The path to the language files. You can leave this blank if you need
  4560.      *               as from version 1.0.7 the path should be automatically detected
  4561.      * @param boolean Whether to return the result or to echo
  4562.      * @return string The code highlighted (if $return is true)
  4563.      * @since 1.0.2
  4564.      */
  4565.     function geshi_highlight($string$language$path null$return false{
  4566.         $geshi new GeSHi($string$language$path);
  4567.         $geshi->set_header_type(GESHI_HEADER_NONE);
  4568.  
  4569.         if ($return{
  4570.             return '<code>' $geshi->parse_code('</code>';
  4571.         }
  4572.  
  4573.         echo '<code>' $geshi->parse_code('</code>';
  4574.  
  4575.         if ($geshi->error()) {
  4576.             return false;
  4577.         }
  4578.         return true;
  4579.     }
  4580. }
  4581.  
  4582. ?>

Documentation generated on Thu, 25 Dec 2008 14:34:52 +0100 by phpDocumentor 1.4.2