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