-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsyntax.php
388 lines (333 loc) · 11.9 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
/**
* Plugin Button : Add button with image support syntax for links
*
* To be run with Dokuwiki only
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Rémi Peyronnet <[email protected]>
Full Syntax :
[[{namespace:image|extra css}wiki page|Title of the link]]
All fields optional, minimal syntax:
[[{}Simple button]]
Configuration :
[[{conf.styles}style|css]]
[[{conf.target}style|target]]
19/05/2013 : Initial release
20/04/2014 : Added target support (feature request from Andrew St Hilaire)
07/06/2014 : Added dokuwiki formatting support in title section (not working in wiki page section) (feature request from Willi Lethert)
30/08/2014 : Added toolbar button (contribution from Xavier Decuyper) and fixed local anchor (bug reported by Andreas Kuzma)
06/09/2014 : Refactored to add backlinks support (feature request from Schümmer Hans-Jürgen)
28/04/2015 : Refactored global config handling, add internal media link support, add escaping of userinput (contribution from Peter Stumm https://github.com/lisps/plugin-button)
*/
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
class syntax_plugin_button extends DokuWiki_Syntax_Plugin {
function getType() { return 'substition'; }
function getPType() { return 'normal'; }
function getSort() { return 25; } // Internal link is 300
// function connectTo($mode) { $this->Lexer->addSpecialPattern('\[\[{[^}]*}[^\]]*\]\]',$mode,'plugin_button'); }
function connectTo($mode) {
$this->Lexer->addSpecialPattern('\[\[{conf[^}]*}[^\]]*\]\]',$mode,'plugin_button');
$this->Lexer->addEntryPattern('\[\[{[^}]*}[^\]\|]*\|?',$mode,'plugin_button');
$this->Lexer->addExitPattern(']]','plugin_button');
}
function postConnect() { }
function getAllowedTypes() { return array('formatting','substition'); }
protected $confStyles;
protected $styles = array();
protected $targets = array();
protected function setStyle($name,$value) {
global $ID;
$this->styles[$ID][$name] = $value;
}
protected function getStyle($name) {
global $ID;
return isset($this->styles[$ID][$name]) ? $this->styles[$ID][$name] : $this->getConfStyles($name);
}
protected function hasStyle($name) {
global $ID;
return (is_array($this->styles[$ID]) && array_key_exists($name,$this->styles[$ID]))
|| $this->getConfStyles($name) ? true : false;
}
protected function getConfStyles($name = null) {
if($this->confStyles === null) {
$this->confStyles = array();
$styles = $this->getConf('styles');
if(!$styles) return;
$styles = explode("\n", $styles);
if(!is_array($styles)) return;
foreach ($styles as $style) {
$style = trim($style);
if(!$style) continue;
$style = explode('|', $style,2);
if(!is_array($style) || !$style[0] || !$style[1]) continue;
$this->confStyles[trim($style[0])] = trim($style[1]);
}
//dbg($this->confStyles);
}
if($name) {
if(!isset($this->confStyles[$name])) return false;
return $this->confStyles[$name];
}
return $this->confStyles;
}
protected function setTarget($name,$value) {
global $ID;
$this->targets[$ID][$name] = $value;
}
protected function getTarget($name) {
global $ID;
return $this->targets[$ID][$name];
}
protected function hasTarget($name) {
global $ID;
return (is_array($this->targets[$ID]) && array_key_exists($name,$this->targets[$ID])) ? true : false;
}
function handle($match, $state, $pos, &$handler)
{
global $plugin_button_styles;
global $plugin_button_target;
switch ($state) {
case DOKU_LEXER_SPECIAL :
case DOKU_LEXER_ENTER :
$data = '';
// Button
if (preg_match('/\[\[{(?<image>[^}\|]*)\|?(?<css>[^}]*)}(?<link>[^\]\|]*)\|?(?<title>[^\]]*)/', $match, $matches))
{
$data = $matches;
}
if (is_array($data))
{
if ($data['image'] == 'conf.styles')
{
$this->setStyle($data['link'],$data['title']);
}
else if ($data['image'] == 'conf.target')
{
$this->setTarget($data['link'],$data['title']);
}
else
{
$data['target'] = "";
if ($this->hasTarget($data['css']))
{
$data['target'] = $this->getTarget($data['css']);
}
else if ($this->hasTarget('default'))
{
$data['target'] = $this->getTarget('default');
}
if ($data['css'] != "" && $this->hasStyle($data['css']))
{
$data['css'] = $this->getStyle($data['css']);
}
if ($this->hasStyle('default') && ($data['css'] != 'default'))
{
$data['css'] = $this->getStyle('default') .' ; '. $data['css'];
}
}
}
return array($state, $data);
case DOKU_LEXER_UNMATCHED : return array($state, $match);
case DOKU_LEXER_ENTRY : return array($state, '');
case DOKU_LEXER_EXIT : return array($state, '');
}
return array();
}
function render($mode, &$renderer, $data)
{
global $plugin_button_styles;
global $plugin_button_target;
if($mode == 'xhtml'){
list($state, $match) = $data;
switch ($state) {
case DOKU_LEXER_SPECIAL:
case DOKU_LEXER_ENTER:
if (is_array($match))
{
$image = $match['image'];
if (($image != "conf.target") && ($image != "conf.styles"))
{
// Test if internal or external link (from handler.php / internallink)
if (preg_match('#^([a-z0-9\-\.+]+?)://#i',$match['link']))
{
// External
$link['url'] = $match['link'];
$link['name'] = $match['title'];
if ($link['name'] == "") $link['name'] = $match['link'];
$link['class'] = 'urlextern';
}
else
{
// Internal
$link = $this->dokuwiki_get_link($renderer, $match['link'], $match['title']);
}
$target = $match['target'];
if($target) $target = " target ='" .hsc($target). "' ";
$link['name'] = str_replace('\\\\','<br />', $link['name']); //textbreak support
if ($image != '')
{
$image = Doku_Handler_Parse_Media("{{".$image."}}");
$image = "<span class='plugin_button_image'>". $this->internalmedia($renderer,$image['src'],null,null,$image['width'],$image['height'])['name'] ."</span>";
}
$text = "<a ".$target." href='".$link['url']."'><span class='plugin_button' style='".hsc($match['css'])."'>$image<span class='plugin_button_text ${link['class']}'>";
if (substr($match[0],-1) != "|") $text .= $link['name'];
$renderer->doc .= $text;
}
}
break;
case DOKU_LEXER_UNMATCHED : $renderer->doc .= $renderer->_xmlEntities($match); break;
case DOKU_LEXER_EXIT : $renderer->doc .= "</span></span></a>"; break;
}
return true;
}
elseif ($mode=='metadata')
{
list($state, $match) = $data;
switch ($state) {
case DOKU_LEXER_SPECIAL:
case DOKU_LEXER_ENTER:
if (is_array($match))
{
/** @var Doku_Renderer_metadata $renderer */
$renderer->internallink($match['link']);
// I am assuming that when processing in handle(), you have stored
// the link destination in $data[0]
return true;
}
break;
case DOKU_LEXER_UNMATCHED : break;
case DOKU_LEXER_EXIT : break;
}
return true;
}
return false;
}
function dokuwiki_get_link(&$xhtml, $id, $name = NULL) {
global $ID;
$resolveid = $id; // To prevent resolve_pageid to change $id value
resolve_pageid(getNS($ID),$resolveid,$exists); //page file?
if($exists) {
return $this->internallink($xhtml,$id,$name);
}
$resolveid = $id;
resolve_mediaid(getNS($ID),$resolveid,$exists); //media file?
if($exists) {
return $this->internalmedia($xhtml,$id,$name);
} else {
return $this->internallink($xhtml,$id,$name);
}
}
// Copied and adapted from inc/parser/xhtml.php, function internallink (see RPHACK)
// Should use wl instead (from commons), but this won't do the trick for the name
function internallink(&$xhtml, $id, $name = NULL, $search=NULL,$returnonly=false,$linktype='content')
{
global $conf;
global $ID;
global $INFO;
$params = '';
$parts = explode('?', $id, 2);
if (count($parts) === 2) {
$id = $parts[0];
$params = $parts[1];
}
// For empty $id we need to know the current $ID
// We need this check because _simpleTitle needs
// correct $id and resolve_pageid() use cleanID($id)
// (some things could be lost)
if ($id === '') {
$id = $ID;
}
// RPHACK for get_link to work with local links '#id'
if (substr($id, 0, 1) === '#') {
$id = $ID . $id;
}
// -------
// default name is based on $id as given
$default = $xhtml->_simpleTitle($id);
// now first resolve and clean up the $id
resolve_pageid(getNS($ID),$id,$exists);
$name = $xhtml->_getLinkTitle($name, $default, $isImage, $id, $linktype);
if ( !$isImage ) {
if ( $exists ) {
$class='wikilink1';
} else {
$class='wikilink2';
$link['rel']='nofollow';
}
} else {
$class='media';
}
//keep hash anchor
list($id,$hash) = explode('#',$id,2);
if(!empty($hash)) $hash = $xhtml->_headerToLink($hash);
//prepare for formating
$link['target'] = $conf['target']['wiki'];
$link['style'] = '';
$link['pre'] = '';
$link['suf'] = '';
// highlight link to current page
if ($id == $INFO['id']) {
$link['pre'] = '<span class="curid">';
$link['suf'] = '</span>';
}
$link['more'] = '';
$link['class'] = $class;
$link['url'] = wl($id, $params);
$link['name'] = $name;
$link['title'] = $id;
//add search string
if($search){
($conf['userewrite']) ? $link['url'].='?' : $link['url'].='&';
if(is_array($search)){
$search = array_map('rawurlencode',$search);
$link['url'] .= 's[]='.join('&s[]=',$search);
}else{
$link['url'] .= 's='.rawurlencode($search);
}
}
//keep hash
if($hash) $link['url'].='#'.$hash;
return $link;
//output formatted
//if($returnonly){
// return $this->_formatLink($link);
//}else{
// $this->doc .= $this->_formatLink($link);
//}
}
function internalmedia (&$xhtml, $src, $title=NULL, $align=NULL, $width=NULL,
$height=NULL, $cache=NULL, $linking=NULL) {
global $ID;
list($src,$hash) = explode('#',$src,2);
resolve_mediaid(getNS($ID),$src, $exists);
$noLink = false;
$render = ($linking == 'linkonly') ? false : true;
$link = $xhtml->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
list($ext,$mime,$dl) = mimetype($src,false);
if(substr($mime,0,5) == 'image' && $render){
$link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct'));
}elseif($mime == 'application/x-shockwave-flash' && $render){
// don't link flash movies
$noLink = true;
}else{
// add file icons
$class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
$link['class'] .= ' mediafile mf_'.$class;
$link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true);
if ($exists) $link['title'] .= ' (' . filesize_h(filesize(mediaFN($src))).')';
}
if($hash) $link['url'] .= '#'.$hash;
//markup non existing files
if (!$exists) {
$link['class'] .= ' wikilink2';
}
return $link;
//output formatted
// if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
// else $this->doc .= $this->_formatLink($link);
}
}
?>