xref: /dokuwiki/inc/Parsing/ParserMode/Internallink.php (revision 1e28e406b358f79221c515b2a56520d5dbbfb6c8)
1be906b56SAndreas Gohr<?php
2be906b56SAndreas Gohr
3be906b56SAndreas Gohrnamespace dokuwiki\Parsing\ParserMode;
4be906b56SAndreas Gohr
571096e46SAndreas Gohruse dokuwiki\Parsing\Handler;
6*1e28e406SAndreas Gohruse dokuwiki\Parsing\Helpers\Link;
771096e46SAndreas Gohr
8be906b56SAndreas Gohrclass Internallink extends AbstractMode
9be906b56SAndreas Gohr{
10be906b56SAndreas Gohr    /** @inheritdoc */
1171096e46SAndreas Gohr    public function getSort()
1271096e46SAndreas Gohr    {
1371096e46SAndreas Gohr        return 300;
1471096e46SAndreas Gohr    }
1571096e46SAndreas Gohr
1671096e46SAndreas Gohr    /** @inheritdoc */
17be906b56SAndreas Gohr    public function connectTo($mode)
18be906b56SAndreas Gohr    {
19be906b56SAndreas Gohr        // Word boundaries?
20be906b56SAndreas Gohr        $this->Lexer->addSpecialPattern("\[\[.*?\]\](?!\])", $mode, 'internallink');
21be906b56SAndreas Gohr    }
22be906b56SAndreas Gohr
23be906b56SAndreas Gohr    /** @inheritdoc */
2471096e46SAndreas Gohr    public function handle($match, $state, $pos, Handler $handler)
25be906b56SAndreas Gohr    {
2671096e46SAndreas Gohr        // Strip the opening and closing markup
2771096e46SAndreas Gohr        $link = preg_replace(['/^\[\[/', '/\]\]$/u'], '', $match);
2871096e46SAndreas Gohr
2971096e46SAndreas Gohr        // Split title from URL
3071096e46SAndreas Gohr        $link = sexplode('|', $link, 2);
3171096e46SAndreas Gohr        if ($link[1] !== null && preg_match('/^\{\{[^\}]+\}\}$/', $link[1])) {
3271096e46SAndreas Gohr            // If the title is an image, convert it to an array containing the image details
3371096e46SAndreas Gohr            $link[1] = Media::parseMedia($link[1]);
3471096e46SAndreas Gohr        }
3571096e46SAndreas Gohr        $link[0] = trim($link[0]);
3671096e46SAndreas Gohr
37*1e28e406SAndreas Gohr        [$call, $args] = Link::classify($link[0], $link[1]);
383440a8c0SAndreas Gohr        $handler->addCall($call, $args, $pos);
3971096e46SAndreas Gohr        return true;
40be906b56SAndreas Gohr    }
41be906b56SAndreas Gohr}
42