xref: /plugin/simplenavi/syntax.php (revision 492ddc4e56f02a8f1bad2a6fff64a187b268db61)
1<?php
2/**
3 * DokuWiki Plugin simplenavi (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once DOKU_PLUGIN.'syntax.php';
17
18class syntax_plugin_simplenavi extends DokuWiki_Syntax_Plugin {
19    function getType() {
20        return 'substition';
21    }
22
23    function getPType() {
24        return 'block';
25    }
26
27    function getSort() {
28        return 155;
29    }
30
31
32    function connectTo($mode) {
33        $this->Lexer->addSpecialPattern('{{simplenavi>[^}]*}}',$mode,'plugin_simplenavi');
34    }
35
36    function handle($match, $state, $pos, &$handler){
37        $data = array(cleanID(substr($match,13,-2)));
38
39        return $data;
40    }
41
42    function render($mode, &$R, $pass) {
43        if($mode != 'xhtml') return false;
44
45        global $conf;
46        global $INFO;
47        $R->info['cache'] = false;
48
49        $ns = utf8_encodeFN(str_replace(':','/',$pass[0]));
50        $data = array();
51        search($data,$conf['datadir'],array($this,'_search'),array('ns' => $INFO['id']),$ns);
52
53        $R->doc .= '<div class="plugin__simplenavi">';
54        $R->doc .= html_buildlist($data,'idx',array($this,'_list'),array($this,'_li'));
55        $R->doc .= '</div>';
56
57        return true;
58    }
59
60
61
62    function _list($item){
63        global $INFO;
64
65        if(($item['type'] == 'd' && $item['open']) || $INFO['id'] == $item['id']){
66            return '<strong>'.html_wikilink(':'.$item['id']).'</strong>';
67        }else{
68            return html_wikilink(':'.$item['id']);
69        }
70
71    }
72
73    function _li($item){
74        if($item['type'] == "f"){
75            return '<li class="level'.$item['level'].'">';
76        }elseif($item['open']){
77            return '<li class="open">';
78        }else{
79            return '<li class="closed">';
80        }
81    }
82
83    function _search(&$data,$base,$file,$type,$lvl,$opts){
84        global $conf;
85        $return = true;
86
87        $item = array();
88
89        $id = pathID($file);
90
91        if($type == 'd' && !(
92            preg_match('#^'.$id.'(:|$)#',$opts['ns']) ||
93            preg_match('#^'.$id.'(:|$)#',getNS($opts['ns']))
94
95        )){
96            //add but don't recurse
97            $return = false;
98        }elseif($type == 'f' && ($opts['nofiles'] || substr($file,-4) != '.txt')){
99            //don't add
100            return false;
101        }
102
103        if($type=='d' && $conf['sneaky_index'] && auth_quickaclcheck($id.':') < AUTH_READ){
104            return false;
105        }
106
107        if($type == 'd'){
108            // link directories to their start pages
109            $exists = false;
110            $id = "$id:";
111            resolve_pageid('',$id,$exists);
112            $this->startpages[$id] = 1;
113        }elseif($this->startpages[$id]){
114            // skip already shown start pages
115            return false;
116        }elseif(noNS($id) == $conf['start']){
117            // skip the main start page
118            return false;
119        }
120
121        //check hidden
122        if(isHiddenPage($id)){
123            return false;
124        }
125
126        //check ACL
127        if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
128            return false;
129        }
130
131        $data[]=array( 'id'    => $id,
132                       'type'  => $type,
133                       'level' => $lvl,
134                       'open'  => $return);
135        return $return;
136    }
137
138
139}
140
141// vim:ts=4:sw=4:et:enc=utf-8:
142