PageRenderTime 55ms CodeModel.GetById 15ms 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

Large files files are truncated, but you can click here to view the full file

  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

Large files files are truncated, but you can click here to view the full file