| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | class HighlightPlugin extends Plugin { |
|---|
| 28 | |
|---|
| 29 | public function info() { |
|---|
| 30 | return array ( |
|---|
| 31 | 'name' => 'Highlighter', |
|---|
| 32 | 'url' => 'http://seancoates.com/habari', |
|---|
| 33 | 'author' => 'Sean Coates', |
|---|
| 34 | 'authorurl' => 'http://seancoates.com/', |
|---|
| 35 | 'version' => '0.1.1', |
|---|
| 36 | 'description' => 'Highlighter', |
|---|
| 37 | 'license' => 'Apache License 2.0', |
|---|
| 38 | ); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | public function action_init() { |
|---|
| 42 | spl_autoload_register( array( __CLASS__, '_autoload') ); |
|---|
| 43 | Format::apply( 'do_highlight', 'post_content_out' ); |
|---|
| 44 | Format::apply( 'do_highlight', 'comment_content_out' ); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public static function _autoload( $class_name ) { |
|---|
| 48 | if ( strtolower( $class_name ) == 'geshi' ) { |
|---|
| 49 | require HABARI_PATH . "/3rdparty/geshi/geshi.php"; |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | class GeshiHighlighterFormatPlugin extends Format |
|---|
| 55 | { |
|---|
| 56 | |
|---|
| 57 | public static function do_highlight( $in ) |
|---|
| 58 | { |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | $tokenizer = new HTMLTokenizer( $in, false ); |
|---|
| 62 | $tokens = $tokenizer->parse(); |
|---|
| 63 | $slices = $tokens->slice( array('div','pre','code') , array( 'class' => 'highlight' ) ); |
|---|
| 64 | foreach ($slices as $slice) { |
|---|
| 65 | $classes = array_filter( explode( ' ', trim( str_replace( 'highlight', '', $slice[0]['attrs']['class'] ) ) ) ); |
|---|
| 66 | $slice->trim_container(); |
|---|
| 67 | $sliceValue = trim( (string)$slice ); |
|---|
| 68 | |
|---|
| 69 | $sliceCacheName = 'plugin.highlight.' . md5($sliceValue); |
|---|
| 70 | |
|---|
| 71 | if ( Cache::has( $sliceCacheName ) ) { |
|---|
| 72 | $geshiOutput = Cache::get( $sliceCacheName ); |
|---|
| 73 | } else { |
|---|
| 74 | |
|---|
| 75 | if ( substr( $sliceValue, 0, 9 ) == '<![CDATA[' && substr( $sliceValue, -3 ) == ']]>' ) { |
|---|
| 76 | |
|---|
| 77 | $sliceValue = substr( $sliceValue, 9, -3 ); |
|---|
| 78 | } |
|---|
| 79 | $geshi = new Geshi( trim( $sliceValue ), isset( $classes[0] ) ? $classes[0] : 'php' ); |
|---|
| 80 | $geshi->set_header_type( GESHI_HEADER_PRE ); |
|---|
| 81 | $geshi->set_overall_class( 'geshicode' ); |
|---|
| 82 | $geshiOutput = @$geshi->parse_code(); |
|---|
| 83 | Cache::set( $sliceCacheName, $geshiOutput ); |
|---|
| 84 | } |
|---|
| 85 | $slice->tokenize_replace( $geshiOutput ); |
|---|
| 86 | $tokens->replace_slice( $slice ); |
|---|
| 87 | } |
|---|
| 88 | return (string) $tokens; |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | ?> |
|---|