1<?php
2/**
3 * BBCode plugin: allows BBCode markup familiar from forum software
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Esther Brunner <esther@kaffeehaus.ch>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12
13/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 */
17class syntax_plugin_bbcode_link extends DokuWiki_Syntax_Plugin {
18
19    function getType() { return 'substition'; }
20    function getSort() { return 105; }
21    function connectTo($mode) { $this->Lexer->addSpecialPattern('\[url.+?\[/url\]',$mode,'plugin_bbcode_link'); }
22
23    /**
24     * Handle the match
25     */
26    function handle($match, $state, $pos, Doku_Handler $handler) {
27        $match = substr($match, 5, -6);
28        if (preg_match('/".+?"/',$match)) $match = substr($match, 1, -1); // addition #1: unquote
29        $match = preg_split('/\]/u',$match,2);
30        if ( !isset($match[0]) ) {
31            $url   = $match[1];
32            $title = NULL;
33        } else {
34            $url   = $match[0];
35            $title = $match[1];
36        }
37
38        // external link (accepts all protocols)
39        if ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$url) ) {
40            $handler->_addCall('externallink',array($url,$title),$pos);
41
42        // local link
43        } elseif ( preg_match('!^#.+!',$url) ) {
44            $handler->_addCall('locallink',array(substr($url,1),$title),$pos);
45
46        // internal link
47        } else {
48            $handler->_addCall('internallink',array($url,$title),$pos);
49        }
50        return true;
51    }
52
53    /**
54     * Create output
55     */
56    function render($mode, Doku_Renderer $renderer, $data) {
57        return true;
58    }
59}
60// vim:ts=4:sw=4:et:enc=utf-8:
61