PageRenderTime 69ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/geshi/geshi.php

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