A tumblelog CMS built on AJAX, PHP and MySQL.

geshi.php 124KB

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