A tumblelog CMS built on AJAX, PHP and MySQL.

kodrs.php 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /*
  3. Plugin Name: kodrs
  4. Plugin URI: http://plugins.gelatocms.com/kodrs/
  5. Description: Geshify your source codes
  6. Author: Pedro Santana
  7. Author URI: http://www.pecesama.net/
  8. Version: 0.1
  9. */
  10. class kodrs extends plugins {
  11. function kodrs() {
  12. if (!defined('GESHI_VERSION')) {
  13. require_once("geshi/geshi.php");
  14. }
  15. $this->addAction('post_content', 'source_code_beautifier');
  16. }
  17. function source_code_beautifier() {
  18. global $rows;
  19. if(count($rows)>0){
  20. foreach($rows as $key=>$post){
  21. // Si no es tipo 'post' entonces no tiene 'Body' :)
  22. if($post["postType"]=="post"){
  23. $text = $rows[$key]['Body'];
  24. $result = preg_replace_callback("/<code\s+.*lang\s*=\"(.*)\">(.*)<\/code>/siU",
  25. array('kodrs', 'replace_with_geshi'),
  26. $text
  27. );
  28. $rows[$key]['Body'] = $result;
  29. }
  30. }
  31. }
  32. }
  33. static function replace_with_geshi($matches) {
  34. $lang = strtolower($matches[1]) ;
  35. $code = trim($matches[2]);
  36. $geshi = new geshi($code, (isset($lang)) ? $lang : "");
  37. $geshi->enable_classes(false);
  38. $geshi->set_overall_id('geshi_code');
  39. return @$geshi->parse_code();
  40. }
  41. }
  42. ?>