last update: Wed, 18 Jan 2012 19:27:18 +0000
  1. <?
  2. require_once 'mysql_cfg.php';
  3. require_once('code_css.php');
  4. class languagedata
  5. {
  6. var $case_sensitive = true;
  7. var $elements_in_array = array();
  8. var $elements_in_array_by_len = array();
  9. var $elements_until_end = array();
  10. var $elements_start_to_end = array();
  11. var $elements_start_to_end_escape = array();
  12. var $elements_until_end_first = array();
  13. var $elements_start_to_end_first = array();
  14. var $elements_start_to_end_escape_first = array();
  15. var $elements_regexp = array();
  16. var $shorts;
  17. var $delimiters = array();
  18. function cs($element)
  19. {
  20. if (!$this->case_sensitive)
  21. {
  22. return strtolower($element);
  23. }
  24. else
  25. {
  26. return $element;
  27. }
  28. }
  29. // +set
  30. function add_element_in_array($element, $tag)
  31. {
  32. $element = $this->cs($element);
  33. $this->elements_in_array[$element] = $tag;
  34. $this->elements_in_array_by_len[strlen($element)][$element] = $tag;
  35. }
  36. function add_element_until_end($element, $tag)
  37. {
  38. $element = $this->cs($element);
  39. $this->elements_until_end[$element] = $tag;
  40. $this->elements_until_end_first[$element[0]][$element] = $tag;
  41. }
  42. function add_element_start_to_end($element_start, $element_end, $tag)
  43. {
  44. $element_start = $this->cs($element_start);
  45. $element_end = $this->cs($element_end);
  46. $this->elements_start_to_end[$element_start] = array('TAG' => $tag,'START' => $element_start, 'END' => $element_end);
  47. $this->elements_start_to_end_first[$element_start[0]][$element_start] = array('TAG' => $tag,'START' => $element_start, 'END' => $element_end);
  48. }
  49. function add_element_start_to_end_escape($element_start, $element_end, $escape, $tag)
  50. {
  51. $element_start = $this->cs($element_start);
  52. $element_end = $this->cs($element_end);
  53. $this->elements_start_to_end_escape[$element_start] = array('TAG' => $tag,'START' => $element_start, 'END' => $element_end, 'ESCAPE' => $escape);
  54. $this->elements_start_to_end_escape_first[$element_start[0]][$element_start] = array('TAG' => $tag,'START' => $element_start, 'END' => $element_end, 'ESCAPE' => $escape);
  55. }
  56. function add_element_regexp($element, $tag)
  57. {
  58. $this->elements_regexp[$element] = $tag;
  59. }
  60. // -set
  61. function get_element_in_array_tag($element)
  62. {
  63. $element = $this->cs($element);
  64. if (isset($this->elements_in_array[$element]))
  65. {
  66. return $this->elements_in_array[$element];
  67. }
  68. return false;
  69. }
  70. function get_element_in_array_tag_fast($element, $len)
  71. {
  72. $element = $this->cs($element);
  73. if (isset($this->elements_in_array_by_len[$len][$element]))
  74. {
  75. return $this->elements_in_array_by_len[$len][$element];
  76. }
  77. return false;
  78. }
  79. function get_element_until_end_tag($element)
  80. {
  81. $element = $this->cs($element);
  82. if (isset($this->elements_until_end[$element]))
  83. {
  84. return $this->elements_until_end[$element];
  85. }
  86. return false;
  87. }
  88. function get_element_start_to_end_tag($element)
  89. {
  90. $element = $this->cs($element);
  91. if (isset($this->elements_start_to_end[$element]))
  92. {
  93. return $this->elements_start_to_end[$element];
  94. }
  95. return false;
  96. }
  97. var $regexp_count = 0;
  98. function get_element_regexp_tag($element)
  99. {
  100. foreach ($this->elements_regexp as $regexp => $tag)
  101. {
  102. if (preg_match($regexp, $element))
  103. {
  104. return $tag;
  105. }
  106. }
  107. return false;
  108. }
  109. function is_delimiter($element)
  110. {
  111. if (isset($this->delimiters[$element]))
  112. {
  113. return true;
  114. }
  115. return false;
  116. }
  117. function debug_print()
  118. {
  119. echo "<pre>";
  120. echo "<h3>delimiters</h3>";
  121. print_r($this->delimiters);
  122. echo "<h3>elements_in_array</h3>";
  123. print_r($this->elements_in_array);
  124. echo "<h3>elements_in_array_by_len</h3>";
  125. print_r($this->elements_in_array_by_len);
  126. echo "<h3>elements_until_end</h3>";
  127. print_r($this->elements_until_end);
  128. echo "<h3>elements_start_to_end</h3>";
  129. print_r($this->elements_start_to_end);
  130. echo "<h3>elements_regexp</h3>";
  131. print_r($this->elements_regexp);
  132. echo "</pre><hr />";
  133. }
  134. function load_array($a)
  135. {
  136. //load delimiters
  137. $this->delimiters = array_flip($a['DELIMITERS']);
  138. unset($a['DELIMITERS']);
  139. if (isset($a['CASE_SENSITIVE']))
  140. {
  141. $this->case_sensitive = !(!$a['CASE_SENSITIVE']);
  142. unset($a['CASE_SENSITIVE']);
  143. }
  144. //loop through all tags...
  145. foreach ($a as $tag => $e0)
  146. {
  147. foreach ($e0 as $e)
  148. {
  149. if ($e['TYPE'] == 'IN_ARRAY')
  150. {
  151. //loop through all array elemnts...
  152. foreach ($e['ARRAY'] as $element)
  153. {
  154. $this->add_element_in_array($element, $tag);
  155. }
  156. }
  157. elseif ($e['TYPE'] == 'UNTIL_END')
  158. {
  159. $this->add_element_until_end($e['START'], $tag);
  160. }
  161. elseif ($e['TYPE'] == 'REGEXP')
  162. {
  163. $this->add_element_regexp($e['REGEXP'], $tag);
  164. }
  165. elseif ($e['TYPE'] == 'START_TO_END')
  166. {
  167. $this->add_element_start_to_end($e['START'], $e['END'], $tag);
  168. }
  169. elseif ($e['TYPE'] == 'START_TO_END_ESCAPE')
  170. {
  171. $this->add_element_start_to_end_escape($e['START'], $e['END'], $e['ESCAPE'], $tag);
  172. }
  173. else
  174. {
  175. echo "Corrupted Language-Data-Array. Error in line ". __LINE__;
  176. }
  177. }
  178. }
  179. }
  180. }
  181. class highlightstyle
  182. {
  183. var $styles = array();
  184. function get_style($tag)
  185. {
  186. if (isset($this->styles[$tag]))
  187. {
  188. return $this->styles[$tag];
  189. }
  190. return '%%';
  191. }
  192. function create_styled_text ($text, $tag)
  193. {
  194. $style = $this->get_style($tag);
  195. $text2=$text;
  196. $text2 = str_replace("<","<",$text2);
  197. $text2 = str_replace(">",">",$text2);
  198. //$text2 = str_replace(" "," ",$text2);
  199. return str_replace('%%', $text2, $style);
  200. }
  201. function create_styled_text_raw ($text, $tag)
  202. {
  203. $style = $this->get_style($tag);
  204. $text2=$text;
  205. //$text2 = str_replace("<","<",$text2);
  206. //$text2 = str_replace(">",">",$text2);
  207. //$text2 = str_replace(" "," ",$text2);
  208. return str_replace('%%', $text2, $style);
  209. }
  210. function load_array($a)
  211. {
  212. $this->styles = $a;
  213. }
  214. }
  215. class codehighlighter
  216. {
  217. var $lines;
  218. function codehighlighter($lines)
  219. {
  220. $this->lines = $lines;
  221. }
  222. function insert_highlight($line, $from, $to, $hl_start = '<span class="highlight">', $hl_end = '</span>')
  223. {
  224. //echo "<br /><br />$from - $to ".htmlspecialchars($line)." <br />\n";
  225. //return $line;
  226. $result = '';
  227. $intag = false;
  228. $hl = false;
  229. $ignore_next = false;
  230. $add_start = false;
  231. $pos = 0;
  232. $real_pos = 0;
  233. $len = strlen($line);
  234. $c = "";
  235. while ($pos < $len)
  236. {
  237. $c = $line[$pos];
  238. if ($intag)
  239. {
  240. if ($c == ">")
  241. {
  242. $intag = false;
  243. if ($hl)
  244. {
  245. $add_start = true;
  246. //$result.= $hl_start;
  247. }
  248. }
  249. }
  250. else
  251. {
  252. if ($c == "<")
  253. {
  254. $intag = true;
  255. if (($real_pos == $to))
  256. {
  257. $result.=$hl_end;
  258. $hl=false;
  259. }
  260. else if ($hl)
  261. {
  262. if (!$ignore_next)
  263. {
  264. $result.= $hl_end;
  265. }
  266. $ignore_next = false;
  267. }
  268. }
  269. else
  270. {
  271. if (($real_pos == $from))
  272. {
  273. //echo "real_pos == from<br />";
  274. $hl = true;
  275. if ($line[$pos] == "<")
  276. {
  277. //echo "line[pos]+1 == <";
  278. $ignore_next = true;
  279. //$result.="<";
  280. //$pos++;
  281. //$intag=true;
  282. }
  283. else
  284. {
  285. //echo "adding hl_start<br />";
  286. $result.=$hl_start;
  287. }
  288. }
  289. if (($real_pos == $to))
  290. {
  291. $result.=$hl_end;
  292. $hl=false;
  293. }
  294. if (!$intag)
  295. {
  296. $real_pos++;
  297. }
  298. }
  299. }
  300. $result.=$line[$pos];
  301. if ($add_start)
  302. {
  303. $result.= $hl_start;
  304. $add_start = false;
  305. }
  306. //echo "($pos, $real_pos, ". (($intag) ? "true" : "false") .") ".htmlspecialchars($result)." <br />\n";
  307. $pos++;
  308. }
  309. return $result;
  310. }
  311. function insert_highlight_old($line, $from, $to)
  312. {
  313. $soll = substr(strip_tags($line), $from, $to-$from);
  314. $result = '';
  315. $hl = false;
  316. $intag = false;
  317. $open = false;
  318. $start_after_endtag = false;
  319. //loop through chars
  320. $pos = 0;
  321. $real_pos = 0;
  322. $len = strlen($line);
  323. while ($pos < $len)
  324. {
  325. //echo "$pos || $real_pos || ". $line[$pos] . "<br />";
  326. if ((!$hl) && (!$intag) && ($real_pos == $from))
  327. {
  328. $result.= '<span class="highlight">';
  329. $hl = true;
  330. }
  331. if ($intag)
  332. {
  333. $result.=$line[$pos];
  334. if ($line[$pos] == '>')
  335. {
  336. $intag = false;
  337. if ($start_after_endtag)
  338. {
  339. $start_after_endtag = false;
  340. $result.='<span class="highlight">';
  341. }
  342. }
  343. }
  344. else //not intag
  345. {
  346. if ($line[$pos] == '<')
  347. {
  348. $intag = true;
  349. if ($line[$pos+1] == '/')
  350. {
  351. $open = false;
  352. if (($hl) && ($real_pos >= $from) && ($real_pos < $to))
  353. {
  354. $result.='</span>';
  355. $start_after_endtag = true;
  356. }
  357. }
  358. else
  359. {
  360. $open = true;
  361. }
  362. $result.=$line[$pos];
  363. }
  364. else
  365. {
  366. $result.=$line[$pos];
  367. ++$real_pos;
  368. }
  369. if (($hl) && ($real_pos == $to))
  370. {
  371. $result.= '</span>';
  372. $hl = false;
  373. }
  374. }
  375. ++$pos;
  376. }
  377. if ($hl)
  378. {
  379. $result.= '</span>';
  380. }
  381. $result = str_replace('<span class="highlight"></span>', '', $result);
  382. echo "<pre>\n\n".htmlspecialchars($line). "\n". htmlspecialchars($result) ."\n</pre>";
  383. return $result;
  384. }
  385. function fill_line_array($s)
  386. {
  387. return explode("\n",trim($s));
  388. }
  389. function parse_code($language_data, $highlight_style)
  390. {
  391. $output_lines = array();
  392. //go through all lines
  393. //temp
  394. $cur_tag = '';
  395. $cur_end = '';
  396. $line_nr = 0;
  397. foreach($this->lines as $line)
  398. {
  399. $line_nr++;
  400. $add_lines = substr_count($line, "\n");
  401. //cut off right whitespace
  402. $line = rtrim($line);
  403. //replace tabs by 4 spaces
  404. $line = str_replace("\t"," ",$line);
  405. //manage highlights with or ##
  406. $highlight_line = false;
  407. if (substr($line, 0, 2) == '##')
  408. {
  409. $highlight_line = true;
  410. $line = substr($line, 2);
  411. }
  412. $highlight_part_a = array();
  413. $highlight_part = false;
  414. $highlight_count = 0;
  415. $pos = 0;
  416. while (1)
  417. {
  418. $pos = strpos($line, "", $pos);
  419. if ($pos === false)
  420. {
  421. if ((isset($highlight_part_a[$highlight_count-1]['START'])) && (!isset($highlight_part_a[$highlight_count-1]['END'])))
  422. {
  423. $highlight_part_a[$highlight_count-1]['END'] = strlen($line);
  424. }
  425. break;
  426. }
  427. else
  428. {
  429. if ($highlight_part)
  430. {
  431. //end
  432. $highlight_part_a[$highlight_count-1]['END'] = $pos - 4 * $highlight_count + 2;
  433. }
  434. else
  435. {
  436. //start
  437. $highlight_part_a[$highlight_count]['START'] = $pos - 4 * $highlight_count;
  438. ++$highlight_count;
  439. }
  440. $highlight_part = !$highlight_part;
  441. ++$pos;
  442. }
  443. ++$pos;
  444. }
  445. if ($highlight_count)
  446. {
  447. $line = str_replace("","",$line);
  448. }
  449. $pos = 0;
  450. $output_line = '';
  451. if (!$cur_tag)
  452. {
  453. //find the first no whitespace character
  454. $pos = strlen($line) - strlen(ltrim($line));
  455. $output_line.= substr($line,0,$pos);
  456. }
  457. else
  458. {
  459. $end_pos = false;
  460. if ($cur_escape === false) {
  461. $end_pos = strpos($line, $cur_end);
  462. } else {
  463. $p = 0;
  464. $escape = $cur_escape;
  465. while ($p <= strlen($line)-strlen($end)) {
  466. if ($line[$p] == $escape) {
  467. $p+=2;
  468. continue;
  469. } elseif (substr($line, $p, strlen($end)) == $end) {
  470. $end_pos = $p;
  471. break;
  472. }
  473. $p++;
  474. }
  475. }
  476. if ($end_pos === false)
  477. {
  478. $output_line.= $highlight_style->create_styled_text($line , $cur_tag);
  479. $pos = strlen($line);
  480. //continue;
  481. }
  482. else
  483. {
  484. $pos = $end_pos+strlen($cur_end);
  485. $output_line.= $highlight_style->create_styled_text(substr($line, 0, $pos) , $cur_tag);
  486. $cur_tag = '';
  487. $cur_end = '';
  488. }
  489. }
  490. //temp strings
  491. $word = '';
  492. $endword = false;
  493. $add = '';
  494. //loop through remaining chars
  495. $len = strlen($line);
  496. while ($pos < $len)
  497. {
  498. $c = array();
  499. $c[0] = $line[$pos];
  500. for ($o = 1; ($o + $pos < $len) && ($o < 3); $o++ )
  501. {
  502. $c[$o] = $c[$o-1] . $line[$pos + $o];
  503. }
  504. /*if ((isset($c[1])) && ($c[1] == ''))
  505. {
  506. if ($highlight_part)
  507. {
  508. $highlight_part = false;
  509. //$word.= '</span>';
  510. }
  511. else
  512. {
  513. $highlight_part = true;
  514. //$word.= '<span class="highlight">';
  515. }
  516. $pos+=2;
  517. continue;
  518. }
  519. */
  520. $found = false;
  521. /* old way
  522. for ($c_pos = sizeof($c)-1; $c_pos >= 0; $c_pos--)
  523. {
  524. $cc = $c[$c_pos];
  525. if ($tag = $language_data->get_element_until_end_tag($cc))
  526. {
  527. $endword = true;
  528. $add = $highlight_style->create_styled_text(substr($line, $pos) , $tag);
  529. $pos = strlen($line);
  530. $found = true;
  531. break;
  532. }
  533. elseif ($data = $language_data->get_element_start_to_end_tag($cc))
  534. {
  535. $tag = $data['TAG'];
  536. $end = $data['END'];
  537. $end_pos = strpos($line, $end, $pos + $c_pos + 1);
  538. if ($end_pos === false)
  539. {
  540. $endword = true;
  541. $add = $highlight_style->create_styled_text(substr($line, $pos) , $tag);
  542. $pos = strlen($line);
  543. //save tag and end for next line
  544. $cur_tag = $tag;
  545. $cur_end = $end;
  546. }
  547. else
  548. {
  549. $endword = true;
  550. $add = $highlight_style->create_styled_text(substr($line, $pos, $end_pos - $pos + strlen($end)) , $tag);
  551. $pos = $end_pos + $c_pos;
  552. }
  553. $found = true;
  554. break;
  555. }
  556. }
  557. */
  558. //Altrenative : andersrum...
  559. if (isset($language_data->elements_until_end_first[$c[0]]))
  560. {
  561. $found_tag = '';
  562. $found_tag_len = 0;
  563. foreach ($language_data->elements_until_end_first[$c[0]] as $element => $tag)
  564. {
  565. $element_len = strlen($element)-1;
  566. if (isset($c[$element_len]) &&($element == $c[$element_len]))
  567. {
  568. if ($element_len >= $found_tag_len)
  569. {
  570. $found_tag = $tag;
  571. $found_tag_len = $element_len;
  572. }
  573. }
  574. }
  575. if ($found_tag != '')
  576. {
  577. $tag = $found_tag;
  578. $endword = true;
  579. $add = $highlight_style->create_styled_text(substr($line, $pos) , $tag);
  580. $pos = strlen($line);
  581. $found = true;
  582. //break;
  583. }
  584. }
  585. if (!$found)
  586. {
  587. if (isset($language_data->elements_start_to_end_first[$c[0]]))
  588. {
  589. $found_tag = '';
  590. $found_data;
  591. $found_tag_len = 0;
  592. foreach ($language_data->elements_start_to_end_first[$c[0]] as $element => $data)
  593. {
  594. $element_len = strlen($element)-1;
  595. if (isset($c[$element_len]) &&($element == $c[$element_len]))
  596. {
  597. if ($element_len >= $found_tag_len)
  598. {
  599. $found_tag = $data['TAG']; //undefined variable tag?
  600. $found_data = $data;
  601. $found_tag_len = $element_len;
  602. }
  603. }
  604. }
  605. if ($found_tag != '')
  606. {
  607. $data = $found_data;
  608. $tag = $data['TAG'];
  609. $end = $data['END'];
  610. $end_pos = strpos($line, $end, $pos + $element_len + 1);
  611. if ($end_pos === false)
  612. {
  613. $endword = true;
  614. $add = $highlight_style->create_styled_text(substr($line, $pos) , $tag);
  615. $pos = strlen($line);
  616. //save tag and end for next line
  617. $cur_tag = $tag;
  618. $cur_end = $end;
  619. $cur_escape = false;
  620. }
  621. else
  622. {
  623. $endword = true;
  624. $add = $highlight_style->create_styled_text(substr($line, $pos, $end_pos - $pos + strlen($end)) , $tag);
  625. $pos = $end_pos + $element_len;
  626. }
  627. $found = true;
  628. //break;
  629. }
  630. }
  631. }
  632. if (!$found)
  633. {
  634. if (isset($language_data->elements_start_to_end_escape_first[$c[0]]))
  635. {
  636. $found_tag = '';
  637. $found_data;
  638. $found_tag_len = 0;
  639. foreach ($language_data->elements_start_to_end_escape_first[$c[0]] as $element => $data)
  640. {
  641. $element_len = strlen($element)-1;
  642. if (isset($c[$element_len]) &&($element == $c[$element_len]))
  643. {
  644. if ($element_len >= $found_tag_len)
  645. {
  646. $found_tag = $data['TAG']; //undefined variable tag?
  647. $found_data = $data;
  648. $found_tag_len = $element_len;
  649. }
  650. }
  651. }
  652. if ($found_tag != '')
  653. {
  654. $data = $found_data;
  655. $tag = $data['TAG'];
  656. $end = $data['END'];
  657. $escape = $data['ESCAPE'];
  658. $p = $pos+1;
  659. $end_pos = false;
  660. while ($p <= strlen($line)-strlen($end)) {
  661. if ($line[$p] == $escape) {
  662. $p+=2;
  663. continue;
  664. } elseif (substr($line, $p, strlen($end)) == $end) {
  665. $end_pos = $p;
  666. break;
  667. }
  668. $p++;
  669. }
  670. //$end_pos = strpos($line, $end, $pos + $element_len + 1);
  671. if ($end_pos === false)
  672. {
  673. $endword = true;
  674. $add = $highlight_style->create_styled_text(substr($line, $pos) , $tag);
  675. $pos = strlen($line);
  676. //save tag and end for next line
  677. $cur_tag = $tag;
  678. $cur_end = $end;
  679. $cur_escape = $escape;
  680. }
  681. else
  682. {
  683. $endword = true;
  684. $add = $highlight_style->create_styled_text(substr($line, $pos, $end_pos - $pos + strlen($end)) , $tag);
  685. $pos = $end_pos + $element_len;
  686. }
  687. $found = true;
  688. //break;
  689. }
  690. }
  691. }
  692. if (!$found)
  693. {
  694. if ($tag = $language_data->get_element_in_array_tag($c[0]))
  695. {
  696. $add = $highlight_style->create_styled_text($c[0], $tag);
  697. $endword = true;
  698. }
  699. elseif ($language_data->is_delimiter($c[0]))
  700. {
  701. $add = $c[0];
  702. $endword = true;
  703. }
  704. }
  705. if (!$endword)
  706. {
  707. $word.= $c[0];
  708. }
  709. else
  710. { //endword
  711. if ($word != '')
  712. {
  713. if ($tag = $language_data->get_element_in_array_tag($word))
  714. {
  715. $output_line.= $highlight_style->create_styled_text($word, $tag);
  716. }
  717. elseif ($tag = $language_data->get_element_regexp_tag($word))
  718. {
  719. $output_line.= $highlight_style->create_styled_text($word, $tag);
  720. }
  721. else
  722. {
  723. $output_line.= $word;
  724. }
  725. }
  726. $endword = false;
  727. $word = '';
  728. }
  729. if ($add != '')
  730. {
  731. $output_line.= $add;
  732. $add = '';
  733. }
  734. //----
  735. ++$pos;
  736. }
  737. if ($word != '') //REST
  738. {
  739. if ($tag = $language_data->get_element_in_array_tag($word))
  740. {
  741. $output_line.= $highlight_style->create_styled_text($word, $tag);
  742. }
  743. elseif ($tag = $language_data->get_element_regexp_tag($word))
  744. {
  745. $output_line.= $highlight_style->create_styled_text($word, $tag);
  746. }
  747. else
  748. {
  749. $output_line.= $word;
  750. }
  751. $word = '';
  752. }
  753. if ($output_line == "")
  754. {
  755. $output_line = " ";
  756. }
  757. if ($highlight_line)
  758. {
  759. $output_line = $highlight_style->create_styled_text_raw($output_line,"HIGHLIGHT_LINE");//"<li class='highlight_line'>$output_line</li>";
  760. }
  761. else
  762. {
  763. $output_line = $highlight_style->create_styled_text_raw($output_line,"NORMAL_LINE");//"<li class='normal_line' style=''>$output_line</li>";
  764. }
  765. foreach ($highlight_part_a as $x)
  766. {
  767. $output_line = $this->insert_highlight($output_line, $x['START'], $x['END']);
  768. }
  769. if (($add_lines > 0) && (trim($line) == "!!!FIRSTLINE-SKIPTHIS!!!")) {
  770. // dirty hack, i hat me for doing it^^
  771. $output_line = '';
  772. }
  773. for ($i=0; $i < $add_lines; $i++)
  774. {
  775. $output_line.=$highlight_style->create_styled_text_raw($line_nr,"NULL_LINE");//"\n<li value='$line_nr' class='nullline'></li>";
  776. }
  777. $output_lines[] = $output_line;
  778. }
  779. $output = '';
  780. foreach ($output_lines as $output_line)
  781. {
  782. $output.="$output_line\n";
  783. }
  784. $output=$highlight_style->create_styled_text_raw($output,"BLOCK");
  785. return $output;
  786. //return $output_lines;
  787. }
  788. }
  789. /*
  790. //test
  791. $test =
  792. '
  793. 123456789abcdefghijklmn
  794. //! library CSSafety
  795. //******************************************************************************************
  796. //*
  797. //* CSSafety 14.5
  798. //* ��������
  799. //*
  800. //* Utilities to make things safer. Currently this simply includes a timer recycling
  801. //* Stack. Once you replace CreateTimer with NewTimer and DestroyTimer with ReleaseTimer
  802. //* you no longer have to care about setting timers to null nor about timer related issues
  803. //* with the handle index stack.
  804. //*
  805. //******************************************************************************************
  806. //==========================================================================================
  807. globals
  808. private timer array T
  809. private integer N = 0
  810. endglobals
  811. //==========================================================================================
  812. function NewTimer takes nothing returns timer
  813. if (N==0) then
  814. return CreateTimer()
  815. endif
  816. set N=N-1
  817. return T[N]
  818. endfunction
  819. //==========================================================================================
  820. function ReleaseTimer takes timer t returns nothing
  821. call PauseTimer(t)
  822. if (N==8191) then
  823. debug call BJDebugMsg("Warning: Timer stack is full, destroying timer!!")
  824. //stack is full, the map already has much more troubles than the chance of bug
  825. call DestroyTimer(t)
  826. else
  827. set T[N]=t
  828. set N=N+1
  829. endif
  830. endfunction
  831. //! endlibrary
  832. ';
  833. $time = microtime(1);
  834. $ld = new languagedata();
  835. $ld->load_array($language_data);
  836. //$ld->debug_print();
  837. echo "languagedata init in ".(microtime(1)-$time)." secs<br />" ;
  838. $time = microtime(1);
  839. $hs = new highlightstyle();
  840. $hs->load_array($style_data);
  841. echo "highlightstyle init in ".(microtime(1)-$time)." secs<br />" ;
  842. $lines = array(
  843. 'function f$oo$ takes nothing returns string ',
  844. ' local integer i = 0.8 //comment
  845. ',
  846. ' return "Hallo, das
  847. ',
  848. ' ist ein String',
  849. ' der �ber mehrere ',
  850. ' Zeilen geht." ',
  851. 'endfunction //comment'
  852. );
  853. //$ch = new codehighlighter(fill_line_array($test));
  854. $ch = new codehighlighter($lines);
  855. $time = microtime(1);
  856. $ch->parse_code($ld, $hs);
  857. echo "parsing done in ".(microtime(1)-$time)." secs<br />" ;
  858. */
  859. class easycodehighlighter
  860. {
  861. var $language_id;
  862. var $style_id;
  863. var $language_name;
  864. var $code;
  865. var $output;
  866. var $code_css;
  867. var $language = false;
  868. var $format_script;
  869. function fill_line_array($s)
  870. {
  871. return explode("\n",$s);
  872. }
  873. function strip_markup($s)
  874. {
  875. return str_replace('##', '', str_replace('', '', $s));
  876. }
  877. function easycodehighlighter($input, $default_lang, $language_id = null, $style_id = null, $format_output = false)
  878. {
  879. $this->code = $input;
  880. $output_lines = '';
  881. $this->language = false;
  882. if ($language_id != null)
  883. {
  884. //try to load language
  885. $query = 'SELECT data, defaultstyle, `id`, `name` FROM languages WHERE `id` = "'.$language_id.'";';
  886. $this->language = mysql_fetch_array(mysql_query($query));
  887. }
  888. if (!$this->language)
  889. {
  890. //load default language
  891. $query = 'SELECT data, defaultstyle, `id`, `name` FROM languages WHERE `name` = "'.$default_lang.'";';
  892. $this->language = mysql_fetch_array(mysql_query($query));
  893. }
  894. if ($this->language)
  895. {
  896. $this->language_id = $this->language['id'];
  897. $this->format_script = 'format_'.$this->language['name'].'.php';
  898. if (file_exists($this->format_script) != '')
  899. {
  900. if ($format_output)
  901. {
  902. require_once('format_'.$this->language['name'].'.php');
  903. //$my_jass_formater = new jass_formater();
  904. eval('$my_formater = new '.$this->language['name'].'_formater();');
  905. $this->code = $my_formater->format($this->strip_markup($this->code),true,true);
  906. }
  907. }
  908. else
  909. {
  910. $this->format_script = null;
  911. }
  912. $code_lines = null;
  913. if (is_array($this->code))
  914. {
  915. $code_lines = $this->code;
  916. }
  917. else
  918. {
  919. $code_lines = $this->fill_line_array($this->code);
  920. }
  921. eval('$language_data = array('.$this->language['data'].');');
  922. $ld = new languagedata();
  923. $ld->load_array($language_data);
  924. //$style_id = 0;
  925. $style = null;
  926. $cssdata = null;
  927. if ($style_id)
  928. {
  929. $query = 'SELECT data, cssdata FROM styles WHERE `id` = "'.$style_id.'";';
  930. $style = mysql_fetch_array(mysql_query($query));
  931. if (!$style)
  932. {
  933. $style_id = 0;
  934. }
  935. $cssdata = $style['cssdata'];
  936. $style = $style['data'];
  937. }
  938. if ($style_id == 0)
  939. {
  940. //load default value of language
  941. $style_id = $this->language['defaultstyle'];
  942. $query = 'SELECT data, cssdata FROM styles WHERE `id` = "'.$style_id.'";';
  943. $style = mysql_fetch_array(mysql_query($query));
  944. $cssdata = $style['cssdata'];
  945. $style = $style['data'];
  946. }
  947. $this->code_css = code_css($cssdata);
  948. $this->style_id = $style_id;
  949. eval('$style_data = array('.$style.');');
  950. $hs = new highlightstyle();
  951. $hs->load_array($style_data);
  952. $ch = new codehighlighter($code_lines);
  953. $output = $ch->parse_code($ld, $hs);
  954. }
  955. else
  956. { //no language definend
  957. $this->language_id = 0;
  958. $code_lines = fill_line_array($this->code);
  959. $output='<textarea>';
  960. for($i=0;$i<sizeof($code_lines);$i++)
  961. {
  962. $output_lines[$i] = "".$code_lines[$i]."\n";
  963. }
  964. $output='</textarea>';
  965. }
  966. $this->output = $output;
  967. }
  968. }
  969. ?>

goto line:
Compare with:
text copy window edit this code post new code