Highlighter.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Highlighter base class
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category Text
  15. * @package Text_Highlighter
  16. * @author Andrey Demenev <demenev@gmail.com>
  17. * @copyright 2004-2006 Andrey Demenev
  18. * @license http://www.php.net/license/3_0.txt PHP License
  19. * @version CVS: $Id: Highlighter.php,v 1.1 2007/06/03 02:35:28 ssttoo Exp $
  20. * @link http://pear.php.net/package/Text_Highlighter
  21. */
  22. // {{{ BC constants
  23. // BC trick : define constants related to default
  24. // renderer if needed
  25. if (!defined('HL_NUMBERS_LI')) {
  26. /**#@+
  27. * Constant for use with $options['numbers']
  28. * @see Text_Highlighter_Renderer_Html::_init()
  29. */
  30. /**
  31. * use numbered list
  32. */
  33. define ('HL_NUMBERS_LI' , 1);
  34. /**
  35. * Use 2-column table with line numbers in left column and code in right column.
  36. * Forces $options['tag'] = HL_TAG_PRE
  37. */
  38. define ('HL_NUMBERS_TABLE' , 2);
  39. /**#@-*/
  40. }
  41. // }}}
  42. // {{{ constants
  43. /**
  44. * for our purpose, it is infinity
  45. */
  46. define ('HL_INFINITY', 1000000000);
  47. // }}}
  48. /**
  49. * Text highlighter base class
  50. *
  51. * @author Andrey Demenev <demenev@gmail.com>
  52. * @copyright 2004-2006 Andrey Demenev
  53. * @license http://www.php.net/license/3_0.txt PHP License
  54. * @version Release: 0.7.1
  55. * @link http://pear.php.net/package/Text_Highlighter
  56. */
  57. // {{{ Text_Highlighter
  58. /**
  59. * Text highlighter base class
  60. *
  61. * This class implements all functions necessary for highlighting,
  62. * but it does not contain highlighting rules. Actual highlighting is
  63. * done using a descendent of this class.
  64. *
  65. * One is not supposed to manually create descendent classes.
  66. * Instead, describe highlighting rules in XML format and
  67. * use {@link Text_Highlighter_Generator} to create descendent class.
  68. * Alternatively, an instance of a descendent class can be created
  69. * directly.
  70. *
  71. * Use {@link Text_Highlighter::factory()} to create an
  72. * object for particular language highlighter
  73. *
  74. * Usage example
  75. * <code>
  76. *require_once 'Text/Highlighter.php';
  77. *$hlSQL =& Text_Highlighter::factory('SQL',array('numbers'=>true));
  78. *echo $hlSQL->highlight('SELECT * FROM table a WHERE id = 12');
  79. * </code>
  80. *
  81. * @author Andrey Demenev <demenev@gmail.com>
  82. * @package Text_Highlighter
  83. * @access public
  84. */
  85. class Text_Highlighter
  86. {
  87. // {{{ members
  88. /**
  89. * Syntax highlighting rules.
  90. * Auto-generated classes set this var
  91. *
  92. * @access protected
  93. * @see _init
  94. * @var array
  95. */
  96. var $_syntax;
  97. /**
  98. * Renderer object.
  99. *
  100. * @access private
  101. * @var array
  102. */
  103. var $_renderer;
  104. /**
  105. * Options. Keeped for BC
  106. *
  107. * @access protected
  108. * @var array
  109. */
  110. var $_options = array();
  111. /**
  112. * Conditionds
  113. *
  114. * @access protected
  115. * @var array
  116. */
  117. var $_conditions = array();
  118. /**
  119. * Disabled keywords
  120. *
  121. * @access protected
  122. * @var array
  123. */
  124. var $_disabled = array();
  125. /**
  126. * Language
  127. *
  128. * @access protected
  129. * @var string
  130. */
  131. var $_language = '';
  132. // }}}
  133. // {{{ _checkDefines
  134. /**
  135. * Called by subclssses' constructors to enable/disable
  136. * optional highlighter rules
  137. *
  138. * @param array $defines Conditional defines
  139. *
  140. * @access protected
  141. */
  142. function _checkDefines()
  143. {
  144. if (isset($this->_options['defines'])) {
  145. $defines = $this->_options['defines'];
  146. } else {
  147. $defines = array();
  148. }
  149. foreach ($this->_conditions as $name => $actions) {
  150. foreach($actions as $action) {
  151. $present = in_array($name, $defines);
  152. if (!$action[1]) {
  153. $present = !$present;
  154. }
  155. if ($present) {
  156. unset($this->_disabled[$action[0]]);
  157. } else {
  158. $this->_disabled[$action[0]] = true;
  159. }
  160. }
  161. }
  162. }
  163. // }}}
  164. // {{{ factory
  165. /**
  166. * Create a new Highlighter object for specified language
  167. *
  168. * @param string $lang language, for example "SQL"
  169. * @param array $options Rendering options. This
  170. * parameter is only keeped for BC reasons, use
  171. * {@link Text_Highlighter::setRenderer()} instead
  172. *
  173. * @return mixed a newly created Highlighter object, or
  174. * a PEAR error object on error
  175. *
  176. * @static
  177. * @access public
  178. */
  179. public static function factory($lang, $options = array())
  180. {
  181. $lang = strtoupper($lang);
  182. $langFile = dirname(__FILE__)."/Highlighter/$lang.php";
  183. if (is_file($langFile))
  184. include_once $langFile;
  185. else
  186. return false;
  187. $classname = 'Text_Highlighter_' . $lang;
  188. if (!class_exists($classname))
  189. return false;
  190. return new $classname($options);
  191. }
  192. // }}}
  193. // {{{ setRenderer
  194. /**
  195. * Set renderer object
  196. *
  197. * @param object $renderer Text_Highlighter_Renderer
  198. *
  199. * @access public
  200. */
  201. function setRenderer($renderer)
  202. {
  203. $this->_renderer = $renderer;
  204. }
  205. // }}}
  206. /**
  207. * Helper function to find matching brackets
  208. *
  209. * @access private
  210. */
  211. function _matchingBrackets($str)
  212. {
  213. return strtr($str, '()<>[]{}', ')(><][}{');
  214. }
  215. function _getToken()
  216. {
  217. if (!empty($this->_tokenStack)) {
  218. return array_pop($this->_tokenStack);
  219. }
  220. if ($this->_pos >= $this->_len) {
  221. return NULL;
  222. }
  223. if ($this->_state != -1 && preg_match($this->_endpattern, $this->_str, $m, PREG_OFFSET_CAPTURE, $this->_pos)) {
  224. $endpos = $m[0][1];
  225. $endmatch = $m[0][0];
  226. } else {
  227. $endpos = -1;
  228. }
  229. preg_match ($this->_regs[$this->_state], $this->_str, $m, PREG_OFFSET_CAPTURE, $this->_pos);
  230. $n = 1;
  231. foreach ($this->_counts[$this->_state] as $i=>$count) {
  232. if (!isset($m[$n])) {
  233. break;
  234. }
  235. if ($m[$n][1]>-1 && ($endpos == -1 || $m[$n][1] < $endpos)) {
  236. if ($this->_states[$this->_state][$i] != -1) {
  237. $this->_tokenStack[] = array($this->_delim[$this->_state][$i], $m[$n][0]);
  238. } else {
  239. $inner = $this->_inner[$this->_state][$i];
  240. if (isset($this->_parts[$this->_state][$i])) {
  241. $parts = array();
  242. $partpos = $m[$n][1];
  243. for ($j=1; $j<=$count; $j++) {
  244. if ($m[$j+$n][1] < 0) {
  245. continue;
  246. }
  247. if (isset($this->_parts[$this->_state][$i][$j])) {
  248. if ($m[$j+$n][1] > $partpos) {
  249. array_unshift($parts, array($inner, substr($this->_str, $partpos, $m[$j+$n][1]-$partpos)));
  250. }
  251. array_unshift($parts, array($this->_parts[$this->_state][$i][$j], $m[$j+$n][0]));
  252. }
  253. $partpos = $m[$j+$n][1] + strlen($m[$j+$n][0]);
  254. }
  255. if ($partpos < $m[$n][1] + strlen($m[$n][0])) {
  256. array_unshift($parts, array($inner, substr($this->_str, $partpos, $m[$n][1] - $partpos + strlen($m[$n][0]))));
  257. }
  258. $this->_tokenStack = array_merge($this->_tokenStack, $parts);
  259. } else {
  260. foreach ($this->_keywords[$this->_state][$i] as $g => $re) {
  261. if (isset($this->_disabled[$g])) {
  262. continue;
  263. }
  264. if (preg_match($re, $m[$n][0])) {
  265. $inner = $this->_kwmap[$g];
  266. break;
  267. }
  268. }
  269. $this->_tokenStack[] = array($inner, $m[$n][0]);
  270. }
  271. }
  272. if ($m[$n][1] > $this->_pos) {
  273. $this->_tokenStack[] = array($this->_lastinner, substr($this->_str, $this->_pos, $m[$n][1]-$this->_pos));
  274. }
  275. $this->_pos = $m[$n][1] + strlen($m[$n][0]);
  276. if ($this->_states[$this->_state][$i] != -1) {
  277. $this->_stack[] = array($this->_state, $this->_lastdelim, $this->_lastinner, $this->_endpattern);
  278. $this->_lastinner = $this->_inner[$this->_state][$i];
  279. $this->_lastdelim = $this->_delim[$this->_state][$i];
  280. $l = $this->_state;
  281. $this->_state = $this->_states[$this->_state][$i];
  282. $this->_endpattern = $this->_end[$this->_state];
  283. if ($this->_subst[$l][$i]) {
  284. for ($k=0; $k<=$this->_counts[$l][$i]; $k++) {
  285. if (!isset($m[$i+$k])) {
  286. break;
  287. }
  288. $quoted = preg_quote($m[$n+$k][0], '/');
  289. $this->_endpattern = str_replace('%'.$k.'%', $quoted, $this->_endpattern);
  290. $this->_endpattern = str_replace('%b'.$k.'%', $this->_matchingBrackets($quoted), $this->_endpattern);
  291. }
  292. }
  293. }
  294. return array_pop($this->_tokenStack);
  295. }
  296. $n += $count + 1;
  297. }
  298. if ($endpos > -1) {
  299. $this->_tokenStack[] = array($this->_lastdelim, $endmatch);
  300. if ($endpos > $this->_pos) {
  301. $this->_tokenStack[] = array($this->_lastinner, substr($this->_str, $this->_pos, $endpos-$this->_pos));
  302. }
  303. list($this->_state, $this->_lastdelim, $this->_lastinner, $this->_endpattern) = array_pop($this->_stack);
  304. $this->_pos = $endpos + strlen($endmatch);
  305. return array_pop($this->_tokenStack);
  306. }
  307. $p = $this->_pos;
  308. $this->_pos = HL_INFINITY;
  309. return array($this->_lastinner, substr($this->_str, $p));
  310. }
  311. // {{{ highlight
  312. /**
  313. * Highlights code
  314. *
  315. * @param string $str Code to highlight
  316. * @access public
  317. * @return string Highlighted text
  318. *
  319. */
  320. function highlight($str)
  321. {
  322. if (!($this->_renderer)) {
  323. include_once(dirname(__FILE__).'/Renderer/Html.php');
  324. $this->_renderer = new Text_Highlighter_Renderer_Html($this->_options);
  325. }
  326. $this->_state = -1;
  327. $this->_pos = 0;
  328. $this->_stack = array();
  329. $this->_tokenStack = array();
  330. $this->_lastinner = $this->_defClass;
  331. $this->_lastdelim = $this->_defClass;
  332. $this->_endpattern = '';
  333. $this->_renderer->reset();
  334. $this->_renderer->setCurrentLanguage($this->_language);
  335. $this->_str = $this->_renderer->preprocess($str);
  336. $this->_len = strlen($this->_str);
  337. while ($token = $this->_getToken()) {
  338. $this->_renderer->acceptToken($token[0], $token[1]);
  339. }
  340. $this->_renderer->finalize();
  341. return $this->_renderer->getOutput();
  342. }
  343. // }}}
  344. }
  345. // }}}
  346. /*
  347. * Local variables:
  348. * tab-width: 4
  349. * c-basic-offset: 4
  350. * c-hanging-comment-ender-p: nil
  351. * End:
  352. */
  353. ?>