A tumblelog CMS built on AJAX, PHP and MySQL.

kodrs.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. class kodrs extends plugins
  3. {
  4. public function kodrs()
  5. {
  6. if (!defined('GESHI_VERSION')) {
  7. require_once("geshi/geshi.php");
  8. }
  9. $this->addAction('post_content', 'source_code_beautifier');
  10. }
  11. public function info()
  12. {
  13. return array(
  14. 'name' => 'kodrs',
  15. 'version' => '0.1',
  16. 'author' => 'Pedro Santana',
  17. 'authorurl' => 'http://www.pecesama.net/',
  18. 'license' => 'MIT License',
  19. 'description' => 'Geshify your source codes',
  20. );
  21. }
  22. public function source_code_beautifier()
  23. {
  24. global $rows;
  25. if (count($rows)>0) {
  26. foreach ($rows as $key=>$post) {
  27. if ($post["postType"]=="post") {
  28. $text = $rows[$key]['Body'];
  29. $result = preg_replace_callback("/<code\s+.*lang\s*=\"(.*)\">(.*)<\/code>/siU",
  30. array('kodrs', 'replace_with_geshi'),
  31. $text
  32. );
  33. $rows[$key]['Body'] = $result;
  34. }
  35. }
  36. }
  37. }
  38. public function replace_with_geshi($matches)
  39. {
  40. $lang = strtolower($matches[1]) ;
  41. $code = trim($matches[2]);
  42. $geshi = new geshi($code, (isset($lang)) ? $lang : "");
  43. $geshi->enable_classes(false);
  44. $geshi->set_overall_id('geshi_code');
  45. return @$geshi->parse_code();
  46. }
  47. }