-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsyntax.php
343 lines (282 loc) · 10.8 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
/**
* Info Alphaindex: Displays the alphabetical index of a specified namespace.
*
* Version: 1.2
* last modified: 2006-06-14 12:00:00
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Hubert Molière <[email protected]>
* Modified by Nicolas H. <[email protected]>
* Modified by Jonesy <[email protected]>
* Modified by Samuele Tognini <[email protected]>
*/
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
require_once(DOKU_INC.'inc/search.php');
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_alphaindex extends DokuWiki_Syntax_Plugin {
function getType(){ return 'substition';}
function getAllowedTypes() { return array('baseonly', 'substition', 'formatting', 'paragraphs', 'protected'); }
function getPType() { return 'block'; }
/**
* Where to sort in?
*/
function getSort(){
return 139;
}
/**
* Connect pattern to lexer
*/
function connectTo($mode) {
$this->Lexer->addSpecialPattern('{{alphaindex>.+?}}',$mode,'plugin_alphaindex');
}
/**
* Handle the match
*/
function handle($match, $state, $pos, Doku_Handler $handler){
$level = 0;
$nons = true;
$match = substr($match, 13, -2);
// split namespaces
$match = preg_split('/\|/u', $match, 2);
// split level
$ns_opt = preg_split('/\#/u', $match[0], 2);
// namespace name
$ns = $ns_opt[0];
// add @NS@ option
if(empty($ns) || $ns == '@NS@') {
$pos = strrpos($ID,':');
if($pos != false){
$ns = substr($ID,0,$pos);
} else {
$ns = '.';
}
}
// level;
if (is_numeric($ns_opt[1])) {
$level = $ns_opt[1];
}
$match = explode(" ", $match[1]);
// namespaces option
$nons = in_array('nons', $match);
// multi-columns option
$incol = in_array('incol', $match);
return array($ns, array('level' => $level, 'nons' => $nons, 'incol' => $incol));
}
/**
* Render output
*/
function render($mode, Doku_Renderer $renderer, $data) {
global $conf;
if($mode == 'xhtml'){
$alpha_data = $this->_alpha_index($data, $renderer);
if ((!@$n)) {
if ($this->getConf('empty_msg')) {
$n = $this->getConf('empty_msg');
} else {
$n = 'No index for <b>{{ns}}</b>';
}
}
$alpha_data = str_replace('{{ns}}', cleanID($data[0]), $alpha_data);
$alpha_data = p_render('xhtml', p_get_instructions($alpha_data), $info);
// remove toc, section edit buttons and category tags
$patterns = array('!<div class="toc">.*?(</div>\n</div>)!s',
'#<!-- SECTION \[(\d*-\d*)\] -->#e',
'!<div class="category">.*?</div>!s');
$replace = array('','','');
$alpha_data = preg_replace($patterns, $replace, $alpha_data);
$renderer->doc .= '<div id="alphaindex_content">' ;
$renderer->doc .= $ns_data;
$renderer->doc .= '<hr />';
$renderer->doc .= $alpha_data;
$renderer->doc .= '</div>' ;
return true;
}
return false;
}
/**
* Return the alphabetical index
* @author Hubert MOLIERE <[email protected]>
*
* This function is a hack of Indexmenu _tree_menu($ns)
* @author Samuele Tognini <[email protected]>
*
* This function is a simple hack of DokuWiki html_index($ns)
* @author Andreas Gohr <[email protected]>
*/
function _alpha_index($myns, &$renderer) {
global $conf;
global $ID;
$ns = $myns[0];
$opts = $myns[1];
// Articles deletion configuration
$articlesDeletionPatterns = array();
if($this->getConf('articles_deletion')) {
$articlesDeletionPatterns = explode('|', $this->getConf('articles_deletion'));
$articlesDeletion = true;
} else {
$articlesDeletion = false;
}
// Hide pages configuration
$hidepages = array();
if($this->getConf('hidepages')) {
$hidepages = explode('|', $this->getConf('hidepages'));
}
// template configuration
if($this->getConf('title_tpl')) {
$titleTpl = $this->getConf('title_tpl');
} else {
$titleTpl = '===== Index =====';
}
if($this->getConf('begin_letter_tpl')) {
$beginLetterTpl = $this->getConf('begin_letter_tpl');
} else {
$beginLetterTpl = '==== {{letter}} ====';
}
if($this->getConf('entry_tpl')) {
$entryTpl = $this->getConf('entry_tpl');
} else {
$entryTpl = ' * [[{{link}}|{{name}}]]';
}
if($this->getConf('end_letter_tpl')) {
$endLetterTpl = $this->getConf('end_letter_tpl');
} else {
$endLetterTpl = '';
}
if($ns == '.') {
$ns = dirname(str_replace(':','/',$ID));
if ($ns == '.') $ns = '';
} else {
$ns = cleanID($ns);
}
$ns = utf8_encodeFN(str_replace(':','/',$ns));
$data = array();
// Searches for files below the given datadir and calls for every file the function alphaindex_search_index
search($data, $conf['datadir'], 'alphaindex_search_index', $opts, "/" . $ns);
$nb_data = count($data);
$alpha_data = array();
// alphabetical ordering
for($cpt = 0; $cpt <$nb_data; $cpt++) {
$tmpData = $data[$cpt]['id'];
$pos = strrpos(utf8_decode($tmpData), ':');
if($conf['useheading']) {
$pageName = p_get_first_heading($tmpData);
if($pageName == NULL) {
if($pos != FALSE) {
$pageName = utf8_substr($tmpData, $pos+1, utf8_strlen($tmpData));
} else {
$pageName = $tmpData;
}
$pageName = str_replace('_', ' ', $pageName);
}
} else {
if($pos != FALSE) {
$pageName = utf8_substr($tmpData, $pos+1, utf8_strlen($tmpData));
} else {
$pageName = $tmpData;
}
$pageName = str_replace('_', ' ', $pageName);
}
$pageNameArticle = '';
// if the current page is not a page to hide
if(!in_array($pageName, $hidepages)) {
// Articles deletion
if($articlesDeletion) {
foreach($articlesDeletionPatterns as $pattern) {
if(eregi($pattern, $pageName, $result)) {
$pageName = eregi_replace($pattern, '', $pageName);
$pageNameArticle = ucfirst(trim($result[0]));
}
}
}
// Fix for useheading - Decide if the heading is used or the pagename
if($this->getConf('metadata_title')) {
$tmp = p_get_metadata($data[$cpt]['id']);
if(isset($tmp['title'])) $pageName = $tmp['title'];
}
// R�cup�ration de la premi�re lettre du mot et classement
$firstLetter = utf8_deaccent(utf8_strtolower(utf8_substr($pageName, 0, 1)));
if(is_numeric($firstLetter)) {
if($this->getConf('numerical_index')) {
$firstLetter = $this->getConf('numerical_index');
} else {
$firstLetter = '0-9';
}
}
if($this->getConf('articles_moving')) {
$articleMoving = $this->getConf('articles_moving');
} else {
$articleMoving = 1;
}
if($articleMoving == 0) {
$pageName = $pageNameArticle.' '.$pageName;
} else if (($articleMoving == 1)&&($pageNameArticle != '')) {
$pageName = $pageName.' ('.$pageNameArticle.')';
}
$data[$cpt]['id2'] = ucfirst($pageName);
$alpha_data[$firstLetter][] = $data[$cpt];
}
}
// array sorting by key
ksort($alpha_data);
// Display of results
// alphabetical index
$alphaOutput .= $titleTpl . "\n";
$nb_data = count($alpha_data);
foreach($alpha_data as $key => $currentLetter) {
// Sorting of $currentLetter array
usort($currentLetter, create_function('$a, $b', "return strnatcasecmp(\$a['id2'], \$b['id2']);"));
$begin = str_replace("{{letter}}" ,utf8_strtoupper($key), $beginLetterTpl);
$alphaOutput .= $begin."\n";
foreach($currentLetter as $currentLetterEntry) {
$link = str_replace("{{link}}" ,$currentLetterEntry['id'], $entryTpl);
$alphaOutput .= str_replace("{{name}}" ,$currentLetterEntry['id2'], $link);
$alphaOutput .= "\n";
}
$end = str_replace("{{letter}}" ,utf8_strtoupper($key), $endLetterTpl);
$alphaOutput .= $end."\n";
}
return $alphaOutput;
}
} // Alphaindex class end
/**
* Build the browsable index of pages
*
* $opts['ns'] is the current namespace
*
* @author Andreas Gohr <[email protected]>
* modified by Samuele Tognini <[email protected]>
*/
function alphaindex_search_index(&$data, $base, $file, $type, $lvl, $opts) {
$return = true;
$item = array();
if($type == 'd'){
if ($opts['level'] == $lvl) $return = false;
if ($opts['nons']) return $return;
}elseif($type == 'f' && !preg_match('#\.txt$#',$file)){
// don't add the page
return false;
}
$id = pathID($file);
// check hidden
if($type=='f' && isHiddenPage($id)){
return false;
}
// check ACL
if($type == 'f' && auth_quickaclcheck($id) < AUTH_READ) {
return false;
}
// Set all pages at first level
if ($opts['nons']) {
$lvl = 1;
}
$data[] = array( 'id' => $id,
'type' => $type,
'level' => $lvl,
'open' => $return);
return $return;
}