Guest User

Untitled

a guest
May 27th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.78 KB | None | 0 0
  1. <?php
  2. class HighlightComponent extends Object {
  3.  
  4. var $stx;
  5. var $lang;
  6. var $len;
  7. var $status;
  8. var $stx_path = '';
  9. var $lang_arr = array();
  10. var $ext_arr = array(
  11. 'cpp'=>'cpp',
  12. 'h'=>'cpp',
  13. 'c'=>'cpp',
  14. 'cs'=>'cs',
  15. 'css'=>'css',
  16. 'htm'=>'html',
  17. 'html'=>'html',
  18. 'xhtml'=>'html',
  19. 'xml'=>'xml',
  20. 'xsd'=>'xml',
  21. 'xsl'=>'xml',
  22. 'java'=>'java',
  23. 'class'=>'java',
  24. 'js'=>'js',
  25. 'jsp'=>'jsp',
  26. 'pl'=>'perl',
  27. 'pm'=>'perl',
  28. 'cgi'=>'perl',
  29. 'php'=>'php',
  30. 'vbs'=>'vb'
  31. );
  32. var $color = array(
  33. 'delimiter'=>'#0000CC',
  34. 'reserved'=>'#0000FF',
  35. 'functions'=>'#FF0000',
  36. 'variables1'=>'#008080',
  37. 'variables2'=>'#808000',
  38. 'variables3'=>'#800000',
  39. 'comment'=>'#FF9900',
  40. 'quotation'=>'#FF00FF',
  41. 'words'=>'#000000',
  42. );
  43.  
  44. function STX($lang='')
  45. {
  46. if ($lang != '') $this->lang = strtolower($lang);
  47. foreach ($this->ext_arr as $val) {
  48. if(in_array($val,$this->lang_arr) == false) {
  49. $this->lang_arr[] = $val;
  50. }
  51. }
  52. $this->set_stx();
  53. }
  54.  
  55. function set_stx()
  56. {
  57. if (in_array($this->lang, array('php','jsp'))) {
  58. $this->lang_arr = array($this->lang,'html');
  59. } elseif ($this->lang != '') {
  60. $this->lang_arr = array($this->lang);
  61. }
  62.  
  63. foreach ($this->lang_arr as $lang) {
  64. $root_path = preg_replace("/^(.*\/)(.*)$/","\\1",__FILE__);
  65. //$root_path = realpath('class.stx.php');
  66. $root_path=dirname($root_path);
  67. $stx_file = $root_path.'/stx/'.$this->stx_path.$lang.'.stx';
  68. //echo $stx_file;
  69. if (file_exists($stx_file) == false)continue;
  70.  
  71. $line_arr = file($stx_file);
  72. foreach ($line_arr as $val)
  73. {
  74. $val = trim($val);
  75. if (strlen($val) == 0) continue;
  76. if (in_array($val{0}, array(';', ''))) continue;
  77. if (preg_match("/^#([\w]+)=(.*)$/i", $val, $matches)) {
  78. if (empty($matches)) continue;
  79. if ($matches[1] == 'DELIMITER') {
  80. $len = strlen($matches[2]);
  81. for ($i = 0; $i < $len; $i++) $this->stx[$lang]['DELIMITER'][] = $matches[2]{$i};
  82. } elseif ($matches[1] == 'KEYWORD') {
  83. if (isset($flag)) $flag = 2;
  84. else $flag = 1;
  85. } else {
  86. $this->stx[$lang][$matches[1]] = $matches[2];
  87. }
  88. } else {
  89. if ($flag == 1) $this->stx[$lang]['RESERVED'][] = strtolower($val);
  90. else $this->stx[$lang]['FUNCTIONS'][] = strtolower($val);
  91. }
  92. }
  93. if (empty($this->stx[$lang]['SCRIPT_BEGIN'])) $this->stx[$lang]['SCRIPT_BEGIN'] = '';
  94. if (empty($this->stx[$lang]['SCRIPT_END'])) $this->stx[$lang]['SCRIPT_END'] = '';
  95. if (empty($this->stx[$lang]['DELIMITER'])) $this->stx[$lang]['DELIMITER'] = '';
  96. if (empty($this->stx[$lang]['COMMENTON'])) $this->stx[$lang]['COMMENTON'] = '';
  97. if (empty($this->stx[$lang]['COMMENTOFF'])) $this->stx[$lang]['COMMENTOFF'] = '';
  98. if (empty($this->stx[$lang]['LINECOMMENT'])) $this->stx[$lang]['LINECOMMENT'] = '';
  99. if (empty($this->stx[$lang]['LINECOMMENT2'])) $this->stx[$lang]['LINECOMMENT2'] = '';
  100. if (empty($this->stx[$lang]['QUOTATION1'])) $this->stx[$lang]['QUOTATION1'] = "'";
  101. if (empty($this->stx[$lang]['QUOTATION2'])) $this->stx[$lang]['QUOTATION2'] = '"';
  102. if (empty($this->stx[$lang]['ESCAPE'])) $this->stx[$lang]['ESCAPE'] = '';
  103. if (empty($this->stx[$lang]['PREFIX3'])) $this->stx[$lang]['PREFIX3'] = '';
  104. if (empty($this->stx[$lang]['PREFIX4'])) $this->stx[$lang]['PREFIX4'] = '';
  105. if (empty($this->stx[$lang]['PREFIX5'])) $this->stx[$lang]['PREFIX5'] = '';
  106. if (empty($this->stx[$lang]['RESERVED'])) $this->stx[$lang]['RESERVED'] = '';
  107. if (empty($this->stx[$lang]['FUNCTIONS'])) $this->stx[$lang]['FUNCTIONS'] = '';
  108. unset($flag);
  109. }
  110. return true;
  111. }
  112.  
  113. function get_flag(&$str,&$i,$word,$flag)
  114. {
  115. $lang = $this->lang;
  116.  
  117. $char = $str{$i};
  118. if ($i > 1) $char0 = $str{$i-2};
  119. else $char0 = '';
  120. if ($i > 0) $char1 = $str{$i-1};
  121. else $char1 = '';
  122. if ($i < $this->len - 1) $char2 = $str{$i+1};
  123. else $char2 = '';
  124. if ($i < $this->len - 2) $char3 = $str{$i+2};
  125. else $char3 = '';
  126. if ($i < $this->len - 3) $char4 = $str{$i+3};
  127. else $char4 = '';
  128. if ($lang == 'perl') $this->stx[$lang]['LINECOMMENT2'] = $this->stx[$lang]['LINECOMMENT'];
  129.  
  130. if ($this->status == 'SCRIPT' || $char.$char2 == $this->stx[$lang]['SCRIPT_BEGIN'])
  131. {
  132. if ($this->status == 'SCRIPT' && $char1.$char == $this->stx[$lang]['SCRIPT_END']) {
  133. $this->status = 'SCRIPT_END';
  134. } else {
  135. $this->status = 'SCRIPT';
  136. }
  137. }
  138. if ($this->status == 'SCRIPT_END' && ($flag == 'HTML_COMMENT' || $char.$char2.$char3.$char4 == '<!--')) {
  139. if ($flag == 'HTML_COMMENT' && $char0.$char1.$char == '-->') {
  140. $flag = 'HTML_COMMENT_END';
  141. } elseif (in_array($flag, array('QUOTATION1','QUOTATION2')) == false) {
  142. $flag = 'HTML_COMMENT';
  143. }
  144. }
  145. elseif ($this->status == 'SCRIPT' && ($flag == 'MULTI_COMMENT' || $char.$char2 == $this->stx[$lang]['COMMENTON']))
  146. {
  147. if ($flag == 'MULTI_COMMENT' && $char1.$char == $this->stx[$lang]['COMMENTOFF']) {
  148. $flag = 'MULTI_COMMENT_END';
  149. } elseif (in_array($flag, array('LINE_COMMENT','LINE_COMMENT2','QUOTATION1','QUOTATION2')) == false) {
  150. $flag = 'MULTI_COMMENT';
  151. }
  152. }
  153. elseif ($this->status == 'SCRIPT' && ($flag == 'LINE_COMMENT' || $char.$char2 == $this->stx[$lang]['LINECOMMENT']))
  154. {
  155. if ($flag == 'LINE_COMMENT' && $char2 == "\n"){
  156. $flag = 'LINE_COMMENT_END';
  157. } elseif (in_array($flag, array('MULTI_COMMENT','LINE_COMMENT2','QUOTATION1','QUOTATION2')) == false) {
  158. $flag = 'LINE_COMMENT';
  159. }
  160. }
  161. elseif ($this->status == 'SCRIPT' && ($flag == 'LINE_COMMENT2' || $char == $this->stx[$lang]['LINECOMMENT2']))
  162. {
  163. if ($flag == 'LINE_COMMENT2' && $char2 == "\n"){
  164. $flag = 'LINE_COMMENT2_END';
  165. } elseif (in_array($flag, array('MULTI_COMMENT','LINE_COMMENT','QUOTATION1','QUOTATION2')) == false) {
  166. $flag = 'LINE_COMMENT2';
  167. }
  168. }
  169. elseif ($flag == 'QUOTATION1' || $char == $this->stx[$lang]['QUOTATION1'])
  170. {
  171. $word .= $char;
  172. if ($flag == 'QUOTATION1' && $char == $this->stx[$lang]['QUOTATION1']) {
  173. if (preg_match("/([\\".$this->stx[$lang]['ESCAPE']."]+)".$this->stx[$lang]['QUOTATION1']."$/",$word,$matches)) {
  174. if (strlen($matches[0])%2) {
  175. $flag = 'QUOTATION1_END';
  176. $word = '';
  177. }
  178. } else {
  179. $flag = 'QUOTATION1_END';
  180. $word = '';
  181. }
  182. } elseif (in_array($flag, array('MULTI_COMMENT','LINE_COMMENT','LINE_COMMENT2','QUOTATION2')) == false) {
  183. $flag = 'QUOTATION1';
  184. }
  185. }
  186. elseif ($flag == 'QUOTATION2' || $char == $this->stx[$lang]['QUOTATION2'])
  187. {
  188. $word .= $char;
  189. if ($flag == 'QUOTATION2' && $char == $this->stx[$lang]['QUOTATION2']) {
  190. if (preg_match("/([\\".$this->stx[$lang]['ESCAPE']."]+)".$this->stx[$lang]['QUOTATION2']."$/",$word,$matches)) {
  191. if (strlen($matches[0])%2) {
  192. $flag = 'QUOTATION2_END';
  193. $word = '';
  194. }
  195. } else {
  196. $flag = 'QUOTATION2_END';
  197. $word = '';
  198. }
  199. } elseif (in_array($flag, array('MULTI_COMMENT','LINE_COMMENT','LINE_COMMENT2','QUOTATION1')) == false) {
  200. $flag = 'QUOTATION2';
  201. }
  202. }
  203. elseif (in_array($char, array("\r","\n","\t"," ")))
  204. {
  205. $flag = 'SPACE';
  206. }
  207. elseif (in_array($char, $this->stx[$lang]['DELIMITER']))
  208. {
  209. if (in_array($flag, array('WORD','VARIABLES1','VARIABLES2','VARIABLES3')) == false) $flag = 'DELIMITER';
  210. }
  211. elseif ($this->status == 'SCRIPT' && $char == $this->stx[$lang]['PREFIX3'])
  212. {
  213. $flag = 'PREFIX3';
  214. }
  215. elseif ($this->status == 'SCRIPT' && $char == $this->stx[$lang]['PREFIX4'])
  216. {
  217. $flag = 'PREFIX4';
  218. }
  219. elseif ($this->status == 'SCRIPT' && $char == $this->stx[$lang]['PREFIX5'])
  220. {
  221. $flag = 'PREFIX5';
  222. }
  223. elseif (preg_match("/!|[\w]|[\x80-\xff]/", $char))
  224. {
  225. $word .= $char;
  226. if ($flag == 'PREFIX3' || $flag == 'VARIABLES1') $flag = 'VARIABLES1';
  227. elseif ($flag == 'PREFIX4' || $flag == 'VARIABLES2') $flag = 'VARIABLES2';
  228. elseif ($flag == 'PREFIX5' || $flag == 'VARIABLES3') $flag = 'VARIABLES3';
  229. else $flag = 'WORD';
  230. if (preg_match("/!|[\w]|[\x80-\xff]/", $char2) == false) {
  231. if ($flag == 'VARIABLES1') {
  232. $flag = 'VARIABLES1_END';
  233. } elseif ($flag == 'VARIABLES2') {
  234. $flag = 'VARIABLES2_END';
  235. } elseif ($flag == 'VARIABLES3') {
  236. $flag = 'VARIABLES3_END';
  237. } elseif ($this->status == 'SCRIPT' && in_array(strtolower($word), $this->stx[$lang]['RESERVED'])) {
  238. $flag = 'RESERVED';
  239. } elseif ($this->status == 'SCRIPT' && in_array(strtolower($word), $this->stx[$lang]['FUNCTIONS'])) {
  240. $flag = 'FUNCTIONS';
  241. } elseif ($this->status == 'SCRIPT_END' && in_array(strtolower($word), $this->stx['html']['RESERVED'])) {
  242. $flag = 'HTML_TAG';
  243. } elseif ($this->status == 'SCRIPT_END' && in_array(strtolower($word), $this->stx['html']['FUNCTIONS'])) {
  244. $flag = 'HTML_ATTRIBUTES';
  245. } else {
  246. $flag = 'WORD_END';
  247. }
  248. $word = '';
  249. }
  250. }
  251. return $flag;
  252. }
  253.  
  254. function stx_string($str)
  255. {
  256. if (in_array($this->lang, $this->lang_arr) == false) $this->lang = 'php';
  257. $code_begin = '<code>';
  258. $words_begin = '<span style="color:'.$this->color['words'].';font-family:Courier New;">';
  259. $delimiter_begin = '<span style="color:'.$this->color['delimiter'].';font-family:Courier New;">';
  260. $reserved_begin = '<span style="color:'.$this->color['reserved'].';font-family:Courier New;">';
  261. $functions_begin = '<span style="color:'.$this->color['functions'].';font-family:Courier New;">';
  262. $quotation_begin = '<span style="color:'.$this->color['quotation'].';font-family:Courier New;">';
  263. $variables1_begin = '<span style="color:'.$this->color['variables1'].';font-family:Courier New;">';
  264. $variables2_begin = '<span style="color:'.$this->color['variables2'].';font-family:Courier New;">';
  265. $variables3_begin = '<span style="color:'.$this->color['variables3'].';font-family:Courier New;">';
  266. $comment_begin = '<span style="color:'.$this->color['comment'].';font-family:Courier New;">';
  267. $span_end = '</span>';
  268. $code_end = '</code>';
  269.  
  270. $this->len = strlen($str);
  271. $rs = $code_begin.$words_begin;
  272. $word = '';
  273. $flag = 'NORMAL';
  274. if (in_array($this->lang, array('php','jsp','html'))) {
  275. $this->status = 'SCRIPT_END';
  276. } else {
  277. $this->status = 'SCRIPT';
  278. }
  279.  
  280. for ($i = 0; $i < $this->len; $i++) {
  281. $char = $str{$i};
  282. $flag = $this->get_flag($str, $i, $word, $flag);
  283. switch ($flag) {
  284. case 'MULTI_COMMENT':
  285. case 'LINE_COMMENT':
  286. case 'LINE_COMMENT2':
  287. case 'QUOTATION1':
  288. case 'QUOTATION2':
  289. case 'VARIABLES1':
  290. case 'VARIABLES2':
  291. case 'VARIABLES3':
  292. case 'WORD';
  293. case 'HTML_COMMENT';
  294. $word .= $char;
  295. break;
  296. case 'MULTI_COMMENT_END':
  297. case 'LINE_COMMENT_END':
  298. case 'LINE_COMMENT2_END':
  299. case 'HTML_COMMENT_END';
  300. $word .= $char;
  301. $rs .= $comment_begin.htmlspecialchars($word).$span_end;
  302. $word = '';
  303. break;
  304. case 'QUOTATION1_END':
  305. case 'QUOTATION2_END':
  306. $word .= $char;
  307. $rs .= $quotation_begin.htmlspecialchars($word).$span_end;
  308. $word = '';
  309. break;
  310. case 'DELIMITER':
  311. $rs .= $delimiter_begin.htmlspecialchars($char).$span_end;
  312. $word = '';
  313. break;
  314. case 'PREFIX3':
  315. case 'PREFIX4':
  316. case 'PREFIX5':
  317. $rs .= $reserved_begin.$char.$span_end;
  318. $word = '';
  319. break;
  320. case 'VARIABLES1_END':
  321. $word .= $char;
  322. $rs .= $variables1_begin.$word.$span_end;
  323. $word = '';
  324. break;
  325. case 'VARIABLES2_END':
  326. $word .= $char;
  327. $rs .= $variables2_begin.$word.$span_end;
  328. $word = '';
  329. break;
  330. case 'VARIABLES3_END':
  331. $word .= $char;
  332. $rs .= $variables3_begin.$word.$span_end;
  333. $word = '';
  334. break;
  335. case 'RESERVED':
  336. case 'HTML_TAG':
  337. $word .= $char;
  338. $rs .= $reserved_begin.$word.$span_end;
  339. $word = '';
  340. break;
  341. case 'FUNCTIONS':
  342. case 'HTML_ATTRIBUTES':
  343. $word .= $char;
  344. $rs .= $functions_begin.$word.$span_end;
  345. $word = '';
  346. break;
  347. case 'WORD_END':
  348. $word .= $char;
  349. $rs .= $word;
  350. $word = '';
  351. break;
  352. case 'SPACE':
  353. default:
  354. $rs .= $char;
  355. $word = '';
  356. break;
  357. }
  358. }
  359. $rs .= $span_end.$code_end;
  360. $rs = preg_replace("/\n\r|\r\n|\r/", "\n", $rs);
  361. $rs = preg_replace("/\t/","&nbsp;&nbsp;&nbsp;&nbsp;",$rs);
  362. $rs = preg_replace_callback("/\n[ ]+/",
  363. create_function(
  364. '$matches',
  365. 'return str_replace(" ","&nbsp;",$matches[0]);'
  366. ),
  367. $rs);
  368. $rs = nl2br($rs);
  369. return $rs;
  370. }
  371.  
  372. function stx_file($file)
  373. {
  374. if (file_exists($file)) {
  375. if (empty($this->lang)) {
  376. $ext = strtolower(trim(array_pop(explode(".",$file))));
  377. if (empty($this->ext_arr[$ext]) == false) {
  378. $this->lang = strtolower($this->ext_arr[$ext]);
  379. }
  380. }
  381. $str = file_get_contents($file);
  382. $str = $this->stx_string($str);
  383. return $str;
  384. } else {
  385. return false;
  386. }
  387. }
  388.  
  389. }
  390. ?>
Add Comment
Please, Sign In to add comment