1<?php
2/**
3* DokuWiki Plugin dirtylittlehelper (Syntax Component)
4*
5* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6* @author  KalleAPunkt
7*/
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11die();
12}
13
14class syntax_plugin_dirtylittlehelper_tree extends DokuWiki_Syntax_Plugin
15{
16        var $dlh_tree_html  = '';
17        var $dlh_tree_no    = false;
18        var $dlh_tree_count = 0;
19        var $dlh_tree_force = false;
20
21        /**
22        * @return string Syntax mode type
23        */
24        public function getType()
25        {
26                return 'substition';
27        }
28
29        /**
30        * @return string Paragraph type
31        */
32        public function getPType()
33        {
34                return 'normal';
35        }
36
37        /**
38        * @return int Sort order - Low numbers go before high numbers
39        */
40        public function getSort()
41        {
42                return 150;
43        }
44
45        /**
46        * Connect lookup pattern to lexer.
47        *
48        * @param string $mode Parser mode
49        */
50        public function connectTo($mode)
51        {
52                $this->Lexer->addSpecialPattern('\<dlh\.tree\>',$mode,'plugin_dirtylittlehelper_'.$this->getPluginComponent());
53                $this->Lexer->addSpecialPattern('\<dlh\.notree\>',$mode,'plugin_dirtylittlehelper_'.$this->getPluginComponent());
54                $this->Lexer->addSpecialPattern('\<dlh\.forcetree\>',$mode,'plugin_dirtylittlehelper_'.$this->getPluginComponent());
55        }
56
57
58
59        /**
60        * Handle matches of the DLH syntax
61        *
62        * @param string       $match   The match of the syntax
63        * @param int          $state   The state of the handler
64        * @param int          $pos     The position in the document
65        * @param Doku_Handler $handler The handler
66        *
67        * @return array Data for the renderer
68        */
69        public function handle($match, $state, $pos, Doku_Handler $handler)
70        {
71
72                switch ($state) {
73
74                        case DOKU_LEXER_SPECIAL:
75
76                                if ($match == '<dlh.tree>'){
77									return array( $state, 'TREE',$match);
78
79                                }elseif(  $match == '<dlh.notree>'){
80									$this->dlh_tree_no = true;
81									return array($state, 'NOTREE',$match);
82
83								}elseif(  $match == '<dlh.forcetree>'){
84									$this->dlh_tree_no = true;
85									$this->dlh_tree_force = true;
86									return array($state, 'FORCETREE',$match);
87                                }
88
89						break;
90
91                }
92        return false;
93
94        } //handle
95
96
97        /**
98        * Render xhtml output or metadata
99        *
100        * @param string        $mode     Renderer mode (supported modes: xhtml)
101        * @param Doku_Renderer $renderer The renderer
102        * @param array         $data     The data from the handler() function
103        *
104        * @return bool If rendering was successful.
105        */
106        public function render($mode, Doku_Renderer $renderer, $data)
107        {
108                if ($mode == 'xhtml') {
109
110                        if(  $data[1]=='TREE' || $data[1] == 'FORCETREE' ){
111
112							// if the tree is not build yet - do it
113							if( $this->dlh_tree_html == ''){
114
115								$this->dlh_dokubook_tree( substr($_SERVER['PATH_INFO'],1) );
116								$this->dlh_tree_html .= '<script>'
117															.' document.addEventListener("DOMContentLoaded", function(event) { '
118																.' /* attach the AJAX index to the sidebar index */ '
119																.' var sb_dw_index = jQuery(\'#left__index__tree\').dw_tree({deferInit: true, '
120																.' load_data: function  (show_sublist, $clicky) { '
121																.' jQuery.post( '
122																.' DOKU_BASE + \'lib/exe/ajax.php\', '
123																.' $clicky[0].search.substr(1) + \'&call=index\', '
124																.' show_sublist, \'html\' '
125																.' ); '
126																.' } '
127																.' }); '
128																.'  '
129																.' var $tree = jQuery(\'#sb__index__tree\'); '
130																.' sb_dw_index.$obj = $tree; '
131																.' sb_dw_index.init(); '
132																.'  '
133																.' '
134																.' jQuery(\'.dlh_ontheleft *\').each( '
135																.'     function(){ '
136																.'         if(  JSINFO[\'id\'].indexOf( jQuery(this).attr(\'title\')+\':\' ) == 0 '
137																.'           || JSINFO[\'id\'] == jQuery(this).attr(\'title\') '
138																.'             ){ '
139																.'             jQuery(this).css({\'color\':\'BLACK\'}); '
140																.'         } '
141																.'     }     '
142																.'  '
143																.'  );'
144																.'  '
145																.' }); '
146																.' </script> '
147																.' ';
148
149							}// build tree?
150
151							//normal tree
152							if( $data[1] == 'TREE' ){
153								if( $this->dlh_tree_no == false && $this->dlh_tree_force == false){
154									if( $this->dlh_tree_count == 0){
155										$renderer->doc .= $this->dlh_tree_html;
156										$this->dlh_tree_count =1;
157										return true;
158									}else{
159										$renderer->doc .= '<div class="dlh_one_tree_only">! one tree only !</div>';
160										return true;
161									}//count==0?
162								}else{
163									//no tree to build...
164									return true;
165								}//notree & forcetree are false
166							}//normal tree
167
168							if( $data[1] == 'FORCETREE'){
169								if( $this->dlh_tree_count == 0){
170									$renderer->doc .= $this->dlh_tree_html;
171									$this->dlh_tree_count =1;
172									return true;
173								}else{
174									$renderer->doc .= '<div class="dlh_one_tree_only">! one tree only !</div>';
175									return true;
176								}//count==0?
177							}
178
179
180
181
182                        }elseif($data[1]=='NOTREE'){
183                                $renderer->doc .= '';
184								return true;
185                        }
186
187
188                        return false;
189
190
191                } //mode xhtml
192
193        return false;
194
195        } //function render
196
197
198        function dlh_dokubook_p_index_xhtml($ns) {
199
200                require_once(DOKU_INC.'inc/search.php');
201
202                global $conf;
203                global $ID;
204
205                $dir = $conf['datadir'];
206
207                $ns  = cleanID($ns);
208
209                #fixme use appropriate function
210                if(empty($ns)){
211
212                        $ns = dirname(str_replace(':','/',$ID));
213
214                        if($ns == '.') $ns ='';
215
216                }
217
218                $ns  = utf8_encodeFN(str_replace(':','/',$ns));
219
220                // only extract headline
221                preg_match('/<h1>.*?<\/h1>/', p_locale_xhtml('index'), $match);
222                $this->dlh_tree_html .=  $match[0];
223
224                $data = array();
225                search($data,$conf['datadir'],'search_index',array('ns' => $ns));
226
227                $this->dlh_tree_html .=   '<div class="dlh_ontheleft"><div id="sb__index__tree">' . DOKU_LF;
228                $this->dlh_tree_html .=   html_buildlist($data,'idx','html_list_index','html_li_index');
229                $this->dlh_tree_html .=   '</div></div>' . DOKU_LF;
230
231        } //dlh_dokubook_p_index_xhtml
232
233
234
235
236        function dlh_dokubook_tree( $dlh_target=false ){
237
238                global $lang;
239                global $ID;
240                global $INFO;
241
242                if( $dlh_target !== false){
243                $svID  = cleanID($dlh_target);
244                }else{
245                $svID  = cleanID(':');
246                }
247
248                $this->dlh_tree_html .=   '<span class="sb_label">' . $lang['navigation'] . '</span>' . DOKU_LF;
249                $this->dlh_tree_html .=   '<aside id="navigation" class="sidebar_box">' . DOKU_LF;
250                $this->dlh_tree_html .=   $this->dlh_dokubook_p_index_xhtml(cleanID($svID));
251                $this->dlh_tree_html .=   '</aside>' . DOKU_LF;
252
253        } //function dlh_dokubook_tree
254
255} //class
256
257
258
259
260
261
262