<?php
/*
Plugin Name: Table of Contents for Markdown
Plugin URI: http://bmky.net/product/script/toc_for_md.php
Description: Insert table of contents
Version: 0.1
Author: szsk
Author URI: http://bmky.net/
*/

Class toc_for_markdown {
    var 
$html_prefix  "<div class=\"index\">\n<p>目次</p>\n";
    var 
$html_suffix  "</div>";
    var 
$def_minlvl   2;
    var 
$def_maxlvl   3;
    var 
$toc_startcnt 4;
    
    
    
    
    
    function 
toc_for_markdown( ) {
        
$this->minlvl $this->def_minlvl;
        
$this->maxlvl $this->def_maxlvl;
    }
    
    function 
get_toc( ) {
        global 
$post$pages;
        
$content is_page( ) ? $pagescount$pages ) - ] : $post->post_content;
        
        if( !
preg_match'{^<!--disable TOC-->[\r\n]+}m'$content ) ) {
            if( 
is_page( ) )
                
$this->create_toc$pagescount$pages ) - ] );
            else
                
$this->create_toc$post->post_content );
            
            
$this->tochtml $this->html_prefix $this->tochtml $this->html_suffix;
            
            if( 
$this->toccnt $this->toc_startcnt ) {
                if( 
is_page( ) )
                    
$pagescount$pages ) - ] = $content;
                else
                    
$post->post_content $content;
                
                
$this->tochtml "";
            }
        }
    }
    
    function 
get_toc_filter$content ) {
        if( 
preg_match'{^<!--TOC( min="(\d+)")?( max="(\d+)")?-->[\r\n]+}m'$content$m ) ) {
            
$this->minlvl $m[1] ? intval$m[2] ) : $this->def_minlvl;
            
$this->maxlvl $m[3] ? intval$m[4] ) : $this->def_maxlvl;
            
            
$this->create_toc$content );
            
$content str_replace$m[0], $this->html_prefix $this->tochtml $this->html_suffix$content );
        }
        
        return 
$content;
    }
    
    function 
create_toc( &$text ) {
        
$text preg_replace_callback('|
            ^(\#{'
.$this->minlvl.','.$this->maxlvl.'})
            [ ]*(?!\#)
            (.+?)
            (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})?
            [\r\n]*$
        |xm'
,array(&$this'_getHeader'), $text );
        
        
$this->tochtml Markdownjoin"\n"$this->toclst ) );
    }
    
    function 
_getHeader$m ) {
        global 
$post;
        
$level  $m[1];
        
$title  $m[2];
        
$anchor $m[3];
        
$pid    $post "p" $post->ID "";
        
        
// 見出し内にリンクがあったらテキストだけ抜き出す
        
$title  preg_replace'/\[(.*?)\]\(.*?\)/'"$1"$title );
        
$title  trim$title );
        
// 見出しレベルに応じたタブを付加する
        
$tab    str_repeat"\t"strlen$level ) - $this->minlvl );
        
// id無しなら適当につける
        
$anchor $anchor $anchor $pid "h" $this->toccnt++;
        
        
array_push$this->toclst$tab "1. [" $title "](#" $anchor ")" );
        
//return $level . " " . $matches[2] . " {#" . $anchor . "}";
        
return $level " " trim$m[2], "\r\n" ) . " {#" $anchor "}";
    }
    
    var 
$toccnt  1;
    var 
$toclst  = array( );
    var 
$tochtml "";
}

add_filter('the_content', array(new toc_for_markdown'get_toc_filter'), 4);
//add_filter('the_content_rss', array(new toc_for_markdown, 'get_toc_filter'), 4);
//add_filter('get_the_excerpt', array(new toc_for_markdown, 'get_toc_filter'), 4);

?>