A tumblelog CMS built on AJAX, PHP and MySQL.

kodrs.php 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. foreach($rows as $key=>$post){
  20. // Si no es tipo 'post' entonces no tiene 'Body' :)
  21. if($post["postType"]=="post"){
  22. $text = $rows[$key]['Body'];
  23. $result = preg_replace_callback("/<code\s+.*lang\s*=\"(.*)\">(.*)<\/code>/siU",
  24. array('kodrs', 'replace_with_geshi'),
  25. $text
  26. );
  27. $rows[$key]['Body'] = $result;
  28. }
  29. }
  30. }
  31. static function replace_with_geshi($matches) {
  32. $lang = strtolower($matches[1]) ;
  33. $code = trim($matches[2]);
  34. $geshi = new geshi($code, (isset($lang)) ? $lang : "");
  35. $geshi->enable_classes(false);
  36. $geshi->set_overall_id('geshi_code');
  37. return @$geshi->parse_code();
  38. }
  39. }
  40. ?>