xref: /plugin/simplenavi/syntax.php (revision ba93e3e3b70948fad8595ebebac44a2cf961d23f)
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';
17require_once DOKU_INC.'inc/search.php';
18
19class syntax_plugin_simplenavi extends DokuWiki_Syntax_Plugin {
20    function getType() {
21        return 'substition';
22    }
23
24    function getPType() {
25        return 'block';
26    }
27
28    function getSort() {
29        return 155;
30    }
31
32
33    function connectTo($mode) {
34        $this->Lexer->addSpecialPattern('{{simplenavi>[^}]*}}',$mode,'plugin_simplenavi');
35    }
36
37    function handle($match, $state, $pos, &$handler){
38        $data = array(cleanID(substr($match,13,-2)));
39
40        return $data;
41    }
42
43    function render($mode, &$R, $pass) {
44        if($mode != 'xhtml') return false;
45
46        global $conf;
47        global $INFO;
48        $R->info['cache'] = false;
49
50        $ns = utf8_encodeFN(str_replace(':','/',$pass[0]));
51        $data = array();
52        search($data,$conf['datadir'],array($this,'_search'),array('ns' => $INFO['id']),$ns);
53        uksort($data, array($this, '_cmp'));
54
55        $R->doc .= '<div class="plugin__simplenavi">';
56        $R->doc .= html_buildlist($data,'idx',array($this,'_list'),array($this,'_li'));
57        $R->doc .= '</div>';
58
59        return true;
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'],$this->_title($item['id'])).'</strong>';
67        }else{
68            return html_wikilink(':'.$item['id'],$this->_title($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[$id]=array( 'id'    => $id,
132                       'type'  => $type,
133                       'level' => $lvl,
134                       'open'  => $return);
135        return $return;
136    }
137
138    function _title($id) {
139        global $conf;
140
141        if(useHeading('navigation')){
142            $p = p_get_first_heading($id);
143        }
144        if($p) return $p;
145
146        $p = noNS($id);
147        if ($p == $conf['start'] || $p == false) {
148            $p = noNS(getNS($id));
149            if ($p == false) {
150                return $conf['start'];
151            }
152        }
153        return $p;
154    }
155
156    function _cmp($a, $b) {
157        global $conf;
158
159        $a = preg_replace('/:'.$conf['start'].'$/', '', $a);
160        $b = preg_replace('/:'.$conf['start'].'$/', '', $b);
161
162        return strcmp($a, $b);
163    }
164}
165
166// vim:ts=4:sw=4:et:
167