1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4use dokuwiki\Parsing\Handler;
5
6/**
7 * BBCode plugin: allows BBCode markup familiar from forum software
8 *
9 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
10 * @author     Esther Brunner <esther@kaffeehaus.ch>
11 */
12class syntax_plugin_bbcode_link extends SyntaxPlugin
13{
14    /** @inheritdoc */
15    public function getType()
16    {
17        return 'substition';
18    }
19    /** @inheritdoc */
20    public function getSort()
21    {
22        return 105;
23    }
24    /** @inheritdoc */
25    public function connectTo($mode)
26    {
27        $this->Lexer->addSpecialPattern('\[url.+?\[/url\]', $mode, 'plugin_bbcode_link');
28    }
29
30    /** @inheritdoc */
31    public function handle($match, $state, $pos, Handler $handler)
32    {
33        $match = substr($match, 5, -6);
34        if (preg_match('/".+?"/', $match)) $match = substr($match, 1, -1); // addition #1: unquote
35        [$url, $title] = sexplode(']', $match, 2, null);
36
37        // external link (accepts all protocols)
38        if (preg_match('#^([a-z0-9\-.+]+?)://#i', $url)) {
39            $handler->addCall('externallink', [$url,$title], $pos);
40
41        // local link
42        } elseif (preg_match('!^#.+!', $url)) {
43            $handler->addCall('locallink', [substr($url, 1),$title], $pos);
44
45        // internal link
46        } else {
47            $handler->addCall('internallink', [$url,$title], $pos);
48        }
49        return true;
50    }
51
52    /** @inheritdoc */
53    public function render($format, Doku_Renderer $renderer, $data)
54    {
55        return true;
56    }
57}
58