<?php
/**
 * TOC Plugin: Inserts TOC where you need.
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andriy Lesyuk <andriy.lesyuk@softjourn.com>
 */

if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');

require_once(DOKU_PLUGIN.'syntax.php');

class syntax_plugin_toc extends DokuWiki_Syntax_Plugin {

    /**
     * Return some info
     */
    function getInfo() {
        return array(
            'author' => 'Andriy Lesyuk',
            'email'  => 'andriy.lesyuk@softjourn.com',
            'date'   => '2009-04-13',
            'name'   => 'TOC Syntax Plugin',
            'desc'   => 'Insert TOC where you need (disables original TOC)'
        );
    }

    /**
     * Tell the type of the plugin
     */
    function getType() {
        return 'substition';
    }

    /**
     * Return sort value (took the same as for ~~NOTOC~~)
     */
    function getSort() {
        return 30;
    }

    /**
     * Add our syntax
     */
    function connectTo($mode) {
        $this->Lexer->addSpecialPattern('~~TOC~~', $mode, 'plugin_toc');
    }

    /**
     * Process syntax code
     */
    function handle($match, $state, $pos, &$handler) {
        return substr($match, 2, -2);
    }

    /**
     * Add <!-- TOC --> to cached page (will be replaced by action component)
     */
    function render($mode, &$renderer, $data) {
        if ($mode == 'xhtml'){
            $renderer->doc .= '<!-- TOC -->';
            return true;
        } elseif ($mode == 'metadata') {
            $renderer->meta['movetoc'] = true;
            return true;
        } else {
            return false;
        }
    }

}
