PageRenderTime 72ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/app/sitellite/lib/geshi/geshi.php

https://github.com/lux/sitellite
PHP | 4644 lines | 3435 code | 165 blank | 1044 comment | 142 complexity | ece3e2a3cc3fc1a8cb7e38d2fd1e3108 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, GPL-3.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>, Benny Baumann <BenBE@omorphia.de>
  31. * @copyright (C) 2004 - 2007 Nigel McNie, (C) 2007 - 2008 Benny Baumann
  32. * @license http://gnu.org/copyleft/gpl.html GNU GPL
  33. *
  34. */
  35. //
  36. // 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.8.4');
  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. // Define if GeSHi should be paranoid about security
  52. if (!defined('GESHI_SECURITY_PARANOID')) {
  53. /** Tells GeSHi to be paranoid about security settings */
  54. define('GESHI_SECURITY_PARANOID', false);
  55. }
  56. // Line numbers - use with enable_line_numbers()
  57. /** Use no line numbers when building the result */
  58. define('GESHI_NO_LINE_NUMBERS', 0);
  59. /** Use normal line numbers when building the result */
  60. define('GESHI_NORMAL_LINE_NUMBERS', 1);
  61. /** Use fancy line numbers when building the result */
  62. define('GESHI_FANCY_LINE_NUMBERS', 2);
  63. // Container HTML type
  64. /** Use nothing to surround the source */
  65. define('GESHI_HEADER_NONE', 0);
  66. /** Use a "div" to surround the source */
  67. define('GESHI_HEADER_DIV', 1);
  68. /** Use a "pre" to surround the source */
  69. define('GESHI_HEADER_PRE', 2);
  70. /** Use a pre to wrap lines when line numbers are enabled or to wrap the whole code. */
  71. define('GESHI_HEADER_PRE_VALID', 3);
  72. /**
  73. * Use a "table" to surround the source:
  74. *
  75. * <table>
  76. * <thead><tr><td colspan="2">$header</td></tr></thead>
  77. * <tbody><tr><td><pre>$linenumbers</pre></td><td><pre>$code></pre></td></tr></tbody>
  78. * <tfooter><tr><td colspan="2">$footer</td></tr></tfoot>
  79. * </table>
  80. *
  81. * this is essentially only a workaround for Firefox, see sf#1651996 or take a look at
  82. * https://bugzilla.mozilla.org/show_bug.cgi?id=365805
  83. * @note when linenumbers are disabled this is essentially the same as GESHI_HEADER_PRE
  84. */
  85. define('GESHI_HEADER_PRE_TABLE', 4);
  86. // Capatalisation constants
  87. /** Lowercase keywords found */
  88. define('GESHI_CAPS_NO_CHANGE', 0);
  89. /** Uppercase keywords found */
  90. define('GESHI_CAPS_UPPER', 1);
  91. /** Leave keywords found as the case that they are */
  92. define('GESHI_CAPS_LOWER', 2);
  93. // Link style constants
  94. /** Links in the source in the :link state */
  95. define('GESHI_LINK', 0);
  96. /** Links in the source in the :hover state */
  97. define('GESHI_HOVER', 1);
  98. /** Links in the source in the :active state */
  99. define('GESHI_ACTIVE', 2);
  100. /** Links in the source in the :visited state */
  101. define('GESHI_VISITED', 3);
  102. // Important string starter/finisher
  103. // Note that if you change these, they should be as-is: i.e., don't
  104. // write them as if they had been run through htmlentities()
  105. /** The starter for important parts of the source */
  106. define('GESHI_START_IMPORTANT', '<BEGIN GeSHi>');
  107. /** The ender for important parts of the source */
  108. define('GESHI_END_IMPORTANT', '<END GeSHi>');
  109. /**#@+
  110. * @access private
  111. */
  112. // When strict mode applies for a language
  113. /** Strict mode never applies (this is the most common) */
  114. define('GESHI_NEVER', 0);
  115. /** Strict mode *might* apply, and can be enabled or
  116. disabled by {@link GeSHi->enable_strict_mode()} */
  117. define('GESHI_MAYBE', 1);
  118. /** Strict mode always applies */
  119. define('GESHI_ALWAYS', 2);
  120. // Advanced regexp handling constants, used in language files
  121. /** The key of the regex array defining what to search for */
  122. define('GESHI_SEARCH', 0);
  123. /** The key of the regex array defining what bracket group in a
  124. matched search to use as a replacement */
  125. define('GESHI_REPLACE', 1);
  126. /** The key of the regex array defining any modifiers to the regular expression */
  127. define('GESHI_MODIFIERS', 2);
  128. /** The key of the regex array defining what bracket group in a
  129. matched search to put before the replacement */
  130. define('GESHI_BEFORE', 3);
  131. /** The key of the regex array defining what bracket group in a
  132. matched search to put after the replacement */
  133. define('GESHI_AFTER', 4);
  134. /** The key of the regex array defining a custom keyword to use
  135. for this regexp's html tag class */
  136. define('GESHI_CLASS', 5);
  137. /** Used in language files to mark comments */
  138. define('GESHI_COMMENTS', 0);
  139. /** Used to work around missing PHP features **/
  140. define('GESHI_PHP_PRE_433', !(version_compare(PHP_VERSION, '4.3.3') === 1));
  141. /** make sure we can call stripos **/
  142. if (!function_exists('stripos')) {
  143. // the offset param of preg_match is not supported below PHP 4.3.3
  144. if (GESHI_PHP_PRE_433) {
  145. /**
  146. * @ignore
  147. */
  148. function stripos($haystack, $needle, $offset = null) {
  149. if (!is_null($offset)) {
  150. $haystack = substr($haystack, $offset);
  151. }
  152. if (preg_match('/'. preg_quote($needle, '/') . '/', $haystack, $match, PREG_OFFSET_CAPTURE)) {
  153. return $match[0][1];
  154. }
  155. return false;
  156. }
  157. }
  158. else {
  159. /**
  160. * @ignore
  161. */
  162. function stripos($haystack, $needle, $offset = null) {
  163. if (preg_match('/'. preg_quote($needle, '/') . '/', $haystack, $match, PREG_OFFSET_CAPTURE, $offset)) {
  164. return $match[0][1];
  165. }
  166. return false;
  167. }
  168. }
  169. }
  170. /** some old PHP / PCRE subpatterns only support up to xxx subpatterns in
  171. regular expressions. Set this to false if your PCRE lib is up to date
  172. @see GeSHi->optimize_regexp_list()
  173. **/
  174. define('GESHI_MAX_PCRE_SUBPATTERNS', 500);
  175. /** it's also important not to generate too long regular expressions
  176. be generous here... but keep in mind, that when reaching this limit we
  177. still have to close open patterns. 12k should do just fine on a 16k limit.
  178. @see GeSHi->optimize_regexp_list()
  179. **/
  180. define('GESHI_MAX_PCRE_LENGTH', 12288);
  181. //Number format specification
  182. /** Basic number format for integers */
  183. define('GESHI_NUMBER_INT_BASIC', 1); //Default integers \d+
  184. /** Enhanced number format for integers like seen in C */
  185. define('GESHI_NUMBER_INT_CSTYLE', 2); //Default C-Style \d+[lL]?
  186. /** Number format to highlight binary numbers with a suffix "b" */
  187. define('GESHI_NUMBER_BIN_SUFFIX', 16); //[01]+[bB]
  188. /** Number format to highlight binary numbers with a prefix % */
  189. define('GESHI_NUMBER_BIN_PREFIX_PERCENT', 32); //%[01]+
  190. /** Number format to highlight binary numbers with a prefix 0b (C) */
  191. define('GESHI_NUMBER_BIN_PREFIX_0B', 64); //0b[01]+
  192. /** Number format to highlight octal numbers with a leading zero */
  193. define('GESHI_NUMBER_OCT_PREFIX', 256); //0[0-7]+
  194. /** Number format to highlight octal numbers with a suffix of o */
  195. define('GESHI_NUMBER_OCT_SUFFIX', 512); //[0-7]+[oO]
  196. /** Number format to highlight hex numbers with a prefix 0x */
  197. define('GESHI_NUMBER_HEX_PREFIX', 4096); //0x[0-9a-fA-F]+
  198. /** Number format to highlight hex numbers with a suffix of h */
  199. define('GESHI_NUMBER_HEX_SUFFIX', 8192); //[0-9][0-9a-fA-F]*h
  200. /** Number format to highlight floating-point numbers without support for scientific notation */
  201. define('GESHI_NUMBER_FLT_NONSCI', 65536); //\d+\.\d+
  202. /** Number format to highlight floating-point numbers without support for scientific notation */
  203. define('GESHI_NUMBER_FLT_NONSCI_F', 131072); //\d+(\.\d+)?f
  204. /** Number format to highlight floating-point numbers with support for scientific notation (E) and optional leading zero */
  205. define('GESHI_NUMBER_FLT_SCI_SHORT', 262144); //\.\d+e\d+
  206. /** Number format to highlight floating-point numbers with support for scientific notation (E) and required leading digit */
  207. define('GESHI_NUMBER_FLT_SCI_ZERO', 524288); //\d+(\.\d+)?e\d+
  208. //Custom formats are passed by RX array
  209. // Error detection - use these to analyse faults
  210. /** No sourcecode to highlight was specified
  211. * @deprecated
  212. */
  213. define('GESHI_ERROR_NO_INPUT', 1);
  214. /** The language specified does not exist */
  215. define('GESHI_ERROR_NO_SUCH_LANG', 2);
  216. /** GeSHi could not open a file for reading (generally a language file) */
  217. define('GESHI_ERROR_FILE_NOT_READABLE', 3);
  218. /** The header type passed to {@link GeSHi->set_header_type()} was invalid */
  219. define('GESHI_ERROR_INVALID_HEADER_TYPE', 4);
  220. /** The line number type passed to {@link GeSHi->enable_line_numbers()} was invalid */
  221. define('GESHI_ERROR_INVALID_LINE_NUMBER_TYPE', 5);
  222. /**#@-*/
  223. /**
  224. * The GeSHi Class.
  225. *
  226. * Please refer to the documentation for GeSHi 1.0.X that is available
  227. * at http://qbnz.com/highlighter/documentation.php for more information
  228. * about how to use this class.
  229. *
  230. * @package geshi
  231. * @author Nigel McNie <nigel@geshi.org>, Benny Baumann <BenBE@omorphia.de>
  232. * @copyright (C) 2004 - 2007 Nigel McNie, (C) 2007 - 2008 Benny Baumann
  233. */
  234. class GeSHi {
  235. /**#@+
  236. * @access private
  237. */
  238. /**
  239. * The source code to highlight
  240. * @var string
  241. */
  242. var $source = '';
  243. /**
  244. * The language to use when highlighting
  245. * @var string
  246. */
  247. var $language = '';
  248. /**
  249. * The data for the language used
  250. * @var array
  251. */
  252. var $language_data = array();
  253. /**
  254. * The path to the language files
  255. * @var string
  256. */
  257. var $language_path = GESHI_LANG_ROOT;
  258. /**
  259. * The error message associated with an error
  260. * @var string
  261. * @todo check err reporting works
  262. */
  263. var $error = false;
  264. /**
  265. * Possible error messages
  266. * @var array
  267. */
  268. var $error_messages = array(
  269. GESHI_ERROR_NO_SUCH_LANG => 'GeSHi could not find the language {LANGUAGE} (using path {PATH})',
  270. GESHI_ERROR_FILE_NOT_READABLE => 'The file specified for load_from_file was not readable',
  271. GESHI_ERROR_INVALID_HEADER_TYPE => 'The header type specified is invalid',
  272. GESHI_ERROR_INVALID_LINE_NUMBER_TYPE => 'The line number type specified is invalid'
  273. );
  274. /**
  275. * Whether highlighting is strict or not
  276. * @var boolean
  277. */
  278. var $strict_mode = false;
  279. /**
  280. * Whether to use CSS classes in output
  281. * @var boolean
  282. */
  283. var $use_classes = false;
  284. /**
  285. * The type of header to use. Can be one of the following
  286. * values:
  287. *
  288. * - GESHI_HEADER_PRE: Source is outputted in a "pre" HTML element.
  289. * - GESHI_HEADER_DIV: Source is outputted in a "div" HTML element.
  290. * - GESHI_HEADER_NONE: No header is outputted.
  291. *
  292. * @var int
  293. */
  294. var $header_type = GESHI_HEADER_PRE;
  295. /**
  296. * Array of permissions for which lexics should be highlighted
  297. * @var array
  298. */
  299. var $lexic_permissions = array(
  300. 'KEYWORDS' => array(),
  301. 'COMMENTS' => array('MULTI' => true),
  302. 'REGEXPS' => array(),
  303. 'ESCAPE_CHAR' => true,
  304. 'BRACKETS' => true,
  305. 'SYMBOLS' => false,
  306. 'STRINGS' => true,
  307. 'NUMBERS' => true,
  308. 'METHODS' => true,
  309. 'SCRIPT' => true
  310. );
  311. /**
  312. * The time it took to parse the code
  313. * @var double
  314. */
  315. var $time = 0;
  316. /**
  317. * The content of the header block
  318. * @var string
  319. */
  320. var $header_content = '';
  321. /**
  322. * The content of the footer block
  323. * @var string
  324. */
  325. var $footer_content = '';
  326. /**
  327. * The style of the header block
  328. * @var string
  329. */
  330. var $header_content_style = '';
  331. /**
  332. * The style of the footer block
  333. * @var string
  334. */
  335. var $footer_content_style = '';
  336. /**
  337. * Tells if a block around the highlighted source should be forced
  338. * if not using line numbering
  339. * @var boolean
  340. */
  341. var $force_code_block = false;
  342. /**
  343. * The styles for hyperlinks in the code
  344. * @var array
  345. */
  346. var $link_styles = array();
  347. /**
  348. * Whether important blocks should be recognised or not
  349. * @var boolean
  350. * @deprecated
  351. * @todo REMOVE THIS FUNCTIONALITY!
  352. */
  353. var $enable_important_blocks = false;
  354. /**
  355. * Styles for important parts of the code
  356. * @var string
  357. * @deprecated
  358. * @todo As above - rethink the whole idea of important blocks as it is buggy and
  359. * will be hard to implement in 1.2
  360. */
  361. var $important_styles = 'font-weight: bold; color: red;'; // Styles for important parts of the code
  362. /**
  363. * Whether CSS IDs should be added to the code
  364. * @var boolean
  365. */
  366. var $add_ids = false;
  367. /**
  368. * Lines that should be highlighted extra
  369. * @var array
  370. */
  371. var $highlight_extra_lines = array();
  372. /**
  373. * Styles of lines that should be highlighted extra
  374. * @var array
  375. */
  376. var $highlight_extra_lines_styles = array();
  377. /**
  378. * Styles of extra-highlighted lines
  379. * @var string
  380. */
  381. var $highlight_extra_lines_style = 'background-color: #ffc;';
  382. /**
  383. * The line ending
  384. * If null, nl2br() will be used on the result string.
  385. * Otherwise, all instances of \n will be replaced with $line_ending
  386. * @var string
  387. */
  388. var $line_ending = null;
  389. /**
  390. * Number at which line numbers should start at
  391. * @var int
  392. */
  393. var $line_numbers_start = 1;
  394. /**
  395. * The overall style for this code block
  396. * @var string
  397. */
  398. var $overall_style = 'font-family:monospace;';
  399. /**
  400. * The style for the actual code
  401. * @var string
  402. */
  403. var $code_style = 'font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;';
  404. /**
  405. * The overall class for this code block
  406. * @var string
  407. */
  408. var $overall_class = '';
  409. /**
  410. * The overall ID for this code block
  411. * @var string
  412. */
  413. var $overall_id = '';
  414. /**
  415. * Line number styles
  416. * @var string
  417. */
  418. var $line_style1 = 'font-weight: normal; vertical-align:top;';
  419. /**
  420. * Line number styles for fancy lines
  421. * @var string
  422. */
  423. var $line_style2 = 'font-weight: bold; vertical-align:top;';
  424. /**
  425. * Style for line numbers when GESHI_HEADER_PRE_TABLE is chosen
  426. * @var string
  427. */
  428. var $table_linenumber_style = 'width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;';
  429. /**
  430. * Flag for how line numbers are displayed
  431. * @var boolean
  432. */
  433. var $line_numbers = GESHI_NO_LINE_NUMBERS;
  434. /**
  435. * Flag to decide if multi line spans are allowed. Set it to false to make sure
  436. * each tag is closed before and reopened after each linefeed.
  437. * @var boolean
  438. */
  439. var $allow_multiline_span = true;
  440. /**
  441. * The "nth" value for fancy line highlighting
  442. * @var int
  443. */
  444. var $line_nth_row = 0;
  445. /**
  446. * The size of tab stops
  447. * @var int
  448. */
  449. var $tab_width = 8;
  450. /**
  451. * Should we use language-defined tab stop widths?
  452. * @var int
  453. */
  454. var $use_language_tab_width = false;
  455. /**
  456. * Default target for keyword links
  457. * @var string
  458. */
  459. var $link_target = '';
  460. /**
  461. * The encoding to use for entity encoding
  462. * NOTE: Used with Escape Char Sequences to fix UTF-8 handling (cf. SF#2037598)
  463. * @var string
  464. */
  465. var $encoding = 'utf-8';
  466. /**
  467. * Should keywords be linked?
  468. * @var boolean
  469. */
  470. var $keyword_links = true;
  471. /**
  472. * Currently loaded language file
  473. * @var string
  474. * @since 1.0.7.22
  475. */
  476. var $loaded_language = '';
  477. /**
  478. * Wether the caches needed for parsing are built or not
  479. *
  480. * @var bool
  481. * @since 1.0.8
  482. */
  483. var $parse_cache_built = false;
  484. /**
  485. * Work around for Suhosin Patch with disabled /e modifier
  486. *
  487. * Note from suhosins author in config file:
  488. * <blockquote>
  489. * The /e modifier inside <code>preg_replace()</code> allows code execution.
  490. * Often it is the cause for remote code execution exploits. It is wise to
  491. * deactivate this feature and test where in the application it is used.
  492. * The developer using the /e modifier should be made aware that he should
  493. * use <code>preg_replace_callback()</code> instead
  494. * </blockquote>
  495. *
  496. * @var array
  497. * @since 1.0.8
  498. */
  499. var $_kw_replace_group = 0;
  500. var $_rx_key = 0;
  501. /**
  502. * some "callback parameters" for handle_multiline_regexps
  503. *
  504. * @since 1.0.8
  505. * @access private
  506. * @var string
  507. */
  508. var $_hmr_before = '';
  509. var $_hmr_replace = '';
  510. var $_hmr_after = '';
  511. var $_hmr_key = 0;
  512. /**#@-*/
  513. /**
  514. * Creates a new GeSHi object, with source and language
  515. *
  516. * @param string The source code to highlight
  517. * @param string The language to highlight the source with
  518. * @param string The path to the language file directory. <b>This
  519. * is deprecated!</b> I've backported the auto path
  520. * detection from the 1.1.X dev branch, so now it
  521. * should be automatically set correctly. If you have
  522. * renamed the language directory however, you will
  523. * still need to set the path using this parameter or
  524. * {@link GeSHi->set_language_path()}
  525. * @since 1.0.0
  526. */
  527. function GeSHi($source = '', $language = '', $path = '') {
  528. if (!empty($source)) {
  529. $this->set_source($source);
  530. }
  531. if (!empty($language)) {
  532. $this->set_language($language);
  533. }
  534. $this->set_language_path($path);
  535. }
  536. /**
  537. * Returns an error message associated with the last GeSHi operation,
  538. * or false if no error has occured
  539. *
  540. * @return string|false An error message if there has been an error, else false
  541. * @since 1.0.0
  542. */
  543. function error() {
  544. if ($this->error) {
  545. //Put some template variables for debugging here ...
  546. $debug_tpl_vars = array(
  547. '{LANGUAGE}' => $this->language,
  548. '{PATH}' => $this->language_path
  549. );
  550. $msg = str_replace(
  551. array_keys($debug_tpl_vars),
  552. array_values($debug_tpl_vars),
  553. $this->error_messages[$this->error]);
  554. return "<br /><strong>GeSHi Error:</strong> $msg (code {$this->error})<br />";
  555. }
  556. return false;
  557. }
  558. /**
  559. * Gets a human-readable language name (thanks to Simon Patterson
  560. * for the idea :))
  561. *
  562. * @return string The name for the current language
  563. * @since 1.0.2
  564. */
  565. function get_language_name() {
  566. if (GESHI_ERROR_NO_SUCH_LANG == $this->error) {
  567. return $this->language_data['LANG_NAME'] . ' (Unknown Language)';
  568. }
  569. return $this->language_data['LANG_NAME'];
  570. }
  571. /**
  572. * Sets the source code for this object
  573. *
  574. * @param string The source code to highlight
  575. * @since 1.0.0
  576. */
  577. function set_source($source) {
  578. $this->source = $source;
  579. $this->highlight_extra_lines = array();
  580. }
  581. /**
  582. * Sets the language for this object
  583. *
  584. * @note since 1.0.8 this function won't reset language-settings by default anymore!
  585. * if you need this set $force_reset = true
  586. *
  587. * @param string The name of the language to use
  588. * @since 1.0.0
  589. */
  590. function set_language($language, $force_reset = false) {
  591. if ($force_reset) {
  592. $this->loaded_language = false;
  593. }
  594. //Clean up the language name to prevent malicious code injection
  595. $language = preg_replace('#[^a-zA-Z0-9\-_]#', '', $language);
  596. $language = strtolower($language);
  597. //Retreive the full filename
  598. $file_name = $this->language_path . $language . '.php';
  599. if ($file_name == $this->loaded_language) {
  600. // this language is already loaded!
  601. return;
  602. }
  603. $this->language = $language;
  604. $this->error = false;
  605. $this->strict_mode = GESHI_NEVER;
  606. //Check if we can read the desired file
  607. if (!is_readable($file_name)) {
  608. $this->error = GESHI_ERROR_NO_SUCH_LANG;
  609. return;
  610. }
  611. // Load the language for parsing
  612. $this->load_language($file_name);
  613. }
  614. /**
  615. * Sets the path to the directory containing the language files. Note
  616. * that this path is relative to the directory of the script that included
  617. * geshi.php, NOT geshi.php itself.
  618. *
  619. * @param string The path to the language directory
  620. * @since 1.0.0
  621. * @deprecated The path to the language files should now be automatically
  622. * detected, so this method should no longer be needed. The
  623. * 1.1.X branch handles manual setting of the path differently
  624. * so this method will disappear in 1.2.0.
  625. */
  626. function set_language_path($path) {
  627. if(strpos($path,':')) {
  628. //Security Fix to prevent external directories using fopen wrappers.
  629. if(DIRECTORY_SEPARATOR == "\\") {
  630. if(!preg_match('#^[a-zA-Z]:#', $path) || false !== strpos($path, ':', 2)) {
  631. return;
  632. }
  633. } else {
  634. return;
  635. }
  636. }
  637. if(preg_match('#[^/a-zA-Z0-9_\.\-\\\s:]#', $path)) {
  638. //Security Fix to prevent external directories using fopen wrappers.
  639. return;
  640. }
  641. if(GESHI_SECURITY_PARANOID && false !== strpos($path, '/.')) {
  642. //Security Fix to prevent external directories using fopen wrappers.
  643. return;
  644. }
  645. if(GESHI_SECURITY_PARANOID && false !== strpos($path, '..')) {
  646. //Security Fix to prevent external directories using fopen wrappers.
  647. return;
  648. }
  649. if ($path) {
  650. $this->language_path = ('/' == $path[strlen($path) - 1]) ? $path : $path . '/';
  651. $this->set_language($this->language); // otherwise set_language_path has no effect
  652. }
  653. }
  654. /**
  655. * Sets the type of header to be used.
  656. *
  657. * If GESHI_HEADER_DIV is used, the code is surrounded in a "div".This
  658. * means more source code but more control over tab width and line-wrapping.
  659. * GESHI_HEADER_PRE means that a "pre" is used - less source, but less
  660. * control. Default is GESHI_HEADER_PRE.
  661. *
  662. * From 1.0.7.2, you can use GESHI_HEADER_NONE to specify that no header code
  663. * should be outputted.
  664. *
  665. * @param int The type of header to be used
  666. * @since 1.0.0
  667. */
  668. function set_header_type($type) {
  669. //Check if we got a valid header type
  670. if (!in_array($type, array(GESHI_HEADER_NONE, GESHI_HEADER_DIV,
  671. GESHI_HEADER_PRE, GESHI_HEADER_PRE_VALID, GESHI_HEADER_PRE_TABLE))) {
  672. $this->error = GESHI_ERROR_INVALID_HEADER_TYPE;
  673. return;
  674. }
  675. //Set that new header type
  676. $this->header_type = $type;
  677. }
  678. /**
  679. * Sets the styles for the code that will be outputted
  680. * when this object is parsed. The style should be a
  681. * string of valid stylesheet declarations
  682. *
  683. * @param string The overall style for the outputted code block
  684. * @param boolean Whether to merge the styles with the current styles or not
  685. * @since 1.0.0
  686. */
  687. function set_overall_style($style, $preserve_defaults = false) {
  688. if (!$preserve_defaults) {
  689. $this->overall_style = $style;
  690. } else {
  691. $this->overall_style .= $style;
  692. }
  693. }
  694. /**
  695. * Sets the overall classname for this block of code. This
  696. * class can then be used in a stylesheet to style this object's
  697. * output
  698. *
  699. * @param string The class name to use for this block of code
  700. * @since 1.0.0
  701. */
  702. function set_overall_class($class) {
  703. $this->overall_class = $class;
  704. }
  705. /**
  706. * Sets the overall id for this block of code. This id can then
  707. * be used in a stylesheet to style this object's output
  708. *
  709. * @param string The ID to use for this block of code
  710. * @since 1.0.0
  711. */
  712. function set_overall_id($id) {
  713. $this->overall_id = $id;
  714. }
  715. /**
  716. * Sets whether CSS classes should be used to highlight the source. Default
  717. * is off, calling this method with no arguments will turn it on
  718. *
  719. * @param boolean Whether to turn classes on or not
  720. * @since 1.0.0
  721. */
  722. function enable_classes($flag = true) {
  723. $this->use_classes = ($flag) ? true : false;
  724. }
  725. /**
  726. * Sets the style for the actual code. This should be a string
  727. * containing valid stylesheet declarations. If $preserve_defaults is
  728. * true, then styles are merged with the default styles, with the
  729. * user defined styles having priority
  730. *
  731. * Note: Use this method to override any style changes you made to
  732. * the line numbers if you are using line numbers, else the line of
  733. * code will have the same style as the line number! Consult the
  734. * GeSHi documentation for more information about this.
  735. *
  736. * @param string The style to use for actual code
  737. * @param boolean Whether to merge the current styles with the new styles
  738. * @since 1.0.2
  739. */
  740. function set_code_style($style, $preserve_defaults = false) {
  741. if (!$preserve_defaults) {
  742. $this->code_style = $style;
  743. } else {
  744. $this->code_style .= $style;
  745. }
  746. }
  747. /**
  748. * Sets the styles for the line numbers.
  749. *
  750. * @param string The style for the line numbers that are "normal"
  751. * @param string|boolean If a string, this is the style of the line
  752. * numbers that are "fancy", otherwise if boolean then this
  753. * defines whether the normal styles should be merged with the
  754. * new normal styles or not
  755. * @param boolean If set, is the flag for whether to merge the "fancy"
  756. * styles with the current styles or not
  757. * @since 1.0.2
  758. */
  759. function set_line_style($style1, $style2 = '', $preserve_defaults = false) {
  760. //Check if we got 2 or three parameters
  761. if (is_bool($style2)) {
  762. $preserve_defaults = $style2;
  763. $style2 = '';
  764. }
  765. //Actually set the new styles
  766. if (!$preserve_defaults) {
  767. $this->line_style1 = $style1;
  768. $this->line_style2 = $style2;
  769. } else {
  770. $this->line_style1 .= $style1;
  771. $this->line_style2 .= $style2;
  772. }
  773. }
  774. /**
  775. * Sets whether line numbers should be displayed.
  776. *
  777. * Valid values for the first parameter are:
  778. *
  779. * - GESHI_NO_LINE_NUMBERS: Line numbers will not be displayed
  780. * - GESHI_NORMAL_LINE_NUMBERS: Line numbers will be displayed
  781. * - GESHI_FANCY_LINE_NUMBERS: Fancy line numbers will be displayed
  782. *
  783. * For fancy line numbers, the second parameter is used to signal which lines
  784. * are to be fancy. For example, if the value of this parameter is 5 then every
  785. * 5th line will be fancy.
  786. *
  787. * @param int How line numbers should be displayed
  788. * @param int Defines which lines are fancy
  789. * @since 1.0.0
  790. */
  791. function enable_line_numbers($flag, $nth_row = 5) {
  792. if (GESHI_NO_LINE_NUMBERS != $flag && GESHI_NORMAL_LINE_NUMBERS != $flag
  793. && GESHI_FANCY_LINE_NUMBERS != $flag) {
  794. $this->error = GESHI_ERROR_INVALID_LINE_NUMBER_TYPE;
  795. }
  796. $this->line_numbers = $flag;
  797. $this->line_nth_row = $nth_row;
  798. }
  799. /**
  800. * Sets wether spans and other HTML markup generated by GeSHi can
  801. * span over multiple lines or not. Defaults to true to reduce overhead.
  802. * Set it to false if you want to manipulate the output or manually display
  803. * the code in an ordered list.
  804. *
  805. * @param boolean Wether multiline spans are allowed or not
  806. * @since 1.0.7.22
  807. */
  808. function enable_multiline_span($flag) {
  809. $this->allow_multiline_span = (bool) $flag;
  810. }
  811. /**
  812. * Get current setting for multiline spans, see GeSHi->enable_multiline_span().
  813. *
  814. * @see enable_multiline_span
  815. * @return bool
  816. */
  817. function get_multiline_span() {
  818. return $this->allow_multiline_span;
  819. }
  820. /**
  821. * Sets the style for a keyword group. If $preserve_defaults is
  822. * true, then styles are merged with the default styles, with the
  823. * user defined styles having priority
  824. *
  825. * @param int The key of the keyword group to change the styles of
  826. * @param string The style to make the keywords
  827. * @param boolean Whether to merge the new styles with the old or just
  828. * to overwrite them
  829. * @since 1.0.0
  830. */
  831. function set_keyword_group_style($key, $style, $preserve_defaults = false) {
  832. //Set the style for this keyword group
  833. if (!$preserve_defaults) {
  834. $this->language_data['STYLES']['KEYWORDS'][$key] = $style;
  835. } else {
  836. $this->language_data['STYLES']['KEYWORDS'][$key] .= $style;
  837. }
  838. //Update the lexic permissions
  839. if (!isset($this->lexic_permissions['KEYWORDS'][$key])) {
  840. $this->lexic_permissions['KEYWORDS'][$key] = true;
  841. }
  842. }
  843. /**
  844. * Turns highlighting on/off for a keyword group
  845. *
  846. * @param int The key of the keyword group to turn on or off
  847. * @param boolean Whether to turn highlighting for that group on or off
  848. * @since 1.0.0
  849. */
  850. function set_keyword_group_highlighting($key, $flag = true) {
  851. $this->lexic_permissions['KEYWORDS'][$key] = ($flag) ? true : false;
  852. }
  853. /**
  854. * Sets the styles for comment groups. If $preserve_defaults is
  855. * true, then styles are merged with the default styles, with the
  856. * user defined styles having priority
  857. *
  858. * @param int The key of the comment group to change the styles of
  859. * @param string The style to make the comments
  860. * @param boolean Whether to merge the new styles with the old or just
  861. * to overwrite them
  862. * @since 1.0.0
  863. */
  864. function set_comments_style($key, $style, $preserve_defaults = false) {
  865. if (!$preserve_defaults) {
  866. $this->language_data['STYLES']['COMMENTS'][$key] = $style;
  867. } else {
  868. $this->language_data['STYLES']['COMMENTS'][$key] .= $style;
  869. }
  870. }
  871. /**
  872. * Turns highlighting on/off for comment groups
  873. *
  874. * @param int The key of the comment group to turn on or off
  875. * @param boolean Whether to turn highlighting for that group on or off
  876. * @since 1.0.0
  877. */
  878. function set_comments_highlighting($key, $flag = true) {
  879. $this->lexic_permissions['COMMENTS'][$key] = ($flag) ? true : false;
  880. }
  881. /**
  882. * Sets the styles for escaped characters. If $preserve_defaults is
  883. * true, then styles are merged with the default styles, with the
  884. * user defined styles having priority
  885. *
  886. * @param string The style to make the escape characters
  887. * @param boolean Whether to merge the new styles with the old or just
  888. * to overwrite them
  889. * @since 1.0.0
  890. */
  891. function set_escape_characters_style($style, $preserve_defaults = false, $group = 0) {
  892. if (!$preserve_defaults) {
  893. $this->language_data['STYLES']['ESCAPE_CHAR'][$group] = $style;
  894. } else {
  895. $this->language_data['STYLES']['ESCAPE_CHAR'][$group] .= $style;
  896. }
  897. }
  898. /**
  899. * Turns highlighting on/off for escaped characters
  900. *
  901. * @param boolean Whether to turn highlighting for escape characters on or off
  902. * @since 1.0.0
  903. */
  904. function set_escape_characters_highlighting($flag = true) {
  905. $this->lexic_permissions['ESCAPE_CHAR'] = ($flag) ? true : false;
  906. }
  907. /**
  908. * Sets the styles for brackets. If $preserve_defaults is
  909. * true, then styles are merged with the default styles, with the
  910. * user defined styles having priority
  911. *
  912. * This method is DEPRECATED: use set_symbols_style instead.
  913. * This method will be removed in 1.2.X
  914. *
  915. * @param string The style to make the brackets
  916. * @param boolean Whether to merge the new styles with the old or just
  917. * to overwrite them
  918. * @since 1.0.0
  919. * @deprecated In favour of set_symbols_style
  920. */
  921. function set_brackets_style($style, $preserve_defaults = false) {
  922. if (!$preserve_defaults) {
  923. $this->language_data['STYLES']['BRACKETS'][0] = $style;
  924. } else {
  925. $this->language_data['STYLES']['BRACKETS'][0] .= $style;
  926. }
  927. }
  928. /**
  929. * Turns highlighting on/off for brackets
  930. *
  931. * This method is DEPRECATED: use set_symbols_highlighting instead.
  932. * This method will be remove in 1.2.X
  933. *
  934. * @param boolean Whether to turn highlighting for brackets on or off
  935. * @since 1.0.0
  936. * @deprecated In favour of set_symbols_highlighting
  937. */
  938. function set_brackets_highlighting($flag) {
  939. $this->lexic_permissions['BRACKETS'] = ($flag) ? true : false;
  940. }
  941. /**
  942. * Sets the styles for symbols. If $preserve_defaults is
  943. * true, then styles are merged with the default styles, with the
  944. * user defined styles having priority
  945. *
  946. * @param string The style to make the symbols
  947. * @param boolean Whether to merge the new styles with the old or just
  948. * to overwrite them
  949. * @param int Tells the group of symbols for which style should be set.
  950. * @since 1.0.1
  951. */
  952. function set_symbols_style($style, $preserve_defaults = false, $group = 0) {
  953. // Update the style of symbols
  954. if (!$preserve_defaults) {
  955. $this->language_data['STYLES']['SYMBOLS'][$group] = $style;
  956. } else {
  957. $this->language_data['STYLES']['SYMBOLS'][$group] .= $style;
  958. }
  959. // For backward compatibility
  960. if (0 == $group) {
  961. $this->set_brackets_style ($style, $preserve_defaults);
  962. }
  963. }
  964. /**
  965. * Turns highlighting on/off for symbols
  966. *
  967. * @param boolean Whether to turn highlighting for symbols on or off
  968. * @since 1.0.0
  969. */
  970. function set_symbols_highlighting($flag) {
  971. // Update lexic permissions for this symbol group
  972. $this->lexic_permissions['SYMBOLS'] = ($flag) ? true : false;
  973. // For backward compatibility
  974. $this->set_brackets_highlighting ($flag);
  975. }
  976. /**
  977. * Sets the styles for strings. If $preserve_defaults is
  978. * true, then styles are merged with the default styles, with the
  979. * user defined styles having priority
  980. *
  981. * @param string The style to make the escape characters
  982. * @param boolean Whether to merge the new styles with the old or just
  983. * to overwrite them
  984. * @since 1.0.0
  985. */
  986. function set_strings_style($style, $preserve_defaults = false) {
  987. if (!$preserve_defaults) {
  988. $this->language_data['STYLES']['STRINGS'][0] = $style;
  989. } else {
  990. $this->language_data['STYLES']['STRINGS'][0] .= $style;
  991. }
  992. }
  993. /**
  994. * Turns highlighting on/off for strings
  995. *
  996. * @param boolean Whether to turn highlighting for strings on or off
  997. * @since 1.0.0
  998. */
  999. function set_strings_highlighting($flag) {
  1000. $this->lexic_permissions['STRINGS'] = ($flag) ? true : false;
  1001. }
  1002. /**
  1003. * Sets the styles for strict code blocks. If $preserve_defaults is
  1004. * true, then styles are merged with the default styles, with the
  1005. * user defined styles having priority
  1006. *
  1007. * @param string The style to make the script blocks
  1008. * @param boolean Whether to merge the new styles with the old or just
  1009. * to overwrite them
  1010. * @param int Tells the group of script blocks for which style should be set.
  1011. * @since 1.0.8.4
  1012. */
  1013. function set_script_style($style, $preserve_defaults = false, $group = 0) {
  1014. // Update the style of symbols
  1015. if (!$preserve_defaults) {
  1016. $this->language_data['STYLES']['SCRIPT'][$group] = $style;
  1017. } else {
  1018. $this->language_data['STYLES']['SCRIPT'][$group] .= $style;
  1019. }
  1020. }
  1021. /**
  1022. * Sets the styles for numbers. If $preserve_defaults is
  1023. * true, then styles are merged with the default styles, with the
  1024. * user defined styles having priority
  1025. *
  1026. * @param string The style to make the numbers
  1027. * @param boolean Whether to merge the new styles with the old or just
  1028. * to overwrite them
  1029. * @since 1.0.0
  1030. */
  1031. function set_numbers_style($style, $preserve_defaults = false) {
  1032. if (!$preserve_defaults) {
  1033. $this->language_data['STYLES']['NUMBERS'][0] = $style;
  1034. } else {
  1035. $this->language_data['STYLES']['NUMBERS'][0] .= $style;
  1036. }
  1037. }
  1038. /**
  1039. * Turns highlighting on/off for numbers
  1040. *
  1041. * @param boolean Whether to turn highlighting for numbers on or off
  1042. * @since 1.0.0
  1043. */
  1044. function set_numbers_highlighting($flag) {
  1045. $this->lexic_permissions['NUMBERS'] = ($flag) ? true : false;
  1046. }
  1047. /**
  1048. * Sets the styles for methods. $key is a number that references the
  1049. * appropriate "object splitter" - see the language file for the language
  1050. * you are highlighting to get this number. If $preserve_defaults is
  1051. * true, then styles are merged with the default styles, with the
  1052. * user defined styles having priority
  1053. *
  1054. * @param int The key of the object splitter to change the styles of
  1055. * @param string The style to make the methods
  1056. * @param boolean Whether to merge the new styles with the old or just
  1057. * to overwrite them
  1058. * @since 1.0.0
  1059. */
  1060. function set_methods_style($key, $style, $preserve_defaults = false) {
  1061. if (!$preserve_defaults) {
  1062. $this->language_data['STYLES']['METHODS'][$key] = $style;
  1063. } else {
  1064. $this->language_data['STYLES']['METHODS'][$key] .= $style;
  1065. }
  1066. }
  1067. /**
  1068. * Turns highlighting on/off for methods
  1069. *
  1070. * @param boolean Whether to turn highlighting for methods on or off
  1071. * @since 1.0.0
  1072. */
  1073. function set_methods_highlighting($flag) {
  1074. $this->lexic_permissions['METHODS'] = ($flag) ? true : false;
  1075. }
  1076. /**
  1077. * Sets the styles for regexps. If $preserve_defaults is
  1078. * true, then styles are merged with the default styles, with the
  1079. * user defined styles having priority
  1080. *
  1081. * @param string The style to make the regular expression matches
  1082. * @param boolean Whether to merge the new styles with the old or just
  1083. * to overwrite them
  1084. * @since 1.0.0
  1085. */
  1086. function set_regexps_style($key, $style, $preserve_defaults = false) {
  1087. if (!$preserve_defaults) {
  1088. $this->language_data['STYLES']['REGEXPS'][$key] = $style;
  1089. } else {
  1090. $this->language_data['STYLES']['REGEXPS'][$key] .= $style;
  1091. }
  1092. }
  1093. /**
  1094. * Turns highlighting on/off for regexps
  1095. *
  1096. * @param int The key of the regular expression group to turn on or off
  1097. * @param boolean Whether to turn highlighting for the regular expression group on or off
  1098. * @since 1.0.0
  1099. */
  1100. function set_regexps_highlighting($key, $flag) {
  1101. $this->lexic_permissions['REGEXPS'][$key] = ($flag) ? true : false;
  1102. }
  1103. /**
  1104. * Sets whether a set of keywords are checked for in a case sensitive manner
  1105. *
  1106. * @param int The key of the keyword group to change the case sensitivity of
  1107. * @param boolean Whether to check in a case sensitive manner or not
  1108. * @since 1.0.0
  1109. */
  1110. function set_case_sensitivity($key, $case) {
  1111. $this->language_data['CASE_SENSITIVE'][$key] = ($case) ? true : false;
  1112. }
  1113. /**
  1114. * Sets the case that keywords should use when found. Use the constants:
  1115. *
  1116. * - GESHI_CAPS_NO_CHANGE: leave keywords as-is
  1117. * - GESHI_CAPS_UPPER: convert all keywords to uppercase where found
  1118. * - GESHI_CAPS_LOWER: convert all keywords to lowercase where found
  1119. *
  1120. * @param int A constant specifying what to do with matched keywords
  1121. * @since 1.0.1
  1122. */
  1123. function set_case_keywords($case) {
  1124. if (in_array($case, array(
  1125. GESHI_CAPS_NO_CHANGE, GESHI_CAPS_UPPER, GESHI_CAPS_LOWER))) {
  1126. $this->language_data['CASE_KEYWORDS'] = $case;
  1127. }
  1128. }
  1129. /**
  1130. * Sets how many spaces a tab is substituted for
  1131. *
  1132. * Widths below zero are ignored
  1133. *
  1134. * @param int The tab width
  1135. * @since 1.0.0
  1136. */
  1137. function set_tab_width($width) {
  1138. $this->tab_width = intval($width);
  1139. //Check if it fit's the constraints:
  1140. if ($this->tab_width < 1) {
  1141. //Return it to the default
  1142. $this->tab_width = 8;
  1143. }
  1144. }
  1145. /**
  1146. * Sets whether or not to use tab-stop width specifed by language
  1147. *
  1148. * @param boolean Whether to use language-specific tab-stop widths
  1149. * @since 1.0.7.20
  1150. */
  1151. function set_use_language_tab_width($use) {
  1152. $this->use_language_tab_width = (bool) $use;
  1153. }
  1154. /**
  1155. * Returns the tab width to use, based on the current language and user
  1156. * preference
  1157. *
  1158. * @return int Tab width
  1159. * @since 1.0.7.20
  1160. */
  1161. function get_real_tab_width() {
  1162. if (!$this->use_language_tab_width ||
  1163. !isset($this->language_data['TAB_WIDTH'])) {
  1164. return $this->tab_width;
  1165. } else {
  1166. return $this->language_data['TAB_WIDTH'];
  1167. }
  1168. }
  1169. /**
  1170. * Enables/disables strict highlighting. Default is off, calling this
  1171. * method without parameters will turn it on. See documentation
  1172. * for more details on strict mode and where to use it.
  1173. *
  1174. * @param boolean Whether to enable strict mode or not
  1175. * @since 1.0.0
  1176. */
  1177. function enable_strict_mode($mode = true) {
  1178. if (GESHI_MAYBE == $this->language_data['STRICT_MODE_APPLIES']) {
  1179. $this->strict_mode = ($mode) ? GESHI_ALWAYS : GESHI_NEVER;
  1180. }
  1181. }
  1182. /**
  1183. * Disables all highlighting
  1184. *
  1185. * @since 1.0.0
  1186. * @todo Rewrite with array traversal
  1187. * @deprecated In favour of enable_highlighting
  1188. */
  1189. function disable_highlighting() {
  1190. $this->enable_highlighting(false);
  1191. }
  1192. /**
  1193. * Enables all highlighting
  1194. *
  1195. * The optional flag parameter was added in version 1.0.7.21 and can be used
  1196. * to enable (true) or disable (false) all highlighting.
  1197. *
  1198. * @since 1.0.0
  1199. * @param boolean A flag specifying whether to enable or disable all highlighting
  1200. * @todo Rewrite with array traversal
  1201. */
  1202. function enable_highlighting($flag = true) {
  1203. $flag = $flag ? true : false;
  1204. foreach ($this->lexic_permissions as $key => $value) {
  1205. if (is_array($value)) {
  1206. foreach ($value as $k => $v) {
  1207. $this->lexic_permissions[$key][$k] = $flag;
  1208. }
  1209. } else {
  1210. $this->lexic_permissions[$key] = $flag;
  1211. }
  1212. }
  1213. // Context blocks
  1214. $this->enable_important_blocks = $flag;
  1215. }
  1216. /**
  1217. * Given a file extension, this method returns either a valid geshi language
  1218. * name, or the empty string if it couldn't be found
  1219. *
  1220. * @param string The extension to get a language name for
  1221. * @param array A lookup array to use instead of the default one
  1222. * @since 1.0.5
  1223. * @todo Re-think about how this method works (maybe make it private and/or make it
  1224. * a extension->lang lookup?)
  1225. * @todo static?
  1226. */
  1227. function get_language_name_from_extension( $extension, $lookup = array() ) {
  1228. if ( !is_array($lookup) || empty($lookup)) {
  1229. $lookup = array(
  1230. 'abap' => array('abap'),
  1231. 'actionscript' => array('as'),
  1232. 'ada' => array('a', 'ada', 'adb', 'ads'),
  1233. 'apache' => array('conf'),
  1234. 'asm' => array('ash', 'asm', 'inc'),
  1235. 'asp' => array('asp'),
  1236. 'bash' => array('sh'),
  1237. 'bf' => array('bf'),
  1238. 'c' => array('c', 'h'),
  1239. 'c_mac' => array('c', 'h'),
  1240. 'caddcl' => array(),
  1241. 'cadlisp' => array(),
  1242. 'cdfg' => array('cdfg'),
  1243. 'cobol' => array('cbl'),
  1244. 'cpp' => array('cpp', 'hpp', 'C', 'H', 'CPP', 'HPP'),
  1245. 'csharp' => array('cs'),
  1246. 'css' => array('css'),
  1247. 'd' => array('d'),
  1248. 'delphi' => array('dpk', 'dpr', 'pp', 'pas'),
  1249. 'diff' => array('diff', 'patch'),
  1250. 'dos' => array('bat', 'cmd'),
  1251. 'gettext' => array('po', 'pot'),
  1252. 'gml' => array('gml'),
  1253. 'gnuplot' => array('plt'),
  1254. 'groovy' => array('groovy'),
  1255. 'haskell' => array('hs'),
  1256. 'html4strict' => array('html', 'htm'),
  1257. 'ini' => array('ini', 'desktop'),
  1258. 'java' => array('java'),
  1259. 'javascript' => array('js'),
  1260. 'klonec' => array('kl1'),
  1261. 'klonecpp' => array('klx'),
  1262. 'latex' => array('tex'),
  1263. 'lisp' => array('lisp'),
  1264. 'lua' => array('lua'),
  1265. 'matlab' => array('m'),
  1266. 'mpasm' => array(),
  1267. 'mysql' => array('sql'),
  1268. 'nsis' => array(),
  1269. 'objc' => array(),
  1270. 'oobas' => array(),
  1271. 'oracle8' => array(),
  1272. 'oracle10' => array(),
  1273. 'pascal' => array('pas'),
  1274. 'perl' => array('pl', 'pm'),
  1275. 'php' => array('php', 'php5', 'phtml', 'phps'),
  1276. 'povray' => array('pov'),
  1277. 'providex' => array('pvc', 'pvx'),
  1278. 'prolog' => array('pl'),
  1279. 'python' => array('py'),
  1280. 'qbasic' => array('bi'),
  1281. 'reg' => array('reg'),
  1282. 'ruby' => array('rb'),
  1283. 'sas' => array('sas'),
  1284. 'scala' => array('scala'),
  1285. 'scheme' => array('scm'),
  1286. 'scilab' => array('sci'),
  1287. 'smalltalk' => array('st'),
  1288. 'smarty' => array(),
  1289. 'tcl' => array('tcl'),
  1290. 'vb' => array('bas'),
  1291. 'vbnet' => array(),
  1292. 'visualfoxpro' => array(),
  1293. 'whitespace' => array('ws'),
  1294. 'xml' => array('xml', 'svg', 'xrc'),
  1295. 'z80' => array('z80', 'asm', 'inc')
  1296. );
  1297. }
  1298. foreach ($lookup as $lang => $extensions) {
  1299. if (in_array($extension, $extensions)) {
  1300. return $lang;
  1301. }
  1302. }
  1303. return '';
  1304. }
  1305. /**
  1306. * Given a file name, this method loads its contents in, and attempts
  1307. * to set the language automatically. An optional lookup table can be
  1308. * passed for looking up the language name. If not specified a default
  1309. * table is used
  1310. *
  1311. * The language table is in the form
  1312. * <pre>array(
  1313. * 'lang_name' => array('extension', 'extension', ...),
  1314. * 'lang_name' ...
  1315. * );</pre>
  1316. *
  1317. * @param string The filename to load the source from
  1318. * @param array A lookup array to use instead of the default one
  1319. * @todo Complete rethink of this and above method
  1320. * @since 1.0.5
  1321. */
  1322. function load_from_file($file_name, $lookup = array()) {
  1323. if (is_readable($file_name)) {
  1324. $this->set_source(file_get_contents($file_name));
  1325. $this->set_language($this->get_language_name_from_extension(substr(strrchr($file_name, '.'), 1), $lookup));
  1326. } else {
  1327. $this->error = GESHI_ERROR_FILE_NOT_READABLE;
  1328. }
  1329. }
  1330. /**
  1331. * Adds a keyword to a keyword group for highlighting
  1332. *
  1333. * @param int The key of the keyword group to add the keyword to
  1334. * @param string The word to add to the keyword group
  1335. * @since 1.0.0
  1336. */
  1337. function add_keyword($key, $word) {
  1338. if (!in_array($word, $this->language_data['KEYWORDS'][$key])) {
  1339. $this->language_data['KEYWORDS'][$key][] = $word;
  1340. //NEW in 1.0.8 don't recompile the whole optimized regexp, simply append it
  1341. if ($this->parse_cache_built) {
  1342. $subkey = count($this->language_data['CACHED_KEYWORD_LISTS'][$key]) - 1;
  1343. $this->language_data['CACHED_KEYWORD_LISTS'][$key][$subkey] .= '|' . preg_quote($word, '/');
  1344. }
  1345. }
  1346. }
  1347. /**

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