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

Documentation generated on Thu, 07 Aug 2008 22:35:41 +0200 by phpDocumentor 1.4.2