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