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)
*/
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
// Copied and adapted from inc/parser/xhtml.php, function internallink
// Should use wl instead (from commons), but this won't do the trick for the name
function dokuwiki_get_link(&$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;
}
// 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'] = '';
$link['suf'] = '';
}
$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);
//}
}
class syntax_plugin_button extends DokuWiki_Syntax_Plugin {
public $urls = array();
public $styles = array();
function getInfo(){
return array(
'author' => 'Rémi Peyronnet',
'email' => 'remi+button@via.ecp.fr',
'date' => '2013-05-17',
'name' => 'Button Plugin',
'desc' => 'Add button links syntax',
'url' => 'http://people.via.ecp.fr/~remi/',
);
}
function getType() { return 'substition'; }
function getPType() { return 'normal'; }
function getSort() { return 250; } // Internal link is 300
function connectTo($mode) { $this->Lexer->addSpecialPattern('\[\[{[^}]*}[^\]]*\]\]',$mode,'plugin_button'); }
function handle($match, $state, $pos, &$handler)
{
global $plugin_button_styles;
switch ($state) {
case DOKU_LEXER_SPECIAL :
$data = '';
// Button
if (preg_match('/\[\[{(?[^}\|]*)\|?(?[^}]*)}(?[^\]\|]*)\|?(?[^\]]*)\]\]/', $match, $matches))
{
$data = $matches;
}
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:
if (is_array($match))
{
if ($match['image'] == 'conf.styles')
{
$plugin_button_styles[$match['link']] = $match['title'];
}
else if ($match['image'] == 'conf.target')
{
$plugin_button_target[$match['link']] = $match['title'];
}
else
{
// 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 = dokuwiki_get_link($renderer, $match['link'], $match['title']);
}
$target = "";
if (is_array($plugin_button_target) && array_key_exists('default',$plugin_button_target))
{
$target = " target='" . $plugin_button_target['default'] . "'";
}
if (is_array($plugin_button_target) && array_key_exists($match['css'],$plugin_button_target))
{
$target = " target='" . $plugin_button_target[$match['css']] . "'";
}
if ($match['css'] != "")
{
if (is_array($plugin_button_styles) && array_key_exists($match['css'],$plugin_button_styles))
{
$match['css'] = $plugin_button_styles[$match['css']];
}
}
if (is_array($plugin_button_styles) && array_key_exists('default',$plugin_button_styles) && ($match['css'] != 'default'))
{
$match['css'] = $plugin_button_styles['default'] .' ; '. $match['css'];
}
$image = $match['image'];
$link['name'] = str_replace('\\\\','
', $link['name']);
if ($image != '')
{
$image = "
";
}
$text = "$image${link['name']}";
$renderer->doc .= $text;
}
}
break;
case DOKU_LEXER_UNMATCHED : $renderer->doc .= $renderer->_xmlEntities($match); break;
case DOKU_LEXER_EXIT : $renderer->doc .= ""; break;
}
return true;
}
return false;
}
}
?>