xref: /plugin/simplenavi/syntax.php (revision 306670aefbbe93abdda4b975acf0722511d1aa45)
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,1,'natural');
53        if ($this->getConf('sort') == 'ascii') {
54            uksort($data, array($this, '_cmp'));
55        }
56
57        $R->doc .= '<div class="plugin__simplenavi">';
58        $R->doc .= html_buildlist($data,'idx',array($this,'_list'),array($this,'_li'));
59        $R->doc .= '</div>';
60
61        return true;
62    }
63
64    function _list($item){
65        global $INFO;
66
67        if(($item['type'] == 'd' && $item['open']) || $INFO['id'] == $item['id']){
68            return '<strong>'.html_wikilink(':'.$item['id'],$this->_title($item['id'])).'</strong>';
69        }else{
70            return html_wikilink(':'.$item['id'],$this->_title($item['id']));
71        }
72
73    }
74
75    function _li($item){
76        if($item['type'] == "f"){
77            return '<li class="level'.$item['level'].'">';
78        }elseif($item['open']){
79            return '<li class="open">';
80        }else{
81            return '<li class="closed">';
82        }
83    }
84
85    function _search(&$data,$base,$file,$type,$lvl,$opts){
86        global $conf;
87        $return = true;
88
89        $item = array();
90
91        $id = pathID($file);
92
93        if($type == 'd' && !(
94            preg_match('#^'.$id.'(:|$)#',$opts['ns']) ||
95            preg_match('#^'.$id.'(:|$)#',getNS($opts['ns']))
96
97        )){
98            //add but don't recurse
99            $return = false;
100        }elseif($type == 'f' && ($opts['nofiles'] || substr($file,-4) != '.txt')){
101            //don't add
102            return false;
103        }
104
105        if($type=='d' && $conf['sneaky_index'] && auth_quickaclcheck($id.':') < AUTH_READ){
106            return false;
107        }
108
109        if($type == 'd'){
110            // link directories to their start pages
111            $exists = false;
112            $id = "$id:";
113            resolve_pageid('',$id,$exists);
114            $this->startpages[$id] = 1;
115        }elseif($this->startpages[$id]){
116            // skip already shown start pages
117            return false;
118        }elseif(noNS($id) == $conf['start']){
119            // skip the main start page
120            return false;
121        }
122
123        //check hidden
124        if(isHiddenPage($id)){
125            return false;
126        }
127
128        //check ACL
129        if($type=='f' && auth_quickaclcheck($id) < AUTH_READ){
130            return false;
131        }
132
133        $data[$id]=array( 'id'    => $id,
134                       'type'  => $type,
135                       'level' => $lvl,
136                       'open'  => $return);
137        return $return;
138    }
139
140    function _title($id) {
141        global $conf;
142
143        if(useHeading('navigation')){
144            $p = p_get_first_heading($id);
145        }
146        if($p) return $p;
147
148        $p = noNS($id);
149        if ($p == $conf['start'] || $p == false) {
150            $p = noNS(getNS($id));
151            if ($p == false) {
152                return $conf['start'];
153            }
154        }
155        return $p;
156    }
157
158    function _cmp($a, $b) {
159        global $conf;
160
161        $a = preg_replace('/:'.preg_quote($conf['start'], '/').'$/', '', $a);
162        $b = preg_replace('/:'.preg_quote($conf['start'], '/').'$/', '', $b);
163
164        return strcmp($a, $b);
165    }
166}
167
168// vim:ts=4:sw=4:et:
169