1<?php
2/**
3 *  Indexmenu2 plugin, Navigation component
4 *
5 *  $Id: navigation.php 93 2007-05-07 11:56:33Z wingedfox $
6 *  $HeadURL: https://svn.debugger.ru/repos/common/DokuWiki/Indexmenu2/tags/Indexmenu2.v2.1.2/syntax/navigation.php $
7 *
8 *  Syntax:     <navigation indexmenu2_options>
9 *               * list
10 *               * [[of links]]
11 *               * {{to be shown}}
12 *               * {{indexmenu>:in:the:navigation}}
13 *              </navigation>
14 *
15 *  @lastmodified $Date: 2007-05-07 15:56:33 +0400 (Пнд, 07 Май 2007) $
16 *  @license      LGPL 2 (http://www.gnu.org/licenses/lgpl.html)
17 *  @author       Ilya Lebedev <ilya@lebedev.net>
18 *  @version      $Rev: 93 $
19 *  @copyright    (c) 2005-2007, Ilya Lebedev
20 */
21
22if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
23if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
24require_once(DOKU_PLUGIN.'syntax.php');
25require_once(DOKU_PLUGIN.'indexmenu/syntax/indexmenu.php');
26
27/**
28 * All DokuWiki plugins to extend the parser/rendering mechanism
29 * need to inherit from this class
30 */
31class syntax_plugin_indexmenu_navigation extends DokuWiki_Syntax_Plugin {
32
33    var $syntax = "";
34
35    /**
36     * return some info
37     */
38    function getInfo(){
39        preg_match("#^.+Indexmenu2[/.]([^\\/]+)#"," $HeadURL: https://svn.debugger.ru/repos/common/DokuWiki/Indexmenu2/tags/Indexmenu2.v2.1.2/syntax/navigation.php $ ", $v);
40        $v = preg_replace("#.*?((trunk|\.v)[\d.]+)#","\\1",$v[1]);
41        $b = preg_replace("/\\D/","", " $Rev: 93 $ ");
42
43        return array( 'author' => 'Ilya Lebedev'
44                     ,'email'  => 'ilya@lebedev.net'
45                     ,'date'   => preg_replace("#.*?(\d{4}-\d{2}-\d{2}).*#","\\1",'$Date: 2007-05-07 15:56:33 +0400 (Пнд, 07 Май 2007) $')
46                     ,'name'   => "Indexmenu2 Navigation module {$v}.$b"
47                     ,'desc'   => 'Module builds navigation menu from any unordered list, with optionally nested indexmenu sections'
48                     ,'url'    => 'https://www.dokuwiki.org/plugin:indexmenu2'
49                    );
50    }
51
52    function getType(){ return 'protected';}
53    function getPType(){ return 'block'; }
54
55    // must return a number lower than returned by native 'file' mode (210)
56    function getSort(){ return 138; }
57
58
59    /**
60     * Connect pattern to lexer
61     */
62    function connectTo($mode) {
63        $this->Lexer->addEntryPattern("<navigation(?=[^\r\n]*?\x3E.*?\x3C/navigation\x3E)",$mode,'plugin_indexmenu_navigation');
64    }
65
66    function postConnect() {
67        $this->Lexer->addExitPattern('\x3C/navigation\x3E', 'plugin_indexmenu_navigation');
68    }
69
70    /**
71     * Handle the match
72     */
73    function handle($match, $state, $pos, &$handler){
74
75        switch ($state) {
76            case DOKU_LEXER_ENTER:
77                $this->syntax = substr($match, 1);
78                return false;
79
80            case DOKU_LEXER_UNMATCHED:
81                // will include everything from <code ... to ... </code >
82                // e.g. ... [lang] [|title] > [content]
83                list($opts, $content) = preg_split('/>/u',trim($match),2);
84
85                return array(true, $content, $opts);
86        }
87        return false;
88    }
89
90    /**
91     * Create output
92     */
93    function render($mode, &$renderer, $data) {
94        switch ($mode) {
95            case 'xhtml' :
96                if (true === $data[0]) {
97                    list(,$content, $opts) = $data;
98                    $opts = syntax_plugin_indexmenu_indexmenu::parseOptions($opts);
99
100                    $instr = p_get_instructions($content);
101                    $this->__cleanInstructions ($instr,$opts);
102                    /*
103                    *  remove empty menu includes and wrap text with anchors, if menu item has a submenu
104                    */
105                    /*
106                    $instr = preg_replace( "#(<li[^>]*>)([^<]*)(<(ul))#"
107                                          ,"\\1<a href=\"#\" class=\"wikilink1\" title=\"\\2\">\\2</a>\\3"
108                                          ,preg_replace( "#</li>\\s*<li[^>]*>\\s*<ul#"
109                                                        ,"<ul"
110                                                        ,p_render($mode, $instr, $info)));
111                    /**/
112                    $info = null;
113                    $instr = preg_replace( "#<li[^>]*>\s*<li#"
114                                          ,"<li"
115                                          ,preg_replace( "#</li>\\s*</li#"
116                                                        ,"</li"
117                                                        ,p_render($mode, $instr, $info)));
118                    $renderer->doc .= syntax_plugin_indexmenu_indexmenu::getHTML ($opts[1], $instr);
119
120                    return true;
121                }
122                break;
123            default:
124                break;
125        }
126        return false;
127    }
128    /**
129     *  Clean instructions from unallowed stuff
130     *
131     *  @author Ilya Lebedev <ilya@lebedev.net>
132     *  @param $instr mixed list of instructions
133     *  @param $opts mixed list of menu options
134     *  @return mixed filtered list
135     *  @access private
136     */
137    function __cleanInstructions (&$instr, $opts) {
138        /*
139        *  use this flag to skip allowed data in disallowed places
140        */
141        $li_open = false;
142        foreach ($instr as $k=>$v) {
143            switch ($v[0]) {
144                case "document_start":
145                case "document_end":
146                case "listu_close":
147                case "listu_open":
148                case "listitem_close":
149                    $li_open = false;
150                    break;
151                case "listitem_open":
152                    $li_open = true;
153                    break;
154                case "plugin":
155                    if (!$li_open) {
156                        unset($instr[$k]);
157                        break;
158                    }
159                    if ('indexmenu_indexmenu' === $v[1][0]) {
160                        $instr[$k][1][1][1]['js'] = false;
161                        $instr[$k][1][1][1]['ajax'] = $opts[1]['ajax'];
162                        $instr[$k][1][1][1]['navigation'] = true;
163                    }
164                    break;
165                default:
166                    if (!$li_open) unset($instr[$k]);
167                    break;
168                case "listcontent_open" :
169                case "listcontent_close" :
170                    unset($instr[$k]);
171                    break;
172            }
173        }
174    }
175}
176
177//Setup VIM: ex: et ts=4 enc=utf-8 :
178