xref: /dokuwiki/inc/Parsing/ParserMode/Internallink.php (revision e89aeebd5989e476b6a69236d9aabf72a9a01f14)
1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5use dokuwiki\Parsing\Handler;
6
7class Internallink extends AbstractMode
8{
9    use LinkDispatch;
10
11    /** @inheritdoc */
12    public function getSort()
13    {
14        return 300;
15    }
16
17    /** @inheritdoc */
18    public function connectTo($mode)
19    {
20        // Word boundaries?
21        $this->Lexer->addSpecialPattern("\[\[.*?\]\](?!\])", $mode, 'internallink');
22    }
23
24    /** @inheritdoc */
25    public function handle($match, $state, $pos, Handler $handler)
26    {
27        // Strip the opening and closing markup
28        $link = preg_replace(['/^\[\[/', '/\]\]$/u'], '', $match);
29
30        // Split title from URL
31        $link = sexplode('|', $link, 2);
32        if ($link[1] !== null && preg_match('/^\{\{[^\}]+\}\}$/', $link[1])) {
33            // If the title is an image, convert it to an array containing the image details
34            $link[1] = Media::parseMedia($link[1]);
35        }
36        $link[0] = trim($link[0]);
37
38        $this->dispatchLink($link[0], $link[1], $pos, $handler);
39        return true;
40    }
41}
42