PageRenderTime 35ms CodeModel.GetById 29ms RepoModel.GetById 8ms app.codeStats 2ms

/wp-content/plugins/mysql-profiler/lib/geshi/geshi.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 2913 lines | 1487 code | 213 blank | 1213 comment | 478 complexity | ae8f957a5e00d4d5ed7bcc73a22ab1f3 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  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>
  31. * @copyright (C) 2004 - 2007 Nigel McNie
  32. * @license http://gnu.org/copyleft/gpl.html GNU GPL
  33. *
  34. */
  35. //
  36. // GeSHi Constants
  37. // You should use these constant names in your programs instead of
  38. // their values - you never know when a value may change in a future
  39. // version
  40. //
  41. /** The version of this GeSHi file */
  42. define('GESHI_VERSION', '1.0.7.20');
  43. // Define the root directory for the GeSHi code tree
  44. if (!defined('GESHI_ROOT')) {
  45. /** The root directory for GeSHi */
  46. define('GESHI_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
  47. }
  48. /** The language file directory for GeSHi
  49. @access private */
  50. define('GESHI_LANG_ROOT', GESHI_ROOT . 'geshi' . DIRECTORY_SEPARATOR);
  51. // Line numbers - use with enable_line_numbers()
  52. /** Use no line numbers when building the result */
  53. define('GESHI_NO_LINE_NUMBERS', 0);
  54. /** Use normal line numbers when building the result */
  55. define('GESHI_NORMAL_LINE_NUMBERS', 1);
  56. /** Use fancy line numbers when building the result */
  57. define('GESHI_FANCY_LINE_NUMBERS', 2);
  58. // Container HTML type
  59. /** Use nothing to surround the source */
  60. define('GESHI_HEADER_NONE', 0);
  61. /** Use a "div" to surround the source */
  62. define('GESHI_HEADER_DIV', 1);
  63. /** Use a "pre" to surround the source */
  64. define('GESHI_HEADER_PRE', 2);
  65. // Capatalisation constants
  66. /** Lowercase keywords found */
  67. define('GESHI_CAPS_NO_CHANGE', 0);
  68. /** Uppercase keywords found */
  69. define('GESHI_CAPS_UPPER', 1);
  70. /** Leave keywords found as the case that they are */
  71. define('GESHI_CAPS_LOWER', 2);
  72. // Link style constants
  73. /** Links in the source in the :link state */
  74. define('GESHI_LINK', 0);
  75. /** Links in the source in the :hover state */
  76. define('GESHI_HOVER', 1);
  77. /** Links in the source in the :active state */
  78. define('GESHI_ACTIVE', 2);
  79. /** Links in the source in the :visited state */
  80. define('GESHI_VISITED', 3);
  81. // Important string starter/finisher
  82. // Note that if you change these, they should be as-is: i.e., don't
  83. // write them as if they had been run through htmlentities()
  84. /** The starter for important parts of the source */
  85. define('GESHI_START_IMPORTANT', '<BEGIN GeSHi>');
  86. /** The ender for important parts of the source */
  87. define('GESHI_END_IMPORTANT', '<END GeSHi>');
  88. /**#@+
  89. * @access private
  90. */
  91. // When strict mode applies for a language
  92. /** Strict mode never applies (this is the most common) */
  93. define('GESHI_NEVER', 0);
  94. /** Strict mode *might* apply, and can be enabled or
  95. disabled by {@link GeSHi::enable_strict_mode()} */
  96. define('GESHI_MAYBE', 1);
  97. /** Strict mode always applies */
  98. define('GESHI_ALWAYS', 2);
  99. // Advanced regexp handling constants, used in language files
  100. /** The key of the regex array defining what to search for */
  101. define('GESHI_SEARCH', 0);
  102. /** The key of the regex array defining what bracket group in a
  103. matched search to use as a replacement */
  104. define('GESHI_REPLACE', 1);
  105. /** The key of the regex array defining any modifiers to the regular expression */
  106. define('GESHI_MODIFIERS', 2);
  107. /** The key of the regex array defining what bracket group in a
  108. matched search to put before the replacement */
  109. define('GESHI_BEFORE', 3);
  110. /** The key of the regex array defining what bracket group in a
  111. matched search to put after the replacement */
  112. define('GESHI_AFTER', 4);
  113. /** The key of the regex array defining a custom keyword to use
  114. for this regexp's html tag class */
  115. define('GESHI_CLASS', 5);
  116. /** Used in language files to mark comments */
  117. define('GESHI_COMMENTS', 0);
  118. // Error detection - use these to analyse faults
  119. /** No sourcecode to highlight was specified
  120. * @deprecated
  121. */
  122. define('GESHI_ERROR_NO_INPUT', 1);
  123. /** The language specified does not exist */
  124. define('GESHI_ERROR_NO_SUCH_LANG', 2);
  125. /** GeSHi could not open a file for reading (generally a language file) */
  126. define('GESHI_ERROR_FILE_NOT_READABLE', 3);
  127. /** The header type passed to {@link GeSHi::set_header_type()} was invalid */
  128. define('GESHI_ERROR_INVALID_HEADER_TYPE', 4);
  129. /** The line number type passed to {@link GeSHi::enable_line_numbers()} was invalid */
  130. define('GESHI_ERROR_INVALID_LINE_NUMBER_TYPE', 5);
  131. /**#@-*/
  132. /**
  133. * The GeSHi Class.
  134. *
  135. * Please refer to the documentation for GeSHi 1.0.X that is available
  136. * at http://qbnz.com/highlighter/documentation.php for more information
  137. * about how to use this class.
  138. *
  139. * @package geshi
  140. * @author Nigel McNie <nigel@geshi.org>
  141. * @copyright (C) 2004 - 2007 Nigel McNie
  142. */
  143. class GeSHi {
  144. /**#@+
  145. * @access private
  146. */
  147. /**
  148. * The source code to highlight
  149. * @var string
  150. */
  151. var $source = '';
  152. /**
  153. * The language to use when highlighting
  154. * @var string
  155. */
  156. var $language = '';
  157. /**
  158. * The data for the language used
  159. * @var array
  160. */
  161. var $language_data = array();
  162. /**
  163. * The path to the language files
  164. * @var string
  165. */
  166. var $language_path = GESHI_LANG_ROOT;
  167. /**
  168. * The error message associated with an error
  169. * @var string
  170. * @todo check err reporting works
  171. */
  172. var $error = false;
  173. /**
  174. * Possible error messages
  175. * @var array
  176. */
  177. var $error_messages = array(
  178. GESHI_ERROR_NO_SUCH_LANG => 'GeSHi could not find the language {LANGUAGE} (using path {PATH})',
  179. GESHI_ERROR_FILE_NOT_READABLE => 'The file specified for load_from_file was not readable',
  180. GESHI_ERROR_INVALID_HEADER_TYPE => 'The header type specified is invalid',
  181. GESHI_ERROR_INVALID_LINE_NUMBER_TYPE => 'The line number type specified is invalid'
  182. );
  183. /**
  184. * Whether highlighting is strict or not
  185. * @var boolean
  186. */
  187. var $strict_mode = false;
  188. /**
  189. * Whether to use CSS classes in output
  190. * @var boolean
  191. */
  192. var $use_classes = false;
  193. /**
  194. * The type of header to use. Can be one of the following
  195. * values:
  196. *
  197. * - GESHI_HEADER_PRE: Source is outputted in a "pre" HTML element.
  198. * - GESHI_HEADER_DIV: Source is outputted in a "div" HTML element.
  199. * - GESHI_HEADER_NONE: No header is outputted.
  200. *
  201. * @var int
  202. */
  203. var $header_type = GESHI_HEADER_PRE;
  204. /**
  205. * Array of permissions for which lexics should be highlighted
  206. * @var array
  207. */
  208. var $lexic_permissions = array(
  209. 'KEYWORDS' => array(),
  210. 'COMMENTS' => array('MULTI' => true),
  211. 'REGEXPS' => array(),
  212. 'ESCAPE_CHAR' => true,
  213. 'BRACKETS' => true,
  214. 'SYMBOLS' => true,
  215. 'STRINGS' => true,
  216. 'NUMBERS' => true,
  217. 'METHODS' => true,
  218. 'SCRIPT' => true
  219. );
  220. /**
  221. * The time it took to parse the code
  222. * @var double
  223. */
  224. var $time = 0;
  225. /**
  226. * The content of the header block
  227. * @var string
  228. */
  229. var $header_content = '';
  230. /**
  231. * The content of the footer block
  232. * @var string
  233. */
  234. var $footer_content = '';
  235. /**
  236. * The style of the header block
  237. * @var string
  238. */
  239. var $header_content_style = '';
  240. /**
  241. * The style of the footer block
  242. * @var string
  243. */
  244. var $footer_content_style = '';
  245. /**
  246. * Tells if a block around the highlighted source should be forced
  247. * if not using line numbering
  248. * @var boolean
  249. */
  250. var $force_code_block = false;
  251. /**
  252. * The styles for hyperlinks in the code
  253. * @var array
  254. */
  255. var $link_styles = array();
  256. /**
  257. * Whether important blocks should be recognised or not
  258. * @var boolean
  259. * @deprecated
  260. * @todo REMOVE THIS FUNCTIONALITY!
  261. */
  262. var $enable_important_blocks = false;
  263. /**
  264. * Styles for important parts of the code
  265. * @var string
  266. * @deprecated
  267. * @todo As above - rethink the whole idea of important blocks as it is buggy and
  268. * will be hard to implement in 1.2
  269. */
  270. var $important_styles = 'font-weight: bold; color: red;'; // Styles for important parts of the code
  271. /**
  272. * Whether CSS IDs should be added to the code
  273. * @var boolean
  274. */
  275. var $add_ids = false;
  276. /**
  277. * Lines that should be highlighted extra
  278. * @var array
  279. */
  280. var $highlight_extra_lines = array();
  281. /**
  282. * Styles of extra-highlighted lines
  283. * @var string
  284. */
  285. var $highlight_extra_lines_style = 'color: #cc0; background-color: #ffc;';
  286. /**
  287. * The line ending
  288. * If null, nl2br() will be used on the result string.
  289. * Otherwise, all instances of \n will be replaced with $line_ending
  290. * @var string
  291. */
  292. var $line_ending = null;
  293. /**
  294. * Number at which line numbers should start at
  295. * @var int
  296. */
  297. var $line_numbers_start = 1;
  298. /**
  299. * The overall style for this code block
  300. * @var string
  301. */
  302. var $overall_style = '';
  303. /**
  304. * The style for the actual code
  305. * @var string
  306. */
  307. var $code_style = 'font-family: \'Courier New\', Courier, monospace; font-weight: normal;';
  308. /**
  309. * The overall class for this code block
  310. * @var string
  311. */
  312. var $overall_class = '';
  313. /**
  314. * The overall ID for this code block
  315. * @var string
  316. */
  317. var $overall_id = '';
  318. /**
  319. * Line number styles
  320. * @var string
  321. */
  322. var $line_style1 = 'font-family: \'Courier New\', Courier, monospace; color: black; font-weight: normal; font-style: normal;';
  323. /**
  324. * Line number styles for fancy lines
  325. * @var string
  326. */
  327. var $line_style2 = 'font-weight: bold;';
  328. /**
  329. * Flag for how line nubmers are displayed
  330. * @var boolean
  331. */
  332. var $line_numbers = GESHI_NO_LINE_NUMBERS;
  333. /**
  334. * The "nth" value for fancy line highlighting
  335. * @var int
  336. */
  337. var $line_nth_row = 0;
  338. /**
  339. * The size of tab stops
  340. * @var int
  341. */
  342. var $tab_width = 8;
  343. /**
  344. * Should we use language-defined tab stop widths?
  345. * @var int
  346. */
  347. var $use_language_tab_width = false;
  348. /**
  349. * Default target for keyword links
  350. * @var string
  351. */
  352. var $link_target = '';
  353. /**
  354. * The encoding to use for entity encoding
  355. * NOTE: no longer used
  356. * @var string
  357. */
  358. var $encoding = 'ISO-8859-1';
  359. /**
  360. * Should keywords be linked?
  361. * @var boolean
  362. */
  363. var $keyword_links = true;
  364. /**#@-*/
  365. /**
  366. * Creates a new GeSHi object, with source and language
  367. *
  368. * @param string The source code to highlight
  369. * @param string The language to highlight the source with
  370. * @param string The path to the language file directory. <b>This
  371. * is deprecated!</b> I've backported the auto path
  372. * detection from the 1.1.X dev branch, so now it
  373. * should be automatically set correctly. If you have
  374. * renamed the language directory however, you will
  375. * still need to set the path using this parameter or
  376. * {@link GeSHi::set_language_path()}
  377. * @since 1.0.0
  378. */
  379. function GeSHi($source, $language, $path = '') {
  380. $this->set_source($source);
  381. $this->set_language_path($path);
  382. $this->set_language($language);
  383. }
  384. /**
  385. * Returns an error message associated with the last GeSHi operation,
  386. * or false if no error has occured
  387. *
  388. * @return string|false An error message if there has been an error, else false
  389. * @since 1.0.0
  390. */
  391. function error() {
  392. if ($this->error) {
  393. $msg = $this->error_messages[$this->error];
  394. $debug_tpl_vars = array(
  395. '{LANGUAGE}' => $this->language,
  396. '{PATH}' => $this->language_path
  397. );
  398. foreach ($debug_tpl_vars as $tpl => $var) {
  399. $msg = str_replace($tpl, $var, $msg);
  400. }
  401. return "<br /><strong>GeSHi Error:</strong> $msg (code $this->error)<br />";
  402. }
  403. return false;
  404. }
  405. /**
  406. * Gets a human-readable language name (thanks to Simon Patterson
  407. * for the idea :))
  408. *
  409. * @return string The name for the current language
  410. * @since 1.0.2
  411. */
  412. function get_language_name() {
  413. if (GESHI_ERROR_NO_SUCH_LANG == $this->error) {
  414. return $this->language_data['LANG_NAME'] . ' (Unknown Language)';
  415. }
  416. return $this->language_data['LANG_NAME'];
  417. }
  418. /**
  419. * Sets the source code for this object
  420. *
  421. * @param string The source code to highlight
  422. * @since 1.0.0
  423. */
  424. function set_source($source) {
  425. $this->source = $source;
  426. $this->highlight_extra_lines = array();
  427. }
  428. /**
  429. * Sets the language for this object
  430. *
  431. * @param string The name of the language to use
  432. * @since 1.0.0
  433. */
  434. function set_language($language) {
  435. $this->error = false;
  436. $this->strict_mode = GESHI_NEVER;
  437. $language = preg_replace('#[^a-zA-Z0-9\-_]#', '', $language);
  438. $this->language = strtolower($language);
  439. $file_name = $this->language_path . $this->language . '.php';
  440. if (!is_readable($file_name)) {
  441. $this->error = GESHI_ERROR_NO_SUCH_LANG;
  442. return;
  443. }
  444. // Load the language for parsing
  445. $this->load_language($file_name);
  446. }
  447. /**
  448. * Sets the path to the directory containing the language files. Note
  449. * that this path is relative to the directory of the script that included
  450. * geshi.php, NOT geshi.php itself.
  451. *
  452. * @param string The path to the language directory
  453. * @since 1.0.0
  454. * @deprecated The path to the language files should now be automatically
  455. * detected, so this method should no longer be needed. The
  456. * 1.1.X branch handles manual setting of the path differently
  457. * so this method will disappear in 1.2.0.
  458. */
  459. function set_language_path($path) {
  460. if ($path) {
  461. $this->language_path = ('/' == substr($path, strlen($path) - 1, 1)) ? $path : $path . '/';
  462. $this->set_language($this->language); // otherwise set_language_path has no effect
  463. }
  464. }
  465. /**
  466. * Sets the type of header to be used.
  467. *
  468. * If GESHI_HEADER_DIV is used, the code is surrounded in a "div".This
  469. * means more source code but more control over tab width and line-wrapping.
  470. * GESHI_HEADER_PRE means that a "pre" is used - less source, but less
  471. * control. Default is GESHI_HEADER_PRE.
  472. *
  473. * From 1.0.7.2, you can use GESHI_HEADER_NONE to specify that no header code
  474. * should be outputted.
  475. *
  476. * @param int The type of header to be used
  477. * @since 1.0.0
  478. */
  479. function set_header_type($type) {
  480. if (GESHI_HEADER_DIV != $type && GESHI_HEADER_PRE != $type && GESHI_HEADER_NONE != $type) {
  481. $this->error = GESHI_ERROR_INVALID_HEADER_TYPE;
  482. return;
  483. }
  484. $this->header_type = $type;
  485. // Set a default overall style if the header is a <div>
  486. if (GESHI_HEADER_DIV == $type && !$this->overall_style) {
  487. $this->overall_style = 'font-family: monospace;';
  488. }
  489. }
  490. /**
  491. * Sets the styles for the code that will be outputted
  492. * when this object is parsed. The style should be a
  493. * string of valid stylesheet declarations
  494. *
  495. * @param string The overall style for the outputted code block
  496. * @param boolean Whether to merge the styles with the current styles or not
  497. * @since 1.0.0
  498. */
  499. function set_overall_style($style, $preserve_defaults = false) {
  500. if (!$preserve_defaults) {
  501. $this->overall_style = $style;
  502. }
  503. else {
  504. $this->overall_style .= $style;
  505. }
  506. }
  507. /**
  508. * Sets the overall classname for this block of code. This
  509. * class can then be used in a stylesheet to style this object's
  510. * output
  511. *
  512. * @param string The class name to use for this block of code
  513. * @since 1.0.0
  514. */
  515. function set_overall_class($class) {
  516. $this->overall_class = $class;
  517. }
  518. /**
  519. * Sets the overall id for this block of code. This id can then
  520. * be used in a stylesheet to style this object's output
  521. *
  522. * @param string The ID to use for this block of code
  523. * @since 1.0.0
  524. */
  525. function set_overall_id($id) {
  526. $this->overall_id = $id;
  527. }
  528. /**
  529. * Sets whether CSS classes should be used to highlight the source. Default
  530. * is off, calling this method with no arguments will turn it on
  531. *
  532. * @param boolean Whether to turn classes on or not
  533. * @since 1.0.0
  534. */
  535. function enable_classes($flag = true) {
  536. $this->use_classes = ($flag) ? true : false;
  537. }
  538. /**
  539. * Sets the style for the actual code. This should be a string
  540. * containing valid stylesheet declarations. If $preserve_defaults is
  541. * true, then styles are merged with the default styles, with the
  542. * user defined styles having priority
  543. *
  544. * Note: Use this method to override any style changes you made to
  545. * the line numbers if you are using line numbers, else the line of
  546. * code will have the same style as the line number! Consult the
  547. * GeSHi documentation for more information about this.
  548. *
  549. * @param string The style to use for actual code
  550. * @param boolean Whether to merge the current styles with the new styles
  551. */
  552. function set_code_style($style, $preserve_defaults = false) {
  553. if (!$preserve_defaults) {
  554. $this->code_style = $style;
  555. }
  556. else {
  557. $this->code_style .= $style;
  558. }
  559. }
  560. /**
  561. * Sets the styles for the line numbers.
  562. *
  563. * @param string The style for the line numbers that are "normal"
  564. * @param string|boolean If a string, this is the style of the line
  565. * numbers that are "fancy", otherwise if boolean then this
  566. * defines whether the normal styles should be merged with the
  567. * new normal styles or not
  568. * @param boolean If set, is the flag for whether to merge the "fancy"
  569. * styles with the current styles or not
  570. * @since 1.0.2
  571. */
  572. function set_line_style($style1, $style2 = '', $preserve_defaults = false) {
  573. if (is_bool($style2)) {
  574. $preserve_defaults = $style2;
  575. $style2 = '';
  576. }
  577. if (!$preserve_defaults) {
  578. $this->line_style1 = $style1;
  579. $this->line_style2 = $style2;
  580. }
  581. else {
  582. $this->line_style1 .= $style1;
  583. $this->line_style2 .= $style2;
  584. }
  585. }
  586. /**
  587. * Sets whether line numbers should be displayed.
  588. *
  589. * Valid values for the first parameter are:
  590. *
  591. * - GESHI_NO_LINE_NUMBERS: Line numbers will not be displayed
  592. * - GESHI_NORMAL_LINE_NUMBERS: Line numbers will be displayed
  593. * - GESHI_FANCY_LINE_NUMBERS: Fancy line numbers will be displayed
  594. *
  595. * For fancy line numbers, the second parameter is used to signal which lines
  596. * are to be fancy. For example, if the value of this parameter is 5 then every
  597. * 5th line will be fancy.
  598. *
  599. * @param int How line numbers should be displayed
  600. * @param int Defines which lines are fancy
  601. * @since 1.0.0
  602. */
  603. function enable_line_numbers($flag, $nth_row = 5) {
  604. if (GESHI_NO_LINE_NUMBERS != $flag && GESHI_NORMAL_LINE_NUMBERS != $flag
  605. && GESHI_FANCY_LINE_NUMBERS != $flag) {
  606. $this->error = GESHI_ERROR_INVALID_LINE_NUMBER_TYPE;
  607. }
  608. $this->line_numbers = $flag;
  609. $this->line_nth_row = $nth_row;
  610. }
  611. /**
  612. * Sets the style for a keyword group. If $preserve_defaults is
  613. * true, then styles are merged with the default styles, with the
  614. * user defined styles having priority
  615. *
  616. * @param int The key of the keyword group to change the styles of
  617. * @param string The style to make the keywords
  618. * @param boolean Whether to merge the new styles with the old or just
  619. * to overwrite them
  620. * @since 1.0.0
  621. */
  622. function set_keyword_group_style($key, $style, $preserve_defaults = false) {
  623. if (!$preserve_defaults) {
  624. $this->language_data['STYLES']['KEYWORDS'][$key] = $style;
  625. }
  626. else {
  627. $this->language_data['STYLES']['KEYWORDS'][$key] .= $style;
  628. }
  629. }
  630. /**
  631. * Turns highlighting on/off for a keyword group
  632. *
  633. * @param int The key of the keyword group to turn on or off
  634. * @param boolean Whether to turn highlighting for that group on or off
  635. * @since 1.0.0
  636. */
  637. function set_keyword_group_highlighting($key, $flag = true) {
  638. $this->lexic_permissions['KEYWORDS'][$key] = ($flag) ? true : false;
  639. }
  640. /**
  641. * Sets the styles for comment groups. If $preserve_defaults is
  642. * true, then styles are merged with the default styles, with the
  643. * user defined styles having priority
  644. *
  645. * @param int The key of the comment group to change the styles of
  646. * @param string The style to make the comments
  647. * @param boolean Whether to merge the new styles with the old or just
  648. * to overwrite them
  649. * @since 1.0.0
  650. */
  651. function set_comments_style($key, $style, $preserve_defaults = false) {
  652. if (!$preserve_defaults) {
  653. $this->language_data['STYLES']['COMMENTS'][$key] = $style;
  654. }
  655. else {
  656. $this->language_data['STYLES']['COMMENTS'][$key] .= $style;
  657. }
  658. }
  659. /**
  660. * Turns highlighting on/off for comment groups
  661. *
  662. * @param int The key of the comment group to turn on or off
  663. * @param boolean Whether to turn highlighting for that group on or off
  664. * @since 1.0.0
  665. */
  666. function set_comments_highlighting($key, $flag = true) {
  667. $this->lexic_permissions['COMMENTS'][$key] = ($flag) ? true : false;
  668. }
  669. /**
  670. * Sets the styles for escaped characters. If $preserve_defaults is
  671. * true, then styles are merged with the default styles, with the
  672. * user defined styles having priority
  673. *
  674. * @param string The style to make the escape characters
  675. * @param boolean Whether to merge the new styles with the old or just
  676. * to overwrite them
  677. * @since 1.0.0
  678. */
  679. function set_escape_characters_style($style, $preserve_defaults = false) {
  680. if (!$preserve_defaults) {
  681. $this->language_data['STYLES']['ESCAPE_CHAR'][0] = $style;
  682. }
  683. else {
  684. $this->language_data['STYLES']['ESCAPE_CHAR'][0] .= $style;
  685. }
  686. }
  687. /**
  688. * Turns highlighting on/off for escaped characters
  689. *
  690. * @param boolean Whether to turn highlighting for escape characters on or off
  691. * @since 1.0.0
  692. */
  693. function set_escape_characters_highlighting($flag = true) {
  694. $this->lexic_permissions['ESCAPE_CHAR'] = ($flag) ? true : false;
  695. }
  696. /**
  697. * Sets the styles for brackets. If $preserve_defaults is
  698. * true, then styles are merged with the default styles, with the
  699. * user defined styles having priority
  700. *
  701. * This method is DEPRECATED: use set_symbols_style instead.
  702. * This method will be removed in 1.2.X
  703. *
  704. * @param string The style to make the brackets
  705. * @param boolean Whether to merge the new styles with the old or just
  706. * to overwrite them
  707. * @since 1.0.0
  708. * @deprecated In favour of set_symbols_style
  709. */
  710. function set_brackets_style($style, $preserve_defaults = false) {
  711. if (!$preserve_defaults) {
  712. $this->language_data['STYLES']['BRACKETS'][0] = $style;
  713. }
  714. else {
  715. $this->language_data['STYLES']['BRACKETS'][0] .= $style;
  716. }
  717. }
  718. /**
  719. * Turns highlighting on/off for brackets
  720. *
  721. * This method is DEPRECATED: use set_symbols_highlighting instead.
  722. * This method will be remove in 1.2.X
  723. *
  724. * @param boolean Whether to turn highlighting for brackets on or off
  725. * @since 1.0.0
  726. * @deprecated In favour of set_symbols_highlighting
  727. */
  728. function set_brackets_highlighting($flag) {
  729. $this->lexic_permissions['BRACKETS'] = ($flag) ? true : false;
  730. }
  731. /**
  732. * Sets the styles for symbols. If $preserve_defaults is
  733. * true, then styles are merged with the default styles, with the
  734. * user defined styles having priority
  735. *
  736. * @param string The style to make the symbols
  737. * @param boolean Whether to merge the new styles with the old or just
  738. * to overwrite them
  739. * @since 1.0.1
  740. */
  741. function set_symbols_style($style, $preserve_defaults = false) {
  742. if (!$preserve_defaults) {
  743. $this->language_data['STYLES']['SYMBOLS'][0] = $style;
  744. }
  745. else {
  746. $this->language_data['STYLES']['SYMBOLS'][0] .= $style;
  747. }
  748. // For backward compatibility
  749. $this->set_brackets_style ($style, $preserve_defaults);
  750. }
  751. /**
  752. * Turns highlighting on/off for symbols
  753. *
  754. * @param boolean Whether to turn highlighting for symbols on or off
  755. * @since 1.0.0
  756. */
  757. function set_symbols_highlighting($flag) {
  758. $this->lexic_permissions['SYMBOLS'] = ($flag) ? true : false;
  759. // For backward compatibility
  760. $this->set_brackets_highlighting ($flag);
  761. }
  762. /**
  763. * Sets the styles for strings. If $preserve_defaults is
  764. * true, then styles are merged with the default styles, with the
  765. * user defined styles having priority
  766. *
  767. * @param string The style to make the escape characters
  768. * @param boolean Whether to merge the new styles with the old or just
  769. * to overwrite them
  770. * @since 1.0.0
  771. */
  772. function set_strings_style($style, $preserve_defaults = false) {
  773. if (!$preserve_defaults) {
  774. $this->language_data['STYLES']['STRINGS'][0] = $style;
  775. }
  776. else {
  777. $this->language_data['STYLES']['STRINGS'][0] .= $style;
  778. }
  779. }
  780. /**
  781. * Turns highlighting on/off for strings
  782. *
  783. * @param boolean Whether to turn highlighting for strings on or off
  784. * @since 1.0.0
  785. */
  786. function set_strings_highlighting($flag) {
  787. $this->lexic_permissions['STRINGS'] = ($flag) ? true : false;
  788. }
  789. /**
  790. * Sets the styles for numbers. If $preserve_defaults is
  791. * true, then styles are merged with the default styles, with the
  792. * user defined styles having priority
  793. *
  794. * @param string The style to make the numbers
  795. * @param boolean Whether to merge the new styles with the old or just
  796. * to overwrite them
  797. * @since 1.0.0
  798. */
  799. function set_numbers_style($style, $preserve_defaults = false) {
  800. if (!$preserve_defaults) {
  801. $this->language_data['STYLES']['NUMBERS'][0] = $style;
  802. }
  803. else {
  804. $this->language_data['STYLES']['NUMBERS'][0] .= $style;
  805. }
  806. }
  807. /**
  808. * Turns highlighting on/off for numbers
  809. *
  810. * @param boolean Whether to turn highlighting for numbers on or off
  811. * @since 1.0.0
  812. */
  813. function set_numbers_highlighting($flag) {
  814. $this->lexic_permissions['NUMBERS'] = ($flag) ? true : false;
  815. }
  816. /**
  817. * Sets the styles for methods. $key is a number that references the
  818. * appropriate "object splitter" - see the language file for the language
  819. * you are highlighting to get this number. If $preserve_defaults is
  820. * true, then styles are merged with the default styles, with the
  821. * user defined styles having priority
  822. *
  823. * @param int The key of the object splitter to change the styles of
  824. * @param string The style to make the methods
  825. * @param boolean Whether to merge the new styles with the old or just
  826. * to overwrite them
  827. * @since 1.0.0
  828. */
  829. function set_methods_style($key, $style, $preserve_defaults = false) {
  830. if (!$preserve_defaults) {
  831. $this->language_data['STYLES']['METHODS'][$key] = $style;
  832. }
  833. else {
  834. $this->language_data['STYLES']['METHODS'][$key] .= $style;
  835. }
  836. }
  837. /**
  838. * Turns highlighting on/off for methods
  839. *
  840. * @param boolean Whether to turn highlighting for methods on or off
  841. * @since 1.0.0
  842. */
  843. function set_methods_highlighting($flag) {
  844. $this->lexic_permissions['METHODS'] = ($flag) ? true : false;
  845. }
  846. /**
  847. * Sets the styles for regexps. If $preserve_defaults is
  848. * true, then styles are merged with the default styles, with the
  849. * user defined styles having priority
  850. *
  851. * @param string The style to make the regular expression matches
  852. * @param boolean Whether to merge the new styles with the old or just
  853. * to overwrite them
  854. * @since 1.0.0
  855. */
  856. function set_regexps_style($key, $style, $preserve_defaults = false) {
  857. if (!$preserve_defaults) {
  858. $this->language_data['STYLES']['REGEXPS'][$key] = $style;
  859. }
  860. else {
  861. $this->language_data['STYLES']['REGEXPS'][$key] .= $style;
  862. }
  863. }
  864. /**
  865. * Turns highlighting on/off for regexps
  866. *
  867. * @param int The key of the regular expression group to turn on or off
  868. * @param boolean Whether to turn highlighting for the regular expression group on or off
  869. * @since 1.0.0
  870. */
  871. function set_regexps_highlighting($key, $flag) {
  872. $this->lexic_permissions['REGEXPS'][$key] = ($flag) ? true : false;
  873. }
  874. /**
  875. * Sets whether a set of keywords are checked for in a case sensitive manner
  876. *
  877. * @param int The key of the keyword group to change the case sensitivity of
  878. * @param boolean Whether to check in a case sensitive manner or not
  879. * @since 1.0.0
  880. */
  881. function set_case_sensitivity($key, $case) {
  882. $this->language_data['CASE_SENSITIVE'][$key] = ($case) ? true : false;
  883. }
  884. /**
  885. * Sets the case that keywords should use when found. Use the constants:
  886. *
  887. * - GESHI_CAPS_NO_CHANGE: leave keywords as-is
  888. * - GESHI_CAPS_UPPER: convert all keywords to uppercase where found
  889. * - GESHI_CAPS_LOWER: convert all keywords to lowercase where found
  890. *
  891. * @param int A constant specifying what to do with matched keywords
  892. * @since 1.0.1
  893. * @todo Error check the passed value
  894. */
  895. function set_case_keywords($case) {
  896. $this->language_data['CASE_KEYWORDS'] = $case;
  897. }
  898. /**
  899. * Sets how many spaces a tab is substituted for
  900. *
  901. * Widths below zero are ignored
  902. *
  903. * @param int The tab width
  904. * @since 1.0.0
  905. */
  906. function set_tab_width($width) {
  907. $this->tab_width = intval($width);
  908. //Check if it fit's the constraints:
  909. if($this->tab_width < 1) {
  910. //Return it to the default
  911. $this->tab_width = 8;
  912. }
  913. }
  914. /**
  915. * Sets whether or not to use tab-stop width specifed by language
  916. *
  917. * @param boolean Whether to use language-specific tab-stop widths
  918. */
  919. function set_use_language_tab_width($use) {
  920. $this->use_language_tab_width = (bool) $use;
  921. }
  922. /**
  923. * Returns the tab width to use, based on the current language and user
  924. * preference
  925. *
  926. * @return int Tab width
  927. */
  928. function get_real_tab_width() {
  929. if (!$this->use_language_tab_width || !isset($this->language_data['TAB_WIDTH'])) {
  930. return $this->tab_width;
  931. } else {
  932. return $this->language_data['TAB_WIDTH'];
  933. }
  934. }
  935. /**
  936. * Enables/disables strict highlighting. Default is off, calling this
  937. * method without parameters will turn it on. See documentation
  938. * for more details on strict mode and where to use it.
  939. *
  940. * @param boolean Whether to enable strict mode or not
  941. * @since 1.0.0
  942. */
  943. function enable_strict_mode($mode = true) {
  944. if (GESHI_MAYBE == $this->language_data['STRICT_MODE_APPLIES']) {
  945. $this->strict_mode = ($mode) ? true : false;
  946. }
  947. }
  948. /**
  949. * Disables all highlighting
  950. *
  951. * @since 1.0.0
  952. * @todo Rewrite with an array traversal
  953. */
  954. function disable_highlighting() {
  955. foreach ($this->lexic_permissions as $key => $value) {
  956. if (is_array($value)) {
  957. foreach ($value as $k => $v) {
  958. $this->lexic_permissions[$key][$k] = false;
  959. }
  960. }
  961. else {
  962. $this->lexic_permissions[$key] = false;
  963. }
  964. }
  965. // Context blocks
  966. $this->enable_important_blocks = false;
  967. }
  968. /**
  969. * Enables all highlighting
  970. *
  971. * @since 1.0.0
  972. * @todo Rewrite with array traversal
  973. */
  974. function enable_highlighting() {
  975. foreach ($this->lexic_permissions as $key => $value) {
  976. if (is_array($value)) {
  977. foreach ($value as $k => $v) {
  978. $this->lexic_permissions[$key][$k] = true;
  979. }
  980. }
  981. else {
  982. $this->lexic_permissions[$key] = true;
  983. }
  984. }
  985. // Context blocks
  986. $this->enable_important_blocks = true;
  987. }
  988. /**
  989. * Given a file extension, this method returns either a valid geshi language
  990. * name, or the empty string if it couldn't be found
  991. *
  992. * @param string The extension to get a language name for
  993. * @param array A lookup array to use instead of the default
  994. * @since 1.0.5
  995. * @todo Re-think about how this method works (maybe make it private and/or make it
  996. * a extension->lang lookup?)
  997. * @todo static?
  998. */
  999. function get_language_name_from_extension( $extension, $lookup = array() ) {
  1000. if ( !$lookup ) {
  1001. $lookup = array(
  1002. 'actionscript' => array('as'),
  1003. 'ada' => array('a', 'ada', 'adb', 'ads'),
  1004. 'apache' => array('conf'),
  1005. 'asm' => array('ash', 'asm'),
  1006. 'asp' => array('asp'),
  1007. 'bash' => array('sh'),
  1008. 'c' => array('c', 'h'),
  1009. 'c_mac' => array('c', 'h'),
  1010. 'caddcl' => array(),
  1011. 'cadlisp' => array(),
  1012. 'cdfg' => array('cdfg'),
  1013. 'cpp' => array('cpp', 'h', 'hpp'),
  1014. 'csharp' => array(),
  1015. 'css' => array('css'),
  1016. 'delphi' => array('dpk', 'dpr'),
  1017. 'html4strict' => array('html', 'htm'),
  1018. 'java' => array('java'),
  1019. 'javascript' => array('js'),
  1020. 'lisp' => array('lisp'),
  1021. 'lua' => array('lua'),
  1022. 'mpasm' => array(),
  1023. 'nsis' => array(),
  1024. 'objc' => array(),
  1025. 'oobas' => array(),
  1026. 'oracle8' => array(),
  1027. 'pascal' => array('pas'),
  1028. 'perl' => array('pl', 'pm'),
  1029. 'php' => array('php', 'php5', 'phtml', 'phps'),
  1030. 'python' => array('py'),
  1031. 'qbasic' => array('bi'),
  1032. 'sas' => array('sas'),
  1033. 'smarty' => array(),
  1034. 'vb' => array('bas'),
  1035. 'vbnet' => array(),
  1036. 'visualfoxpro' => array(),
  1037. 'xml' => array('xml')
  1038. );
  1039. }
  1040. foreach ($lookup as $lang => $extensions) {
  1041. foreach ($extensions as $ext) {
  1042. if ($ext == $extension) {
  1043. return $lang;
  1044. }
  1045. }
  1046. }
  1047. return '';
  1048. }
  1049. /**
  1050. * Given a file name, this method loads its contents in, and attempts
  1051. * to set the language automatically. An optional lookup table can be
  1052. * passed for looking up the language name. If not specified a default
  1053. * table is used
  1054. *
  1055. * The language table is in the form
  1056. * <pre>array(
  1057. * 'lang_name' => array('extension', 'extension', ...),
  1058. * 'lang_name' ...
  1059. * );</pre>
  1060. *
  1061. * @todo Complete rethink of this and above method
  1062. * @since 1.0.5
  1063. */
  1064. function load_from_file($file_name, $lookup = array()) {
  1065. if (is_readable($file_name)) {
  1066. $this->set_source(implode('', file($file_name)));
  1067. $this->set_language($this->get_language_name_from_extension(substr(strrchr($file_name, '.'), 1), $lookup));
  1068. }
  1069. else {
  1070. $this->error = GESHI_ERROR_FILE_NOT_READABLE;
  1071. }
  1072. }
  1073. /**
  1074. * Adds a keyword to a keyword group for highlighting
  1075. *
  1076. * @param int The key of the keyword group to add the keyword to
  1077. * @param string The word to add to the keyword group
  1078. * @since 1.0.0
  1079. */
  1080. function add_keyword($key, $word) {
  1081. $this->language_data['KEYWORDS'][$key][] = $word;
  1082. }
  1083. /**
  1084. * Removes a keyword from a keyword group
  1085. *
  1086. * @param int The key of the keyword group to remove the keyword from
  1087. * @param string The word to remove from the keyword group
  1088. * @since 1.0.0
  1089. */
  1090. function remove_keyword($key, $word) {
  1091. $this->language_data['KEYWORDS'][$key] =
  1092. array_diff($this->language_data['KEYWORDS'][$key], array($word));
  1093. }
  1094. /**
  1095. * Creates a new keyword group
  1096. *
  1097. * @param int The key of the keyword group to create
  1098. * @param string The styles for the keyword group
  1099. * @param boolean Whether the keyword group is case sensitive ornot
  1100. * @param array The words to use for the keyword group
  1101. * @since 1.0.0
  1102. */
  1103. function add_keyword_group($key, $styles, $case_sensitive = true, $words = array()) {
  1104. $words = (array) $words;
  1105. $this->language_data['KEYWORDS'][$key] = $words;
  1106. $this->lexic_permissions['KEYWORDS'][$key] = true;
  1107. $this->language_data['CASE_SENSITIVE'][$key] = $case_sensitive;
  1108. $this->language_data['STYLES']['KEYWORDS'][$key] = $styles;
  1109. }
  1110. /**
  1111. * Removes a keyword group
  1112. *
  1113. * @param int The key of the keyword group to remove
  1114. * @since 1.0.0
  1115. */
  1116. function remove_keyword_group ($key) {
  1117. unset($this->language_data['KEYWORDS'][$key]);
  1118. unset($this->lexic_permissions['KEYWORDS'][$key]);
  1119. unset($this->language_data['CASE_SENSITIVE'][$key]);
  1120. unset($this->language_data['STYLES']['KEYWORDS'][$key]);
  1121. }
  1122. /**
  1123. * Sets the content of the header block
  1124. *
  1125. * @param string The content of the header block
  1126. * @since 1.0.2
  1127. */
  1128. function set_header_content($content) {
  1129. $this->header_content = $content;
  1130. }
  1131. /**
  1132. * Sets the content of the footer block
  1133. *
  1134. * @param string The content of the footer block
  1135. * @since 1.0.2
  1136. */
  1137. function set_footer_content($content) {
  1138. $this->footer_content = $content;
  1139. }
  1140. /**
  1141. * Sets the style for the header content
  1142. *
  1143. * @param string The style for the header content
  1144. * @since 1.0.2
  1145. */
  1146. function set_header_content_style($style) {
  1147. $this->header_content_style = $style;
  1148. }
  1149. /**
  1150. * Sets the style for the footer content
  1151. *
  1152. * @param string The style for the footer content
  1153. * @since 1.0.2
  1154. */
  1155. function set_footer_content_style($style) {
  1156. $this->footer_content_style = $style;
  1157. }
  1158. /**
  1159. * Sets whether to force a surrounding block around
  1160. * the highlighted code or not
  1161. *
  1162. * @param boolean Tells whether to enable or disable this feature
  1163. * @since 1.0.7.20
  1164. */
  1165. function enable_inner_code_block($flag) {
  1166. $this->force_code_block = (bool)$flag;
  1167. }
  1168. /**
  1169. * Sets the base URL to be used for keywords
  1170. *
  1171. * @param int The key of the keyword group to set the URL for
  1172. * @param string The URL to set for the group. If {FNAME} is in
  1173. * the url somewhere, it is replaced by the keyword
  1174. * that the URL is being made for
  1175. * @since 1.0.2
  1176. */
  1177. function set_url_for_keyword_group($group, $url) {
  1178. $this->language_data['URLS'][$group] = $url;
  1179. }
  1180. /**
  1181. * Sets styles for links in code
  1182. *
  1183. * @param int A constant that specifies what state the style is being
  1184. * set for - e.g. :hover or :visited
  1185. * @param string The styles to use for that state
  1186. * @since 1.0.2
  1187. */
  1188. function set_link_styles($type, $styles) {
  1189. $this->link_styles[$type] = $styles;
  1190. }
  1191. /**
  1192. * Sets the target for links in code
  1193. *
  1194. * @param string The target for links in the code, e.g. _blank
  1195. * @since 1.0.3
  1196. */
  1197. function set_link_target($target) {
  1198. if (!$target) {
  1199. $this->link_target = '';
  1200. }
  1201. else {
  1202. $this->link_target = ' target="' . $target . '" ';
  1203. }
  1204. }
  1205. /**
  1206. * Sets styles for important parts of the code
  1207. *
  1208. * @param string The styles to use on important parts of the code
  1209. * @since 1.0.2
  1210. */
  1211. function set_important_styles($styles) {
  1212. $this->important_styles = $styles;
  1213. }
  1214. /**
  1215. * Sets whether context-important blocks are highlighted
  1216. *
  1217. * @todo REMOVE THIS SHIZ FROM GESHI!
  1218. * @deprecated
  1219. */
  1220. function enable_important_blocks($flag) {
  1221. $this->enable_important_blocks = ( $flag ) ? true : false;
  1222. }
  1223. /**
  1224. * Whether CSS IDs should be added to each line
  1225. *
  1226. * @param boolean If true, IDs will be added to each line.
  1227. * @since 1.0.2
  1228. */
  1229. function enable_ids($flag = true) {
  1230. $this->add_ids = ($flag) ? true : false;
  1231. }
  1232. /**
  1233. * Specifies which lines to highlight extra
  1234. *
  1235. * @param mixed An array of line numbers to highlight, or just a line
  1236. * number on its own.
  1237. * @since 1.0.2
  1238. * @todo Some data replication here that could be cut down on
  1239. */
  1240. function highlight_lines_extra($lines) {
  1241. if (is_array($lines)) {
  1242. foreach ($lines as $line) {
  1243. $this->highlight_extra_lines[intval($line)] = intval($line);
  1244. }
  1245. }
  1246. else {
  1247. $this->highlight_extra_lines[intval($lines)] = intval($lines);
  1248. }
  1249. }
  1250. /**
  1251. * Sets the style for extra-highlighted lines
  1252. *
  1253. * @param string The style for extra-highlighted lines
  1254. * @since 1.0.2
  1255. */
  1256. function set_highlight_lines_extra_style($styles) {
  1257. $this->highlight_extra_lines_style = $styles;
  1258. }
  1259. /**
  1260. * Sets the line-ending
  1261. *
  1262. * @param string The new line-ending
  1263. */
  1264. function set_line_ending($line_ending) {
  1265. $this->line_ending = (string)$line_ending;
  1266. }
  1267. /**
  1268. * Sets what number line numbers should start at. Should
  1269. * be a positive integer, and will be converted to one.
  1270. *
  1271. * <b>Warning:</b> Using this method will add the "start"
  1272. * attribute to the &lt;ol&gt; that is used for line numbering.
  1273. * This is <b>not</b> valid XHTML strict, so if that's what you
  1274. * care about then don't use this method. Firefox is getting
  1275. * support for the CSS method of doing this in 1.1 and Opera
  1276. * has support for the CSS method, but (of course) IE doesn't
  1277. * so it's not worth doing it the CSS way yet.
  1278. *
  1279. * @param int The number to start line numbers at
  1280. * @since 1.0.2
  1281. */
  1282. function start_line_numbers_at($number) {
  1283. $this->line_numbers_start = abs(intval($number));
  1284. }
  1285. /**
  1286. * Sets the encoding used for htmlspecialchars(), for international
  1287. * support.
  1288. *
  1289. * NOTE: This is not needed for now because htmlspecialchars() is not
  1290. * being used (it has a security hole in PHP4 that has not been patched).
  1291. * Maybe in a future version it may make a return for speed reasons, but
  1292. * I doubt it.
  1293. *
  1294. * @param string The encoding to use for the source
  1295. * @since 1.0.3
  1296. */
  1297. function set_encoding($encoding) {
  1298. if ($encoding) {
  1299. $this->encoding = $encoding;
  1300. }
  1301. }
  1302. /**
  1303. * Turns linking of keywords on or off.
  1304. *
  1305. * @param boolean If true, links will be added to keywords
  1306. */
  1307. function enable_keyword_links($enable = true) {
  1308. $this->keyword_links = ($enable) ? true : false;
  1309. }
  1310. /**
  1311. * Returns the code in $this->source, highlighted and surrounded by the
  1312. * nessecary HTML.
  1313. *
  1314. * This should only be called ONCE, cos it's SLOW! If you want to highlight
  1315. * the same source multiple times, you're better off doing a whole lot of
  1316. * str_replaces to replace the &lt;span&gt;s
  1317. *
  1318. * @since 1.0.0
  1319. */
  1320. function parse_code () {
  1321. // Start the timer
  1322. $start_time = microtime();
  1323. // Firstly, if there is an error, we won't highlight
  1324. if ($this->error) {
  1325. $result = GeSHi::hsc($this->source);
  1326. // Timing is irrelevant
  1327. $this->set_time($start_time, $start_time);
  1328. return $this->finalise($result);
  1329. }
  1330. // Replace all newlines to a common form.
  1331. $code = str_replace("\r\n", "\n", $this->source);
  1332. $code = str_replace("\r", "\n", $code);
  1333. // Add spaces for regular expression matching and line numbers
  1334. $code = "\n" . $code . "\n";
  1335. // Initialise various stuff
  1336. $length = strlen($code);
  1337. $STRING_OPEN = '';
  1338. $CLOSE_STRING = false;
  1339. $ESCAPE_CHAR_OPEN = false;
  1340. $COMMENT_MATCHED = false;
  1341. // Turn highlighting on if strict mode doesn't apply to this language
  1342. $HIGHLIGHTING_ON = ( !$this->strict_mode ) ? true : '';
  1343. // Whether to highlight inside a block of code
  1344. $HIGHLIGHT_INSIDE_STRICT = false;
  1345. $HARDQUOTE_OPEN = false;
  1346. $STRICTATTRS = '';
  1347. $stuff_to_parse = '';
  1348. $result = '';
  1349. // "Important" selections are handled like multiline comments
  1350. // @todo GET RID OF THIS SHIZ
  1351. if ($this->enable_important_blocks) {
  1352. $this->language_data['COMMENT_MULTI'][GESHI_START_IMPORTANT] = GESHI_END_IMPORTANT;
  1353. }
  1354. if ($this->strict_mode) {
  1355. // Break the source into bits. Each bit will be a portion of the code
  1356. // within script delimiters - for example, HTML between < and >
  1357. $parts = array(0 => array(0 => ''));
  1358. $k = 0;
  1359. for ($i = 0; $i < $length; $i++) {
  1360. $char = substr($code, $i, 1);
  1361. if (!$HIGHLIGHTING_ON) {
  1362. foreach ($this->language_data['SCRIPT_DELIMITERS'] as $key => $delimiters) {
  1363. foreach ($delimiters as $open => $close) {
  1364. // Get the next little bit for this opening string
  1365. $check = substr($code, $i, strlen($open));
  1366. // If it matches...
  1367. if ($check == $open) {
  1368. // We start a new block with the highlightable
  1369. // code in it
  1370. $HIGHLIGHTING_ON = $open;
  1371. $i += strlen($open) - 1;
  1372. $char = $open;
  1373. $parts[++$k][0] = $char;
  1374. // No point going around again...
  1375. break(2);
  1376. }
  1377. }
  1378. }
  1379. }
  1380. else {
  1381. foreach ($this->language_data['SCRIPT_DELIMITERS'] as $key => $delimiters) {
  1382. foreach ($delimiters as $open => $close) {
  1383. if ($open == $HIGHLIGHTING_ON) {
  1384. // Found the closing tag
  1385. break(2);
  1386. }
  1387. }
  1388. }
  1389. // We check code from our current position BACKWARDS. This is so
  1390. // the ending string for highlighting can be included in the block
  1391. $check = substr($code, $i - strlen($close) + 1, strlen($close));
  1392. if ($check == $close) {
  1393. $HIGHLIGHTING_ON = '';
  1394. // Add the string to the rest of the string for this part
  1395. $parts[$k][1] = ( isset($parts[$k][1]) ) ? $parts[$k][1] . $char : $char;
  1396. $parts[++$k][0] = '';
  1397. $char = '';
  1398. }
  1399. }
  1400. $parts[$k][1] = ( isset($parts[$k][1]) ) ? $parts[$k][1] . $char : $char;
  1401. }
  1402. $HIGHLIGHTING_ON = '';
  1403. }
  1404. else {
  1405. // Not strict mode - simply dump the source into
  1406. // the array at index 1 (the first highlightable block)
  1407. $parts = array(
  1408. 1 => array(
  1409. 0 => '',
  1410. 1 => $code
  1411. )
  1412. );
  1413. }
  1414. // Now we go through each part. We know that even-indexed parts are
  1415. // code that shouldn't be highlighted, and odd-indexed parts should
  1416. // be highlighted
  1417. foreach ($parts as $key => $data) {
  1418. $part = $data[1];
  1419. // If this block should be highlighted...
  1420. if ($key % 2) {
  1421. if ($this->strict_mode) {
  1422. // Find the class key for this block of code
  1423. foreach ($this->language_data['SCRIPT_DELIMITERS'] as $script_key => $script_data) {
  1424. foreach ($script_data as $open => $close) {
  1425. if ($data[0] == $open) {
  1426. break(2);
  1427. }
  1428. }
  1429. }
  1430. if ($this->language_data['STYLES']['SCRIPT'][$script_key] != '' &&
  1431. $this->lexic_permissions['SCRIPT']) {
  1432. // Add a span element around the source to
  1433. // highlight the overall source block
  1434. if (!$this->use_classes &&
  1435. $this->language_data['STYLES']['SCRIPT'][$script_key] != '') {
  1436. $attributes = ' style="' . $this->language_data['STYLES']['SCRIPT'][$script_key] . '"';
  1437. }
  1438. else {
  1439. $attributes = ' class="sc' . $script_key . '"';
  1440. }
  1441. $result .= "<span$attributes>";
  1442. $STRICTATTRS = $attributes;
  1443. }
  1444. }
  1445. if (!$this->strict_mode || $this->language_data['HIGHLIGHT_STRICT_BLOCK'][$script_key]) {
  1446. // Now, highlight the code in this block. This code
  1447. // is really the engine of GeSHi (along with the method
  1448. // parse_non_string_part).
  1449. $length = strlen($part);
  1450. for ($i = 0; $i < $length; $i++) {
  1451. // Get the next char
  1452. $char = substr($part, $i, 1);
  1453. $hq = isset($this->language_data['HARDQUOTE']) ? $this->language_data['HARDQUOTE'][0] : false;
  1454. // Is this char the newline and line numbers being used?
  1455. if (($this->line_numbers != GESHI_NO_LINE_NUMBERS
  1456. || count($this->highlight_extra_lines) > 0)
  1457. && $char == "\n") {
  1458. // If so, is there a string open? If there is, we should end it before
  1459. // the newline and begin it again (so when <li>s are put in the source
  1460. // remains XHTML compliant)
  1461. // note to self: This opens up possibility of config files specifying
  1462. // that languages can/cannot have multiline strings???
  1463. if ($STRING_OPEN) {
  1464. if (!$this->use_classes) {
  1465. $attributes = ' style="' . $this->language_data['STYLES']['STRINGS'][0] . '"';
  1466. }
  1467. else {
  1468. $attributes = ' class="st0"';
  1469. }
  1470. $char = '</span>' . $char . "<span$attributes>";
  1471. }
  1472. }
  1473. else if ($char == $STRING_OPEN) {
  1474. // A match of a string delimiter
  1475. if (($this->lexic_permissions['ESCAPE_CHAR'] && $ESCAPE_CHAR_OPEN) ||
  1476. ($this->lexic_permissions['STRINGS'] && !$ESCAPE_CHAR_OPEN)) {
  1477. $char = GeSHi::hsc($char) . '</span>';
  1478. }
  1479. $escape_me = false;
  1480. if ($HARDQUOTE_OPEN) {
  1481. if ($ESCAPE_CHAR_OPEN) {
  1482. $escape_me = true;
  1483. }
  1484. else {
  1485. foreach ($this->language_data['HARDESCAPE'] as $hardesc) {
  1486. if (substr($part, $i, strlen($hardesc)) == $hardesc) {
  1487. $escape_me = true;
  1488. break;
  1489. }
  1490. }
  1491. }
  1492. }
  1493. if (!$ESCAPE_CHAR_OPEN) {
  1494. $STRING_OPEN = '';
  1495. $CLOSE_STRING = true;
  1496. }
  1497. if (!$escape_me) {
  1498. $HARDQUOTE_OPEN = false;
  1499. }
  1500. $ESCAPE_CHAR_OPEN = false;
  1501. }
  1502. else if (in_array($char, $this->language_data['QUOTEMARKS']) &&
  1503. ($STRING_OPEN == '') && $this->lexic_permissions['STRINGS']) {
  1504. // The start of a new string
  1505. $STRING_OPEN = $char;
  1506. if (!$this->use_classes) {
  1507. $attributes = ' style="' . $this->language_data['STYLES']['STRINGS'][0] . '"';
  1508. }
  1509. else {
  1510. $attributes = ' class="st0"';
  1511. }
  1512. $char = "<span$attributes>" . GeSHi::hsc($char);
  1513. $result .= $this->parse_non_string_part( $stuff_to_parse );
  1514. $stuff_to_parse = '';
  1515. }
  1516. else if ($hq && substr($part, $i, strlen($hq)) == $hq &&
  1517. ($STRING_OPEN == '') && $this->lexic_permissions['STRINGS']) {
  1518. // The start of a hard quoted string
  1519. $STRING_OPEN = $this->language_data['HARDQUOTE'][1];
  1520. if (!$this->use_classes) {
  1521. $attributes = ' style="' . $this->language_data['STYLES']['STRINGS'][0] . '"';
  1522. }
  1523. else {
  1524. $attributes = ' class="st0"';
  1525. }
  1526. $char = "<span$attributes>" . $hq;
  1527. $i += strlen($hq) - 1;
  1528. $HARDQUOTE_OPEN = true;
  1529. $result .= $this->parse_non_string_part($stuff_to_parse);
  1530. $stuff_to_parse = '';
  1531. }
  1532. else if ($char == $this->language_data['ESCAPE_CHAR'] && $STRING_OPEN != '') {
  1533. // An escape character
  1534. if (!$ESCAPE_CHAR_OPEN) {
  1535. $ESCAPE_CHAR_OPEN = !$HARDQUOTE_OPEN; // true unless $HARDQUOTE_OPEN
  1536. if ($HARDQUOTE_OPEN) {
  1537. foreach ($this->language_data['HARDESCAPE'] as $hard) {
  1538. if (substr($part, $i, strlen($hard)) == $hard) {
  1539. $ESCAPE_CHAR_OPEN = true;
  1540. break;
  1541. }
  1542. }
  1543. }
  1544. if ($ESCAPE_CHAR_OPEN && $this->lexic_permissions['ESCAPE_CHAR']) {
  1545. if (!$this->use_classes) {
  1546. $attributes = ' style="' . $this->language_data['STYLES']['ESCAPE_CHAR'][0] . '"';
  1547. }
  1548. else {
  1549. $attributes = ' class="es0"';
  1550. }
  1551. $char = "<span$attributes>" . $char;
  1552. if (substr($code, $i + 1, 1) == "\n") {
  1553. // escaping a newline, what's the point in putting the span around
  1554. // the newline? It only causes hassles when inserting line numbers
  1555. $char .= '</span>';
  1556. $ESCAPE_CHAR_OPEN = false;
  1557. }
  1558. }
  1559. }
  1560. else {
  1561. $ESCAPE_CHAR_OPEN = false;
  1562. if ($this->lexic_permissions['ESCAPE_CHAR']) {
  1563. $char .= '</span>';
  1564. }
  1565. }
  1566. }
  1567. else if ($ESCAPE_CHAR_OPEN) {
  1568. if ($this->lexic_permissions['ESCAPE_CHAR']) {
  1569. $char .= '</span>';
  1570. }
  1571. $ESCAPE_CHAR_OPEN = false;
  1572. $test_str = $char;
  1573. }
  1574. else if ($STRING_OPEN == '') {
  1575. // Is this a multiline comment?
  1576. foreach ($this->language_data['COMMENT_MULTI'] as $open => $close) {
  1577. $com_len = strlen($open);
  1578. $test_str = substr( $part, $i, $com_len );
  1579. $test_str_match = $test_str;
  1580. if ($open == $test_str) {
  1581. $COMMENT_MATCHED = true;
  1582. //@todo If remove important do remove here
  1583. if ($this->lexic_permissions['COMMENTS']['MULTI'] ||
  1584. $test_str == GESHI_START_IMPORTANT) {
  1585. if ($test_str != GESHI_START_IMPORTANT) {
  1586. if (!$this->use_classes) {
  1587. $attributes = ' style="' . $this->language_data['STYLES']['COMMENTS']['MULTI'] . '"';
  1588. }
  1589. else {
  1590. $attributes = ' class="coMULTI"';
  1591. }
  1592. $test_str = "<span$attributes>" . GeSHi::hsc($test_str);
  1593. }
  1594. else {
  1595. if (!$this->use_classes) {
  1596. $attributes = ' style="' . $this->important_styles . '"';
  1597. }
  1598. else {
  1599. $attributes = ' class="imp"';
  1600. }
  1601. // We don't include the start of the comment if it's an
  1602. // "important" part
  1603. $test_str = "<span$attributes>";
  1604. }
  1605. }
  1606. else {
  1607. $test_str = GeSHi::hsc($test_str);
  1608. }
  1609. $close_pos = strpos( $part, $close, $i + strlen($close) );
  1610. $oops = false;
  1611. if ($close_pos === false) {
  1612. $close_pos = strlen($part);
  1613. $oops = true;
  1614. }
  1615. else {
  1616. $close_pos -= ($com_len - strlen($close));
  1617. }
  1618. // Short-cut through all the multiline code
  1619. $rest_of_comment = GeSHi::hsc(substr($part, $i + $com_len, $close_pos - $i));
  1620. if (($this->lexic_permissions['COMMENTS']['MULTI'] ||
  1621. $test_str_match == GESHI_START_IMPORTANT) &&
  1622. ($this->line_numbers != GESHI_NO_LINE_NUMBERS ||
  1623. count($this->highlight_extra_lines) > 0)) {
  1624. // strreplace to put close span and open span around multiline newlines
  1625. $test_str .= str_replace(
  1626. "\n", "</span>\n<span$attributes>",
  1627. str_replace("\n ", "\n&nbsp;", $rest_of_comment)
  1628. );
  1629. }
  1630. else {
  1631. $test_str .= $rest_of_comment;
  1632. }
  1633. if ($this->lexic_permissions['COMMENTS']['MULTI'] ||
  1634. $test_str_match == GESHI_START_IMPORTANT) {
  1635. $test_str .= '</span>';
  1636. if ($oops) {
  1637. $test_str .= "\n";
  1638. }
  1639. }
  1640. $i = $close_pos + $com_len - 1;
  1641. // parse the rest
  1642. $result .= $this->parse_non_string_part($stuff_to_parse);
  1643. $stuff_to_parse = '';
  1644. break;
  1645. }
  1646. }
  1647. // If we haven't matched a multiline comment, try single-line comments
  1648. if (!$COMMENT_MATCHED) {
  1649. foreach ($this->language_data['COMMENT_SINGLE'] as $comment_key => $comment_mark) {
  1650. $com_len = strlen($comment_mark);
  1651. $test_str = substr($part, $i, $com_len);
  1652. if ($this->language_data['CASE_SENSITIVE'][GESHI_COMMENTS]) {
  1653. $match = ($comment_mark == $test_str);
  1654. }
  1655. else {
  1656. $match = (strtolower($comment_mark) == strtolower($test_str));
  1657. }
  1658. if ($match) {
  1659. $COMMENT_MATCHED = true;
  1660. if ($this->lexic_permissions['COMMENTS'][$comment_key]) {
  1661. if (!$this->use_classes) {
  1662. $attributes = ' style="' . $this->language_data['STYLES']['COMMENTS'][$comment_key] . '"';
  1663. }
  1664. else {
  1665. $attributes = ' class="co' . $comment_key . '"';
  1666. }
  1667. $test_str = "<span$attributes>" . GeSHi::hsc($this->change_case($test_str));
  1668. }
  1669. else {
  1670. $test_str = GeSHi::hsc($test_str);
  1671. }
  1672. $close_pos = strpos($part, "\n", $i);
  1673. $oops = false;
  1674. if ($close_pos === false) {
  1675. $close_pos = strlen($part);
  1676. $oops = true;
  1677. }
  1678. $test_str .= GeSHi::hsc(substr($part, $i + $com_len, $close_pos - $i - $com_len));
  1679. if ($this->lexic_permissions['COMMENTS'][$comment_key]) {
  1680. $test_str .= "</span>";
  1681. }
  1682. // Take into account that the comment might be the last in the source
  1683. if (!$oops) {
  1684. $test_str .= "\n";
  1685. }
  1686. $i = $close_pos;
  1687. // parse the rest
  1688. $result .= $this->parse_non_string_part($stuff_to_parse);
  1689. $stuff_to_parse = '';
  1690. break;
  1691. }
  1692. }
  1693. }
  1694. }
  1695. else if ($STRING_OPEN != '') {
  1696. // Otherwise, convert it to HTML form
  1697. if (strtolower($this->encoding) == 'utf-8') {
  1698. //only escape <128 (we don't want to break multibyte chars)
  1699. if (ord($char) < 128) {
  1700. $char = GeSHi::hsc($char);
  1701. }
  1702. }
  1703. else {
  1704. //encode everthing
  1705. $char = GeSHi::hsc($char);
  1706. }
  1707. }
  1708. // Where are we adding this char?
  1709. if (!$COMMENT_MATCHED) {
  1710. if (($STRING_OPEN == '') && !$CLOSE_STRING) {
  1711. $stuff_to_parse .= $char;
  1712. }
  1713. else {
  1714. $result .= $char;
  1715. $CLOSE_STRING = false;
  1716. }
  1717. }
  1718. else {
  1719. $result .= $test_str;
  1720. $COMMENT_MATCHED = false;
  1721. }
  1722. }
  1723. // Parse the last bit
  1724. $result .= $this->parse_non_string_part($stuff_to_parse);
  1725. $stuff_to_parse = '';
  1726. }
  1727. else {
  1728. if ($STRICTATTRS != '') {
  1729. $part = str_replace("\n", "</span>\n<span$STRICTATTRS>", GeSHi::hsc($part));
  1730. $STRICTATTRS = '';
  1731. }
  1732. $result .= $part;
  1733. }
  1734. // Close the <span> that surrounds the block
  1735. if ($this->strict_mode && $this->language_data['STYLES']['SCRIPT'][$script_key] != '' &&
  1736. $this->lexic_permissions['SCRIPT']) {
  1737. $result .= '</span>';
  1738. }
  1739. }
  1740. else {
  1741. // Else not a block to highlight
  1742. $result .= GeSHi::hsc($part);
  1743. }
  1744. }
  1745. // Parse the last stuff (redundant?)
  1746. $result .= $this->parse_non_string_part($stuff_to_parse);
  1747. // Lop off the very first and last spaces
  1748. $result = substr($result, 1, -1);
  1749. // Are we still in a string?
  1750. if ($STRING_OPEN) {
  1751. $result .= '</span>';
  1752. }
  1753. // We're finished: stop timing
  1754. $this->set_time($start_time, microtime());
  1755. return $this->finalise($result);
  1756. }
  1757. /**
  1758. * Swaps out spaces and tabs for HTML indentation. Not needed if
  1759. * the code is in a pre block...
  1760. *
  1761. * @param string The source to indent
  1762. * @return string The source with HTML indenting applied
  1763. * @since 1.0.0
  1764. * @access private
  1765. */
  1766. function indent($result) {
  1767. /// Replace tabs with the correct number of spaces
  1768. if (false !== strpos($result, "\t")) {
  1769. $lines = explode("\n", $result);
  1770. $tab_width = $this->get_real_tab_width();
  1771. foreach ($lines as $key => $line) {
  1772. if (false === strpos($line, "\t")) {
  1773. $lines[$key] = $line;
  1774. continue;
  1775. }
  1776. $pos = 0;
  1777. $length = strlen($line);
  1778. $result_line = '';
  1779. $IN_TAG = false;
  1780. for ($i = 0; $i < $length; $i++) {
  1781. $char = substr($line, $i, 1);
  1782. // Simple engine to work out whether we're in a tag.
  1783. // If we are we modify $pos. This is so we ignore HTML
  1784. // in the line and only workout the tab replacement
  1785. // via the actual content of the string
  1786. // This test could be improved to include strings in the
  1787. // html so that < or > would be allowed in user's styles
  1788. // (e.g. quotes: '<' '>'; or similar)
  1789. if ($IN_TAG && '>' == $char) {
  1790. $IN_TAG = false;
  1791. $result_line .= '>';
  1792. ++$pos;
  1793. }
  1794. else if (!$IN_TAG && '<' == $char) {
  1795. $IN_TAG = true;
  1796. $result_line .= '<';
  1797. ++$pos;
  1798. }
  1799. else if (!$IN_TAG && '&' == $char) {
  1800. $substr = substr($line, $i + 3, 4);
  1801. //$substr_5 = substr($line, 5, 1);
  1802. $posi = strpos($substr, ';');
  1803. if (false !== $posi) {
  1804. $pos += $posi + 3;
  1805. }
  1806. $result_line .= '&';
  1807. }
  1808. else if (!$IN_TAG && "\t" == $char) {
  1809. $str = '';
  1810. // OPTIMISE - move $strs out. Make an array:
  1811. // $tabs = array(
  1812. // 1 => '&nbsp;',
  1813. // 2 => '&nbsp; ',
  1814. // 3 => '&nbsp; &nbsp;' etc etc
  1815. // to use instead of building a string every time
  1816. $strs = array(0 => '&nbsp;', 1 => ' ');
  1817. for ($k = 0; $k < ($tab_width - (($i - $pos) % $tab_width)); $k++) $str .= $strs[$k % 2];
  1818. $result_line .= $str;
  1819. $pos += ($i - $pos) % $tab_width + 1;
  1820. if (false === strpos($line, "\t", $i + 1)) {
  1821. $result_line .= substr($line, $i + 1);
  1822. break;
  1823. }
  1824. }
  1825. else if ($IN_TAG) {
  1826. ++$pos;
  1827. $result_line .= $char;
  1828. }
  1829. else {
  1830. $result_line .= $char;
  1831. //++$pos;
  1832. }
  1833. }
  1834. $lines[$key] = $result_line;
  1835. }
  1836. $result = implode("\n", $lines);
  1837. }
  1838. // Other whitespace
  1839. // BenBE: Fix to reduce the number of replacements to be done
  1840. $result = str_replace("\n ", "\n&nbsp;", $result);
  1841. $result = str_replace(' ', ' &nbsp;', $result);
  1842. if ($this->line_numbers == GESHI_NO_LINE_NUMBERS) {
  1843. if ($this->line_ending === null) {
  1844. $result = nl2br($result);
  1845. } else {
  1846. $result = str_replace("\n", $this->line_ending, $result);
  1847. }
  1848. }
  1849. return $result;
  1850. }
  1851. /**
  1852. * Changes the case of a keyword for those languages where a change is asked for
  1853. *
  1854. * @param string The keyword to change the case of
  1855. * @return string The keyword with its case changed
  1856. * @since 1.0.0
  1857. * @access private
  1858. */
  1859. function change_case($instr) {
  1860. if ($this->language_data['CASE_KEYWORDS'] == GESHI_CAPS_UPPER) {
  1861. return strtoupper($instr);
  1862. }
  1863. else if ($this->language_data['CASE_KEYWORDS'] == GESHI_CAPS_LOWER) {
  1864. return strtolower($instr);
  1865. }
  1866. return $instr;
  1867. }
  1868. /**
  1869. * Adds a url to a keyword where needed.
  1870. *
  1871. * @param string The keyword to add the URL HTML to
  1872. * @param int What group the keyword is from
  1873. * @param boolean Whether to get the HTML for the start or end
  1874. * @return The HTML for either the start or end of the HTML &lt;a&gt; tag
  1875. * @since 1.0.2
  1876. * @access private
  1877. * @todo Get rid of ender
  1878. */
  1879. function add_url_to_keyword($keyword, $group, $start_or_end) {
  1880. if (!$this->keyword_links) {
  1881. // Keyword links have been disabled
  1882. return;
  1883. }
  1884. if (isset($this->language_data['URLS'][$group]) &&
  1885. $this->language_data['URLS'][$group] != '' &&
  1886. substr($keyword, 0, 5) != '&lt;/') {
  1887. // There is a base group for this keyword
  1888. if ($start_or_end == 'BEGIN') {
  1889. // HTML workaround... not good form (tm) but should work for 1.0.X
  1890. if ($keyword != '') {
  1891. // Old system: strtolower
  1892. //$keyword = ( $this->language_data['CASE_SENSITIVE'][$group] ) ? $keyword : strtolower($keyword);
  1893. // New system: get keyword from language file to get correct case
  1894. foreach ($this->language_data['KEYWORDS'][$group] as $word) {
  1895. if (strtolower($word) == strtolower($keyword)) {
  1896. break;
  1897. }
  1898. }
  1899. $word = ( substr($word, 0, 4) == '&lt;' ) ? substr($word, 4) : $word;
  1900. $word = ( substr($word, -4) == '&gt;' ) ? substr($word, 0, strlen($word) - 4) : $word;
  1901. if (!$word) return '';
  1902. return '<|UR1|"' .
  1903. str_replace(
  1904. array('{FNAME}', '.'),
  1905. array(GeSHi::hsc($word), '<DOT>'),
  1906. $this->language_data['URLS'][$group]
  1907. ) . '">';
  1908. }
  1909. return '';
  1910. // HTML fix. Again, dirty hackage...
  1911. }
  1912. else if (!($this->language == 'html4strict' && ('&gt;' == $keyword || '&lt;' == $keyword))) {
  1913. return '</a>';
  1914. }
  1915. }
  1916. }
  1917. /**
  1918. * Takes a string that has no strings or comments in it, and highlights
  1919. * stuff like keywords, numbers and methods.
  1920. *
  1921. * @param string The string to parse for keyword, numbers etc.
  1922. * @since 1.0.0
  1923. * @access private
  1924. * @todo BUGGY! Why? Why not build string and return?
  1925. */
  1926. function parse_non_string_part(&$stuff_to_parse) {
  1927. $stuff_to_parse = ' ' . GeSHi::hsc($stuff_to_parse);
  1928. $stuff_to_parse_pregquote = preg_quote($stuff_to_parse, '/');
  1929. $func = '$this->change_case';
  1930. $func2 = '$this->add_url_to_keyword';
  1931. //
  1932. // Regular expressions
  1933. //
  1934. foreach ($this->language_data['REGEXPS'] as $key => $regexp) {
  1935. if ($this->lexic_permissions['REGEXPS'][$key]) {
  1936. if (is_array($regexp)) {
  1937. $stuff_to_parse = preg_replace(
  1938. "/" .
  1939. str_replace('/', '\/', $regexp[GESHI_SEARCH]) .
  1940. "/{$regexp[GESHI_MODIFIERS]}",
  1941. "{$regexp[GESHI_BEFORE]}<|!REG3XP$key!>{$regexp[GESHI_REPLACE]}|>{$regexp[GESHI_AFTER]}",
  1942. $stuff_to_parse
  1943. );
  1944. }
  1945. else {
  1946. $stuff_to_parse = preg_replace( "/(" . str_replace('/', '\/', $regexp) . ")/", "<|!REG3XP$key!>\\1|>", $stuff_to_parse);
  1947. }
  1948. }
  1949. }
  1950. //
  1951. // Highlight numbers. This regexp sucks... anyone with a regexp that WORKS
  1952. // here wins a cookie if they send it to me. At the moment there's two doing
  1953. // almost exactly the same thing, except the second one prevents a number
  1954. // being highlighted twice (eg <span...><span...>5</span></span>)
  1955. // Put /NUM!/ in for the styles, which gets replaced at the end.
  1956. //
  1957. // NEW ONE: Brice Bernard
  1958. //
  1959. if ($this->lexic_permissions['NUMBERS'] && preg_match('#[0-9]#', $stuff_to_parse )) {
  1960. $stuff_to_parse = preg_replace('/([-+]?\\b(?:[0-9]*\\.)?[0-9]+\\b)/', '<|/NUM!/>\\1|>', $stuff_to_parse);
  1961. }
  1962. // Highlight keywords
  1963. // if there is a couple of alpha symbols there *might* be a keyword
  1964. if (preg_match('#[a-zA-Z]{2,}#', $stuff_to_parse)) {
  1965. foreach ($this->language_data['KEYWORDS'] as $k => $keywordset) {
  1966. if ($this->lexic_permissions['KEYWORDS'][$k]) {
  1967. foreach ($keywordset as $keyword) {
  1968. $keyword = preg_quote($keyword, '/');
  1969. //
  1970. // This replacement checks the word is on it's own (except if brackets etc
  1971. // are next to it), then highlights it. We don't put the color=" for the span
  1972. // in just yet - otherwise languages with the keywords "color" or "or" have
  1973. // a fit.
  1974. //
  1975. if (false !== stristr($stuff_to_parse_pregquote, $keyword )) {
  1976. $stuff_to_parse .= ' ';
  1977. // Might make a more unique string for putting the number in soon
  1978. // Basically, we don't put the styles in yet because then the styles themselves will
  1979. // get highlighted if the language has a CSS keyword in it (like CSS, for example ;))
  1980. $styles = "/$k/";
  1981. if ($this->language_data['CASE_SENSITIVE'][$k]) {
  1982. $stuff_to_parse = preg_replace(
  1983. "/([^a-zA-Z0-9\$_\|\#;>|^])($keyword)(?=[^a-zA-Z0-9_<\|%\-&])/e",
  1984. "'\\1' . $func2('\\2', '$k', 'BEGIN') . '<|$styles>' . $func('\\2') . '|>' . $func2('\\2', '$k', 'END')",
  1985. $stuff_to_parse
  1986. );
  1987. }
  1988. else {
  1989. // Change the case of the word.
  1990. // hackage again... must... release... 1.2...
  1991. if ('smarty' == $this->language) { $hackage = '\/'; } else { $hackage = ''; }
  1992. $stuff_to_parse = preg_replace(
  1993. "/([^a-zA-Z0-9\$_\|\#;>$hackage|^])($keyword)(?=[^a-zA-Z0-9_<\|%\-&])/ie",
  1994. "'\\1' . $func2('\\2', '$k', 'BEGIN') . '<|$styles>' . $func('\\2') . '|>' . $func2('\\2', '$k', 'END')",
  1995. $stuff_to_parse
  1996. );
  1997. }
  1998. $stuff_to_parse = substr($stuff_to_parse, 0, strlen($stuff_to_parse) - 1);
  1999. }
  2000. }
  2001. }
  2002. }
  2003. }
  2004. //
  2005. // Now that's all done, replace /[number]/ with the correct styles
  2006. //
  2007. foreach ($this->language_data['KEYWORDS'] as $k => $kws) {
  2008. if (!$this->use_classes) {
  2009. $attributes = ' style="' . $this->language_data['STYLES']['KEYWORDS'][$k] . '"';
  2010. }
  2011. else {
  2012. $attributes = ' class="kw' . $k . '"';
  2013. }
  2014. $stuff_to_parse = str_replace("/$k/", $attributes, $stuff_to_parse);
  2015. }
  2016. // Put number styles in
  2017. if (!$this->use_classes && $this->lexic_permissions['NUMBERS']) {
  2018. $attributes = ' style="' . $this->language_data['STYLES']['NUMBERS'][0] . '"';
  2019. }
  2020. else {
  2021. $attributes = ' class="nu0"';
  2022. }
  2023. $stuff_to_parse = str_replace('/NUM!/', $attributes, $stuff_to_parse);
  2024. //
  2025. // Highlight methods and fields in objects
  2026. //
  2027. if ($this->lexic_permissions['METHODS'] && $this->language_data['OOLANG']) {
  2028. foreach ($this->language_data['OBJECT_SPLITTERS'] as $key => $splitter) {
  2029. if (false !== stristr($stuff_to_parse, $splitter)) {
  2030. if (!$this->use_classes) {
  2031. $attributes = ' style="' . $this->language_data['STYLES']['METHODS'][$key] . '"';
  2032. }
  2033. else {
  2034. $attributes = ' class="me' . $key . '"';
  2035. }
  2036. $stuff_to_parse = preg_replace("/(" . preg_quote($this->language_data['OBJECT_SPLITTERS'][$key], 1) . "[\s]*)([a-zA-Z\*\(][a-zA-Z0-9_\*]*)/", "\\1<|$attributes>\\2|>", $stuff_to_parse);
  2037. }
  2038. }
  2039. }
  2040. //
  2041. // Highlight brackets. Yes, I've tried adding a semi-colon to this list.
  2042. // You try it, and see what happens ;)
  2043. // TODO: Fix lexic permissions not converting entities if shouldn't
  2044. // be highlighting regardless
  2045. //
  2046. if ($this->lexic_permissions['BRACKETS']) {
  2047. $code_entities_match = array('[', ']', '(', ')', '{', '}');
  2048. if (!$this->use_classes) {
  2049. $code_entities_replace = array(
  2050. '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#91;|>',
  2051. '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#93;|>',
  2052. '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#40;|>',
  2053. '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#41;|>',
  2054. '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#123;|>',
  2055. '<| style="' . $this->language_data['STYLES']['BRACKETS'][0] . '">&#125;|>',
  2056. );
  2057. }
  2058. else {
  2059. $code_entities_replace = array(
  2060. '<| class="br0">&#91;|>',
  2061. '<| class="br0">&#93;|>',
  2062. '<| class="br0">&#40;|>',
  2063. '<| class="br0">&#41;|>',
  2064. '<| class="br0">&#123;|>',
  2065. '<| class="br0">&#125;|>',
  2066. );
  2067. }
  2068. $stuff_to_parse = str_replace( $code_entities_match, $code_entities_replace, $stuff_to_parse );
  2069. }
  2070. //
  2071. // Add class/style for regexps
  2072. //
  2073. foreach ($this->language_data['REGEXPS'] as $key => $regexp) {
  2074. if ($this->lexic_permissions['REGEXPS'][$key]) {
  2075. if (!$this->use_classes) {
  2076. $attributes = ' style="' . $this->language_data['STYLES']['REGEXPS'][$key] . '"';
  2077. }
  2078. else {
  2079. if(is_array($this->language_data['REGEXPS'][$key]) &&
  2080. array_key_exists(GESHI_CLASS, $this->language_data['REGEXPS'][$key])) {
  2081. $attributes = ' class="'
  2082. . $this->language_data['REGEXPS'][$key][GESHI_CLASS] . '"';
  2083. }
  2084. else {
  2085. $attributes = ' class="re' . $key . '"';
  2086. }
  2087. }
  2088. $stuff_to_parse = str_replace("!REG3XP$key!", "$attributes", $stuff_to_parse);
  2089. }
  2090. }
  2091. // Replace <DOT> with . for urls
  2092. $stuff_to_parse = str_replace('<DOT>', '.', $stuff_to_parse);
  2093. // Replace <|UR1| with <a href= for urls also
  2094. if (isset($this->link_styles[GESHI_LINK])) {
  2095. if ($this->use_classes) {
  2096. $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target . ' href=', $stuff_to_parse);
  2097. }
  2098. else {
  2099. $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target . ' style="' . $this->link_styles[GESHI_LINK] . '" href=', $stuff_to_parse);
  2100. }
  2101. }
  2102. else {
  2103. $stuff_to_parse = str_replace('<|UR1|', '<a' . $this->link_target . ' href=', $stuff_to_parse);
  2104. }
  2105. //
  2106. // NOW we add the span thingy ;)
  2107. //
  2108. $stuff_to_parse = str_replace('<|', '<span', $stuff_to_parse);
  2109. $stuff_to_parse = str_replace ( '|>', '</span>', $stuff_to_parse );
  2110. return substr($stuff_to_parse, 1);
  2111. }
  2112. /**
  2113. * Sets the time taken to parse the code
  2114. *
  2115. * @param microtime The time when parsing started
  2116. * @param microtime The time when parsing ended
  2117. * @since 1.0.2
  2118. * @access private
  2119. */
  2120. function set_time($start_time, $end_time) {
  2121. $start = explode(' ', $start_time);
  2122. $end = explode(' ', $end_time);
  2123. $this->time = $end[0] + $end[1] - $start[0] - $start[1];
  2124. }
  2125. /**
  2126. * Gets the time taken to parse the code
  2127. *
  2128. * @return double The time taken to parse the code
  2129. * @since 1.0.2
  2130. */
  2131. function get_time() {
  2132. return $this->time;
  2133. }
  2134. /**
  2135. * Gets language information and stores it for later use
  2136. *
  2137. * @access private
  2138. * @todo Needs to load keys for lexic permissions for keywords, regexps etc
  2139. */
  2140. function load_language($file_name) {
  2141. $this->enable_highlighting();
  2142. $language_data = array();
  2143. require $file_name;
  2144. // Perhaps some checking might be added here later to check that
  2145. // $language data is a valid thing but maybe not
  2146. $this->language_data = $language_data;
  2147. // Set strict mode if should be set
  2148. if ($this->language_data['STRICT_MODE_APPLIES'] == GESHI_ALWAYS) {
  2149. $this->strict_mode = true;
  2150. }
  2151. // Set permissions for all lexics to true
  2152. // so they'll be highlighted by default
  2153. foreach ($this->language_data['KEYWORDS'] as $key => $words) {
  2154. $this->lexic_permissions['KEYWORDS'][$key] = true;
  2155. }
  2156. foreach ($this->language_data['COMMENT_SINGLE'] as $key => $comment) {
  2157. $this->lexic_permissions['COMMENTS'][$key] = true;
  2158. }
  2159. foreach ($this->language_data['REGEXPS'] as $key => $regexp) {
  2160. $this->lexic_permissions['REGEXPS'][$key] = true;
  2161. }
  2162. // Set default class for CSS
  2163. $this->overall_class = $this->language;
  2164. }
  2165. /**
  2166. * Takes the parsed code and various options, and creates the HTML
  2167. * surrounding it to make it look nice.
  2168. *
  2169. * @param string The code already parsed
  2170. * @return string The code nicely finalised
  2171. * @since 1.0.0
  2172. * @access private
  2173. */
  2174. function finalise($parsed_code) {
  2175. // Remove end parts of important declarations
  2176. // This is BUGGY!! My fault for bad code: fix coming in 1.2
  2177. // @todo Remove this crap
  2178. if ($this->enable_important_blocks &&
  2179. (strstr($parsed_code, GeSHi::hsc(GESHI_START_IMPORTANT)) === false)) {
  2180. $parsed_code = str_replace(GeSHi::hsc(GESHI_END_IMPORTANT), '', $parsed_code);
  2181. }
  2182. // Add HTML whitespace stuff if we're using the <div> header
  2183. if ($this->header_type != GESHI_HEADER_PRE) {
  2184. $parsed_code = $this->indent($parsed_code);
  2185. }
  2186. // purge some unnecessary stuff
  2187. $parsed_code = preg_replace('#<span[^>]+>(\s*)</span>#', '\\1', $parsed_code);
  2188. $parsed_code = preg_replace('#<div[^>]+>(\s*)</div>#', '\\1', $parsed_code);
  2189. // If we are using IDs for line numbers, there needs to be an overall
  2190. // ID set to prevent collisions.
  2191. if ($this->add_ids && !$this->overall_id) {
  2192. $this->overall_id = 'geshi-' . substr(md5(microtime()), 0, 4);
  2193. }
  2194. // If we're using line numbers, we insert <li>s and appropriate
  2195. // markup to style them (otherwise we don't need to do anything)
  2196. if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
  2197. // If we're using the <pre> header, we shouldn't add newlines because
  2198. // the <pre> will line-break them (and the <li>s already do this for us)
  2199. $ls = ($this->header_type != GESHI_HEADER_PRE) ? "\n" : '';
  2200. // Get code into lines
  2201. $code = explode("\n", $parsed_code);
  2202. // Set vars to defaults for following loop
  2203. $parsed_code = '';
  2204. $i = 0;
  2205. $attrs = array();
  2206. // Foreach line...
  2207. foreach ($code as $line) {
  2208. // Make lines have at least one space in them if they're empty
  2209. // BenBE: Checking emptiness using trim instead of relying on blanks
  2210. if ('' == trim($line)) {
  2211. $line = '&nbsp;';
  2212. }
  2213. // If this is a "special line"...
  2214. if ($this->line_numbers == GESHI_FANCY_LINE_NUMBERS &&
  2215. $i % $this->line_nth_row == ($this->line_nth_row - 1)) {
  2216. // Set the attributes to style the line
  2217. if ($this->use_classes) {
  2218. //$attr = ' class="li2"';
  2219. $attrs['class'][] = 'li2';
  2220. $def_attr = ' class="de2"';
  2221. }
  2222. else {
  2223. //$attr = ' style="' . $this->line_style2 . '"';
  2224. $attrs['style'][] = $this->line_style2;
  2225. // This style "covers up" the special styles set for special lines
  2226. // so that styles applied to special lines don't apply to the actual
  2227. // code on that line
  2228. $def_attr = ' style="' . $this->code_style . '"';
  2229. }
  2230. // Span or div?
  2231. $start = "<div$def_attr>";
  2232. $end = '</div>';
  2233. }
  2234. else {
  2235. if ($this->use_classes) {
  2236. //$attr = ' class="li1"';
  2237. $attrs['class'][] = 'li1';
  2238. $def_attr = ' class="de1"';
  2239. }
  2240. else {
  2241. //$attr = ' style="' . $this->line_style1 . '"';
  2242. $attrs['style'][] = $this->line_style1;
  2243. $def_attr = ' style="' . $this->code_style . '"';
  2244. }
  2245. $start = "<div$def_attr>";
  2246. $end = '</div>';
  2247. }
  2248. ++$i;
  2249. // Are we supposed to use ids? If so, add them
  2250. if ($this->add_ids) {
  2251. $attrs['id'][] = "$this->overall_id-$i";
  2252. }
  2253. if ($this->use_classes && in_array($i, $this->highlight_extra_lines)) {
  2254. $attrs['class'][] = 'ln-xtra';
  2255. }
  2256. if (!$this->use_classes && in_array($i, $this->highlight_extra_lines)) {
  2257. $attrs['style'][] = $this->highlight_extra_lines_style;
  2258. }
  2259. // Add in the line surrounded by appropriate list HTML
  2260. $attr_string = ' ';
  2261. foreach ($attrs as $key => $attr) {
  2262. $attr_string .= $key . '="' . implode(' ', $attr) . '" ';
  2263. }
  2264. $attr_string = substr($attr_string, 0, -1);
  2265. $parsed_code .= "<li$attr_string>$start$line$end</li>$ls";
  2266. $attrs = array();
  2267. }
  2268. }
  2269. else {
  2270. // No line numbers, but still need to handle highlighting lines extra.
  2271. // Have to use divs so the full width of the code is highlighted
  2272. $code = explode("\n", $parsed_code);
  2273. $parsed_code = '';
  2274. $i = 0;
  2275. foreach ($code as $line) {
  2276. // Make lines have at least one space in them if they're empty
  2277. // BenBE: Checking emptiness using trim instead of relying on blanks
  2278. if ('' == trim($line)) {
  2279. $line = '&nbsp;';
  2280. }
  2281. if (in_array(++$i, $this->highlight_extra_lines)) {
  2282. if ($this->use_classes) {
  2283. $parsed_code .= '<div class="ln-xtra">';
  2284. }
  2285. else {
  2286. $parsed_code .= "<div style=\"{$this->highlight_extra_lines_style}\">";
  2287. }
  2288. // Remove \n because it stuffs up <pre> header
  2289. $parsed_code .= $line . "</div>";
  2290. }
  2291. else {
  2292. $parsed_code .= $line . "\n";
  2293. }
  2294. }
  2295. }
  2296. if ($this->header_type == GESHI_HEADER_PRE) {
  2297. // enforce line numbers when using pre
  2298. $parsed_code = str_replace('<li></li>', '<li>&nbsp;</li>', $parsed_code);
  2299. }
  2300. return $this->header() . chop($parsed_code) . $this->footer();
  2301. }
  2302. /**
  2303. * Creates the header for the code block (with correct attributes)
  2304. *
  2305. * @return string The header for the code block
  2306. * @since 1.0.0
  2307. * @access private
  2308. */
  2309. function header() {
  2310. // Get attributes needed
  2311. $attributes = $this->get_attributes();
  2312. $ol_attributes = '';
  2313. if ($this->line_numbers_start != 1) {
  2314. $ol_attributes .= ' start="' . $this->line_numbers_start . '"';
  2315. }
  2316. // Get the header HTML
  2317. $header = $this->format_header_content();
  2318. if (GESHI_HEADER_NONE == $this->header_type) {
  2319. if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
  2320. return "$header<ol$ol_attributes>";
  2321. }
  2322. return $header .
  2323. ($this->force_code_block ? '<div>' : '');
  2324. }
  2325. // Work out what to return and do it
  2326. if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
  2327. if ($this->header_type == GESHI_HEADER_PRE) {
  2328. return "<pre$attributes>$header<ol$ol_attributes>";
  2329. }
  2330. else if ($this->header_type == GESHI_HEADER_DIV) {
  2331. return "<div$attributes>$header<ol$ol_attributes>";
  2332. }
  2333. }
  2334. else {
  2335. if ($this->header_type == GESHI_HEADER_PRE) {
  2336. return "<pre$attributes>$header" .
  2337. ($this->force_code_block ? '<div>' : '');
  2338. }
  2339. else if ($this->header_type == GESHI_HEADER_DIV) {
  2340. return "<div$attributes>$header" .
  2341. ($this->force_code_block ? '<div>' : '');
  2342. }
  2343. }
  2344. }
  2345. /**
  2346. * Returns the header content, formatted for output
  2347. *
  2348. * @return string The header content, formatted for output
  2349. * @since 1.0.2
  2350. * @access private
  2351. */
  2352. function format_header_content() {
  2353. $header = $this->header_content;
  2354. if ($header) {
  2355. if ($this->header_type == GESHI_HEADER_PRE) {
  2356. $header = str_replace("\n", '', $header);
  2357. }
  2358. $header = $this->replace_keywords($header);
  2359. if ($this->use_classes) {
  2360. $attr = ' class="head"';
  2361. }
  2362. else {
  2363. $attr = " style=\"{$this->header_content_style}\"";
  2364. }
  2365. return "<div$attr>$header</div>";
  2366. }
  2367. }
  2368. /**
  2369. * Returns the footer for the code block.
  2370. *
  2371. * @return string The footer for the code block
  2372. * @since 1.0.0
  2373. * @access private
  2374. */
  2375. function footer() {
  2376. $footer_content = $this->format_footer_content();
  2377. if (GESHI_HEADER_NONE == $this->header_type) {
  2378. return ($this->line_numbers != GESHI_NO_LINE_NUMBERS) ? '</ol>' . $footer_content
  2379. : $footer_content;
  2380. }
  2381. if ($this->header_type == GESHI_HEADER_DIV) {
  2382. if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
  2383. return "</ol>$footer_content</div>";
  2384. }
  2385. return ($this->force_code_block ? '</div>' : '') .
  2386. "$footer_content</div>";
  2387. }
  2388. else {
  2389. if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
  2390. return "</ol>$footer_content</pre>";
  2391. }
  2392. return ($this->force_code_block ? '</div>' : '') .
  2393. "$footer_content</pre>";
  2394. }
  2395. }
  2396. /**
  2397. * Returns the footer content, formatted for output
  2398. *
  2399. * @return string The footer content, formatted for output
  2400. * @since 1.0.2
  2401. * @access private
  2402. */
  2403. function format_footer_content() {
  2404. $footer = $this->footer_content;
  2405. if ($footer) {
  2406. if ($this->header_type == GESHI_HEADER_PRE) {
  2407. $footer = str_replace("\n", '', $footer);;
  2408. }
  2409. $footer = $this->replace_keywords($footer);
  2410. if ($this->use_classes) {
  2411. $attr = ' class="foot"';
  2412. }
  2413. else {
  2414. $attr = " style=\"{$this->footer_content_style}\"";
  2415. }
  2416. return "<div$attr>$footer</div>";
  2417. }
  2418. }
  2419. /**
  2420. * Replaces certain keywords in the header and footer with
  2421. * certain configuration values
  2422. *
  2423. * @param string The header or footer content to do replacement on
  2424. * @return string The header or footer with replaced keywords
  2425. * @since 1.0.2
  2426. * @access private
  2427. */
  2428. function replace_keywords($instr) {
  2429. $keywords = $replacements = array();
  2430. $keywords[] = '<TIME>';
  2431. $keywords[] = '{TIME}';
  2432. $replacements[] = $replacements[] = number_format($this->get_time(), 3);
  2433. $keywords[] = '<LANGUAGE>';
  2434. $keywords[] = '{LANGUAGE}';
  2435. $replacements[] = $replacements[] = $this->language;
  2436. $keywords[] = '<VERSION>';
  2437. $keywords[] = '{VERSION}';
  2438. $replacements[] = $replacements[] = GESHI_VERSION;
  2439. return str_replace($keywords, $replacements, $instr);
  2440. }
  2441. /**
  2442. * Gets the CSS attributes for this code
  2443. *
  2444. * @return The CSS attributes for this code
  2445. * @since 1.0.0
  2446. * @access private
  2447. * @todo Document behaviour change - class is outputted regardless of whether we're using classes or not.
  2448. * Same with style
  2449. */
  2450. function get_attributes() {
  2451. $attributes = '';
  2452. if ($this->overall_class != '') {
  2453. $attributes .= " class=\"{$this->overall_class}\"";
  2454. }
  2455. if ($this->overall_id != '') {
  2456. $attributes .= " id=\"{$this->overall_id}\"";
  2457. }
  2458. if ($this->overall_style != '') {
  2459. $attributes .= ' style="' . $this->overall_style . '"';
  2460. }
  2461. return $attributes;
  2462. }
  2463. /**
  2464. * Secure replacement for PHP built-in function htmlspecialchars().
  2465. *
  2466. * See ticket #427 (http://wush.net/trac/wikka/ticket/427) for the rationale
  2467. * for this replacement function.
  2468. *
  2469. * The INTERFACE for this function is almost the same as that for
  2470. * htmlspecialchars(), with the same default for quote style; however, there
  2471. * is no 'charset' parameter. The reason for this is as follows:
  2472. *
  2473. * The PHP docs say:
  2474. * "The third argument charset defines character set used in conversion."
  2475. *
  2476. * I suspect PHP's htmlspecialchars() is working at the byte-value level and
  2477. * thus _needs_ to know (or asssume) a character set because the special
  2478. * characters to be replaced could exist at different code points in
  2479. * different character sets. (If indeed htmlspecialchars() works at
  2480. * byte-value level that goes some way towards explaining why the
  2481. * vulnerability would exist in this function, too, and not only in
  2482. * htmlentities() which certainly is working at byte-value level.)
  2483. *
  2484. * This replacement function however works at character level and should
  2485. * therefore be "immune" to character set differences - so no charset
  2486. * parameter is needed or provided. If a third parameter is passed, it will
  2487. * be silently ignored.
  2488. *
  2489. * In the OUTPUT there is a minor difference in that we use '&#39;' instead
  2490. * of PHP's '&#039;' for a single quote: this provides compatibility with
  2491. * get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES)
  2492. * (see comment by mikiwoz at yahoo dot co dot uk on
  2493. * http://php.net/htmlspecialchars); it also matches the entity definition
  2494. * for XML 1.0
  2495. * (http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_Special_characters).
  2496. * Like PHP we use a numeric character reference instead of '&apos;' for the
  2497. * single quote. For the other special characters we use the named entity
  2498. * references, as PHP is doing.
  2499. *
  2500. * @author {@link http://wikkawiki.org/JavaWoman Marjolein Katsma}
  2501. *
  2502. * @license http://www.gnu.org/copyleft/lgpl.html
  2503. * GNU Lesser General Public License
  2504. * @copyright Copyright 2007, {@link http://wikkawiki.org/CreditsPage
  2505. * Wikka Development Team}
  2506. *
  2507. * @access public
  2508. * @param string $string string to be converted
  2509. * @param integer $quote_style
  2510. * - ENT_COMPAT: escapes &, <, > and double quote (default)
  2511. * - ENT_NOQUOTES: escapes only &, < and >
  2512. * - ENT_QUOTES: escapes &, <, >, double and single quotes
  2513. * @return string converted string
  2514. */
  2515. function hsc($string, $quote_style=ENT_COMPAT) {
  2516. // init
  2517. $aTransSpecchar = array(
  2518. '&' => '&amp;',
  2519. '"' => '&quot;',
  2520. '<' => '&lt;',
  2521. '>' => '&gt;'
  2522. ); // ENT_COMPAT set
  2523. if (ENT_NOQUOTES == $quote_style) // don't convert double quotes
  2524. {
  2525. unset($aTransSpecchar['"']);
  2526. }
  2527. elseif (ENT_QUOTES == $quote_style) // convert single quotes as well
  2528. {
  2529. $aTransSpecchar["'"] = '&#39;'; // (apos) htmlspecialchars() uses '&#039;'
  2530. }
  2531. // return translated string
  2532. return strtr($string,$aTransSpecchar);
  2533. }
  2534. /**
  2535. * Returns a stylesheet for the highlighted code. If $economy mode
  2536. * is true, we only return the stylesheet declarations that matter for
  2537. * this code block instead of the whole thing
  2538. *
  2539. * @param boolean Whether to use economy mode or not
  2540. * @return string A stylesheet built on the data for the current language
  2541. * @since 1.0.0
  2542. */
  2543. function get_stylesheet($economy_mode = true) {
  2544. // If there's an error, chances are that the language file
  2545. // won't have populated the language data file, so we can't
  2546. // risk getting a stylesheet...
  2547. if ($this->error) {
  2548. return '';
  2549. }
  2550. // First, work out what the selector should be. If there's an ID,
  2551. // that should be used, the same for a class. Otherwise, a selector
  2552. // of '' means that these styles will be applied anywhere
  2553. $selector = ($this->overall_id != '') ? "#{$this->overall_id} " : '';
  2554. $selector = ($selector == '' && $this->overall_class != '') ? ".{$this->overall_class} " : $selector;
  2555. // Header of the stylesheet
  2556. if (!$economy_mode) {
  2557. $stylesheet = "/**\n * GeSHi Dynamically Generated Stylesheet\n * --------------------------------------\n * Dynamically generated stylesheet for {$this->language}\n * CSS class: {$this->overall_class}, CSS id: {$this->overall_id}\n * GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter)\n */\n";
  2558. } else {
  2559. $stylesheet = '/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */' . "\n";
  2560. }
  2561. // Set the <ol> to have no effect at all if there are line numbers
  2562. // (<ol>s have margins that should be destroyed so all layout is
  2563. // controlled by the set_overall_style method, which works on the
  2564. // <pre> or <div> container). Additionally, set default styles for lines
  2565. if (!$economy_mode || $this->line_numbers != GESHI_NO_LINE_NUMBERS) {
  2566. //$stylesheet .= "$selector, {$selector}ol, {$selector}ol li {margin: 0;}\n";
  2567. $stylesheet .= "$selector.de1, $selector.de2 {{$this->code_style}}\n";
  2568. }
  2569. // Add overall styles
  2570. if (!$economy_mode || $this->overall_style != '') {
  2571. $stylesheet .= "$selector {{$this->overall_style}}\n";
  2572. }
  2573. // Add styles for links
  2574. foreach ($this->link_styles as $key => $style) {
  2575. if (!$economy_mode || $key == GESHI_LINK && $style != '') {
  2576. $stylesheet .= "{$selector}a:link {{$style}}\n";
  2577. }
  2578. if (!$economy_mode || $key == GESHI_HOVER && $style != '') {
  2579. $stylesheet .= "{$selector}a:hover {{$style}}\n";
  2580. }
  2581. if (!$economy_mode || $key == GESHI_ACTIVE && $style != '') {
  2582. $stylesheet .= "{$selector}a:active {{$style}}\n";
  2583. }
  2584. if (!$economy_mode || $key == GESHI_VISITED && $style != '') {
  2585. $stylesheet .= "{$selector}a:visited {{$style}}\n";
  2586. }
  2587. }
  2588. // Header and footer
  2589. if (!$economy_mode || $this->header_content_style != '') {
  2590. $stylesheet .= "$selector.head {{$this->header_content_style}}\n";
  2591. }
  2592. if (!$economy_mode || $this->footer_content_style != '') {
  2593. $stylesheet .= "$selector.foot {{$this->footer_content_style}}\n";
  2594. }
  2595. // Styles for important stuff
  2596. if (!$economy_mode || $this->important_styles != '') {
  2597. $stylesheet .= "$selector.imp {{$this->important_styles}}\n";
  2598. }
  2599. // Styles for lines being highlighted extra
  2600. if (!$economy_mode || count($this->highlight_extra_lines)) {
  2601. $stylesheet .= "$selector.ln-xtra {{$this->highlight_extra_lines_style}}\n";
  2602. }
  2603. // Simple line number styles
  2604. if (!$economy_mode || ($this->line_numbers != GESHI_NO_LINE_NUMBERS && $this->line_style1 != '')) {
  2605. $stylesheet .= "{$selector}li {{$this->line_style1}}\n";
  2606. }
  2607. // If there is a style set for fancy line numbers, echo it out
  2608. if (!$economy_mode || ($this->line_numbers == GESHI_FANCY_LINE_NUMBERS && $this->line_style2 != '')) {
  2609. $stylesheet .= "{$selector}li.li2 {{$this->line_style2}}\n";
  2610. }
  2611. foreach ($this->language_data['STYLES']['KEYWORDS'] as $group => $styles) {
  2612. if (!$economy_mode || !($economy_mode && (!$this->lexic_permissions['KEYWORDS'][$group] || $styles == ''))) {
  2613. $stylesheet .= "$selector.kw$group {{$styles}}\n";
  2614. }
  2615. }
  2616. foreach ($this->language_data['STYLES']['COMMENTS'] as $group => $styles) {
  2617. if (!$economy_mode || !($economy_mode && $styles == '') &&
  2618. !($economy_mode && !$this->lexic_permissions['COMMENTS'][$group])) {
  2619. $stylesheet .= "$selector.co$group {{$styles}}\n";
  2620. }
  2621. }
  2622. foreach ($this->language_data['STYLES']['ESCAPE_CHAR'] as $group => $styles) {
  2623. if (!$economy_mode || !($economy_mode && $styles == '') && !($economy_mode &&
  2624. !$this->lexic_permissions['ESCAPE_CHAR'])) {
  2625. $stylesheet .= "$selector.es$group {{$styles}}\n";
  2626. }
  2627. }
  2628. foreach ($this->language_data['STYLES']['SYMBOLS'] as $group => $styles) {
  2629. if (!$economy_mode || !($economy_mode && $styles == '') && !($economy_mode &&
  2630. !$this->lexic_permissions['BRACKETS'])) {
  2631. $stylesheet .= "$selector.br$group {{$styles}}\n";
  2632. }
  2633. }
  2634. foreach ($this->language_data['STYLES']['STRINGS'] as $group => $styles) {
  2635. if (!$economy_mode || !($economy_mode && $styles == '') && !($economy_mode &&
  2636. !$this->lexic_permissions['STRINGS'])) {
  2637. $stylesheet .= "$selector.st$group {{$styles}}\n";
  2638. }
  2639. }
  2640. foreach ($this->language_data['STYLES']['NUMBERS'] as $group => $styles) {
  2641. if (!$economy_mode || !($economy_mode && $styles == '') && !($economy_mode &&
  2642. !$this->lexic_permissions['NUMBERS'])) {
  2643. $stylesheet .= "$selector.nu$group {{$styles}}\n";
  2644. }
  2645. }
  2646. foreach ($this->language_data['STYLES']['METHODS'] as $group => $styles) {
  2647. if (!$economy_mode || !($economy_mode && $styles == '') && !($economy_mode &&
  2648. !$this->lexic_permissions['METHODS'])) {
  2649. $stylesheet .= "$selector.me$group {{$styles}}\n";
  2650. }
  2651. }
  2652. foreach ($this->language_data['STYLES']['SCRIPT'] as $group => $styles) {
  2653. if (!$economy_mode || !($economy_mode && $styles == '')) {
  2654. $stylesheet .= "$selector.sc$group {{$styles}}\n";
  2655. }
  2656. }
  2657. foreach ($this->language_data['STYLES']['REGEXPS'] as $group => $styles) {
  2658. if (!$economy_mode || !($economy_mode && $styles == '') && !($economy_mode &&
  2659. !$this->lexic_permissions['REGEXPS'][$group])) {
  2660. if (is_array($this->language_data['REGEXPS'][$group]) &&
  2661. array_key_exists(GESHI_CLASS,
  2662. $this->language_data['REGEXPS'][$group])) {
  2663. $stylesheet .= "$selector.";
  2664. $stylesheet .= $this->language_data['REGEXPS'][$group][GESHI_CLASS];
  2665. $stylesheet .= " {{$styles}}\n";
  2666. }
  2667. else {
  2668. $stylesheet .= "$selector.re$group {{$styles}}\n";
  2669. }
  2670. }
  2671. }
  2672. return $stylesheet;
  2673. }
  2674. } // End Class GeSHi
  2675. if (!function_exists('geshi_highlight')) {
  2676. /**
  2677. * Easy way to highlight stuff. Behaves just like highlight_string
  2678. *
  2679. * @param string The code to highlight
  2680. * @param string The language to highlight the code in
  2681. * @param string The path to the language files. You can leave this blank if you need
  2682. * as from version 1.0.7 the path should be automatically detected
  2683. * @param boolean Whether to return the result or to echo
  2684. * @return string The code highlighted (if $return is true)
  2685. * @since 1.0.2
  2686. */
  2687. function geshi_highlight($string, $language, $path = null, $return = false) {
  2688. $geshi = new GeSHi($string, $language, $path);
  2689. $geshi->set_header_type(GESHI_HEADER_NONE);
  2690. if ($return) {
  2691. return '<code>' . $geshi->parse_code() . '</code>';
  2692. }
  2693. echo '<code>' . $geshi->parse_code() . '</code>';
  2694. if ($geshi->error()) {
  2695. return false;
  2696. }
  2697. return true;
  2698. }
  2699. }
  2700. ?>