1<?php
2/**
3 * Plugin base links: Creates links relative to site root for all links beginning with "/"
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Robert Meerman <robert.meerman@gmail.com>
7 * @based_on   "externallink" plugin by Otto Vainio <plugins@valjakko.net>
8 */
9
10if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class syntax_plugin_baselink extends DokuWiki_Syntax_Plugin {
19
20    /**
21     * return some info
22     */
23    function getInfo(){
24        return array(
25            'author' => 'Robert Meerman, based on externallink plugin by Otto Vainio',
26            'email'  => 'robert.meerman@gmail.com',
27            'date'   => '2007-05-19',
28            'name'   => 'baselink',
29            'desc'   => 'Makes links beginning with "/" present as (always valid) internal links that point relative to your domain\'s root. Format is [[/pagename|optional title]]',
30            'url'    => 'http://wiki.splitbrain.org/plugin:baselink',
31        );
32    }
33
34    /**
35     * What kind of syntax are we?
36     */
37    function getType(){
38        return 'substition';
39    }
40
41    // Just before build in links
42    function getSort(){ return 299; }
43
44    function connectTo($mode) {
45		// \x2F = "/"
46        $this->Lexer->addSpecialPattern('\[\[\\x2F.*?\]\]',$mode,'plugin_baselink');
47    }
48
49
50    /**
51     * Handle the match
52     */
53    function handle($match, $state, $pos, &$handler){
54        $match = substr($match,2,-2); //strip [[ from start and ]] from end
55        $match = explode("|",$match, 2);
56        if( preg_match('/^\{\{[^\}]+\}\}$/',$match[1]) ){
57			// If the title is an image, convert it to an array containing the image details
58			$match[1] = Doku_Handler_Parse_Media($match[1]);
59		}
60        return $match;
61    }
62
63    /**
64     * Create output
65     */
66    function render($mode, &$renderer, $data) {
67        if($mode == 'xhtml'){
68            $text=$this->_baselink($renderer, $data[0], $data[1]);
69            $renderer->doc .= $text;
70            return true;
71        }
72        return false;
73    }
74
75
76    function _baselink(&$renderer, $url, $name = NULL) {
77        global $conf;
78
79        // Media in titles ( "{{...}}" ) are presented as arrays at this stage
80        if(is_array($name)){
81			$name = $renderer->_getLinkTitle($name, $url, $isImage);
82		}
83		else{
84			// Quick Fix to supress naming bug ("[[/base/link|This & That]]" --displayed-as--> "This &amp; That")
85			//$name = $renderer->_xmlEntities($renderer->_getLinkTitle($name, $url, $isImage));
86			$name = $renderer->_getLinkTitle($name, $url, $isImage);
87		}
88
89        $class='wikilink1';
90        $link['target'] = $conf['target']['wiki'];
91        $link['style']  = '';
92        $link['pre']    = '';
93        $link['suf']    = '';
94        $link['more']   = '';
95        $link['class']  = $class;
96        $link['url']    = $url;
97        $link['name']   = $name;
98        $link['title']  = $renderer->_xmlEntities($url);
99
100        //output formatted
101        return $renderer->_formatLink($link);
102    }
103
104}
105?>
106