A tumblelog CMS built on AJAX, PHP and MySQL.

geshi.php 121KB

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