<?php
    /**
    * simpleindex:  A simple navigation plugin adapted from dokuwiki's built-in index function
    * @license:     GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
    * @author:      lainme <lainme993@gmail.com>
    */

    if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
    if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
    require_once(DOKU_PLUGIN.'syntax.php');
    require_once (DOKU_INC.'inc/search.php');

    class syntax_plugin_simpleindex extends DokuWiki_Syntax_Plugin {
        function getInfo(){
            return array(
	        'author' => 'lainme',
                'email'  => 'lainme993@gmail.com',
                'date'   => '2010-05-16',
                'name'   => 'simpleindex',
                'desc'   => 'A simple navigation plugin adapted from dokuwiki\'s built-in index function',
                'url'    => 'http://www.dokuwiki.org/plugin:simpleindex',
            );
        }
        function getType() { return 'substition';}
        function getSort() { return 705;}
        function getPType(){return 'block';}

        function connectTo($mode) {
            $this->Lexer->addSpecialPattern('<simpleindex[^>]*>',$mode,'plugin_simpleindex');
        }
        function handle($match, $state, $pos, &$handler){
            $return=array();
            $match = mb_substr($match,13,-1);
            if (!empty($match)){
                $match=explode(",",$match);
                    foreach ($match as $value)
                        $return[]=$value;
            }
            return $return;
        }
        function render($mode, &$renderer, $data) {
            if($mode == 'xhtml'){
                global $conf;
                global $ACT;
                if( $ACT != 'index'){
                    $ns = $_SERVER['PHP_SELF'];
                    $ns = preg_replace('/.*doku.php\//','',$ns);
                    $ns = preg_replace('/\/[^\/]*$/','',$ns);
                    $renderer->doc.= '<div id="index__tree">';
                    $result = array();
                    search($result,$conf['datadir'],'search_index',array('ns' => $ns));
                    simpleindex_del($result,$data);
                    $renderer->doc.=html_buildlist($result,'idx','html_list_index','html_li_index');
                    $renderer->doc.= '</div>';
                }
                return true;
            }
            return false;
        }
    }
    function simpleindex_del(&$result,$dels){
        foreach ($dels as $del){
            $temp=$result;
            $result=array();
            foreach ($temp as $value)
                if (stripos($value['id'],$del)!==0)
                    $result[]=$value;
        }
    }
?>

