xref: /dokuwiki/inc/Parsing/ParserMode/Media.php (revision 3440a8c07d59952439e180d2c33a32262fd3a84c)
1be906b56SAndreas Gohr<?php
2be906b56SAndreas Gohr
3be906b56SAndreas Gohrnamespace dokuwiki\Parsing\ParserMode;
4be906b56SAndreas Gohr
571096e46SAndreas Gohruse dokuwiki\Parsing\Handler;
6*3440a8c0SAndreas Gohruse dokuwiki\Parsing\Helpers;
771096e46SAndreas Gohr
8be906b56SAndreas Gohrclass Media extends AbstractMode
9be906b56SAndreas Gohr{
10be906b56SAndreas Gohr    /** @inheritdoc */
1171096e46SAndreas Gohr    public function getSort()
1271096e46SAndreas Gohr    {
1371096e46SAndreas Gohr        return 320;
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, 'media');
21be906b56SAndreas Gohr    }
22be906b56SAndreas Gohr
23be906b56SAndreas Gohr    /** @inheritdoc */
2471096e46SAndreas Gohr    public function handle($match, $state, $pos, Handler $handler)
25be906b56SAndreas Gohr    {
2671096e46SAndreas Gohr        $p = self::parseMedia($match);
2771096e46SAndreas Gohr
2871096e46SAndreas Gohr        $handler->addCall(
2971096e46SAndreas Gohr            $p['type'],
3071096e46SAndreas Gohr            [$p['src'], $p['title'], $p['align'], $p['width'], $p['height'], $p['cache'], $p['linking']],
3171096e46SAndreas Gohr            $pos
3271096e46SAndreas Gohr        );
3371096e46SAndreas Gohr        return true;
3471096e46SAndreas Gohr    }
3571096e46SAndreas Gohr
3671096e46SAndreas Gohr    /**
3771096e46SAndreas Gohr     * Parse media syntax into its components
3871096e46SAndreas Gohr     *
3971096e46SAndreas Gohr     * @param string $match The full media syntax (e.g. {{image.png?200|title}})
4071096e46SAndreas Gohr     * @return array Parsed media parameters (type, src, title, align, width, height, cache, linking)
4171096e46SAndreas Gohr     */
4271096e46SAndreas Gohr    public static function parseMedia($match)
4371096e46SAndreas Gohr    {
4471096e46SAndreas Gohr        // Strip the opening and closing markup
4571096e46SAndreas Gohr        $link = preg_replace(['/^\{\{/', '/\}\}$/u'], '', $match);
4671096e46SAndreas Gohr
4771096e46SAndreas Gohr        // Split title from URL
4871096e46SAndreas Gohr        $link = sexplode('|', $link, 2);
4971096e46SAndreas Gohr
5071096e46SAndreas Gohr        // Check alignment
5171096e46SAndreas Gohr        $ralign = (bool)preg_match('/^ /', $link[0]);
5271096e46SAndreas Gohr        $lalign = (bool)preg_match('/ $/', $link[0]);
5371096e46SAndreas Gohr
5471096e46SAndreas Gohr        // Logic = what's that ;)...
5571096e46SAndreas Gohr        if ($lalign & $ralign) {
5671096e46SAndreas Gohr            $align = 'center';
5771096e46SAndreas Gohr        } elseif ($ralign) {
5871096e46SAndreas Gohr            $align = 'right';
5971096e46SAndreas Gohr        } elseif ($lalign) {
6071096e46SAndreas Gohr            $align = 'left';
6171096e46SAndreas Gohr        } else {
6271096e46SAndreas Gohr            $align = null;
6371096e46SAndreas Gohr        }
6471096e46SAndreas Gohr
6571096e46SAndreas Gohr        // The title...
6671096e46SAndreas Gohr        if (!isset($link[1])) {
6771096e46SAndreas Gohr            $link[1] = null;
6871096e46SAndreas Gohr        }
6971096e46SAndreas Gohr
7071096e46SAndreas Gohr        //remove aligning spaces
7171096e46SAndreas Gohr        $link[0] = trim($link[0]);
7271096e46SAndreas Gohr
73*3440a8c0SAndreas Gohr        $p = Helpers::parseMediaParameters($link[0]);
74*3440a8c0SAndreas Gohr
75*3440a8c0SAndreas Gohr        // Explicit param-derived alignment (?left/?right/?center) beats
76*3440a8c0SAndreas Gohr        // the whitespace-derived one — it's unambiguous and visible, and
77*3440a8c0SAndreas Gohr        // is the only form GfmMedia can express.
78*3440a8c0SAndreas Gohr        if ($p['align'] !== null) {
79*3440a8c0SAndreas Gohr            $align = $p['align'];
8071096e46SAndreas Gohr        }
8171096e46SAndreas Gohr
82*3440a8c0SAndreas Gohr        if (media_isexternal($p['src']) || link_isinterwiki($p['src'])) {
8371096e46SAndreas Gohr            $call = 'externalmedia';
8471096e46SAndreas Gohr        } else {
8571096e46SAndreas Gohr            $call = 'internalmedia';
8671096e46SAndreas Gohr        }
8771096e46SAndreas Gohr
8871096e46SAndreas Gohr        return [
8971096e46SAndreas Gohr            'type' => $call,
90*3440a8c0SAndreas Gohr            'src' => $p['src'],
9171096e46SAndreas Gohr            'title' => $link[1],
9271096e46SAndreas Gohr            'align' => $align,
93*3440a8c0SAndreas Gohr            'width' => $p['width'],
94*3440a8c0SAndreas Gohr            'height' => $p['height'],
95*3440a8c0SAndreas Gohr            'cache' => $p['cache'],
96*3440a8c0SAndreas Gohr            'linking' => $p['linking']
9771096e46SAndreas Gohr        ];
98be906b56SAndreas Gohr    }
99be906b56SAndreas Gohr}
100