xref: /dokuwiki/inc/Parsing/ParserMode/Externallink.php (revision 90fb952c4c30c09c8446076ba05991c89a3f0b01)
1be906b56SAndreas Gohr<?php
2be906b56SAndreas Gohr
3be906b56SAndreas Gohrnamespace dokuwiki\Parsing\ParserMode;
4be906b56SAndreas Gohr
5be906b56SAndreas Gohrclass Externallink extends AbstractMode
6be906b56SAndreas Gohr{
7*bcaec9f4SAndreas Gohr    protected $schemes = [];
8*bcaec9f4SAndreas Gohr    protected $patterns = [];
9be906b56SAndreas Gohr
10be906b56SAndreas Gohr    /** @inheritdoc */
11be906b56SAndreas Gohr    public function preConnect()
12be906b56SAndreas Gohr    {
13be906b56SAndreas Gohr        if (count($this->patterns)) return;
14be906b56SAndreas Gohr
15be906b56SAndreas Gohr        $ltrs = '\w';
16be906b56SAndreas Gohr        $gunk = '/\#~:.?+=&%@!\-\[\]';
17be906b56SAndreas Gohr        $punc = '.:?\-;,';
18be906b56SAndreas Gohr        $host = $ltrs . $punc;
19be906b56SAndreas Gohr        $any  = $ltrs . $gunk . $punc;
20be906b56SAndreas Gohr
21be906b56SAndreas Gohr        $this->schemes = getSchemes();
22be906b56SAndreas Gohr        foreach ($this->schemes as $scheme) {
23be906b56SAndreas Gohr            $this->patterns[] = '\b(?i)' . $scheme . '(?-i)://[' . $any . ']+?(?=[' . $punc . ']*[^' . $any . '])';
24be906b56SAndreas Gohr        }
25be906b56SAndreas Gohr
264da6a3ceSPhy        $this->patterns[] = '(?<![/\\\\])\b(?i)www?(?-i)\.[' . $host . ']+?\.' .
274da6a3ceSPhy                            '[' . $host . ']+?[' . $any . ']+?(?=[' . $punc . ']*[^' . $any . '])';
284da6a3ceSPhy        $this->patterns[] = '(?<![/\\\\])\b(?i)ftp?(?-i)\.[' . $host . ']+?\.' .
294da6a3ceSPhy                            '[' . $host . ']+?[' . $any . ']+?(?=[' . $punc . ']*[^' . $any . '])';
30be906b56SAndreas Gohr    }
31be906b56SAndreas Gohr
32be906b56SAndreas Gohr    /** @inheritdoc */
33be906b56SAndreas Gohr    public function connectTo($mode)
34be906b56SAndreas Gohr    {
35be906b56SAndreas Gohr
36be906b56SAndreas Gohr        foreach ($this->patterns as $pattern) {
37be906b56SAndreas Gohr            $this->Lexer->addSpecialPattern($pattern, $mode, 'externallink');
38be906b56SAndreas Gohr        }
39be906b56SAndreas Gohr    }
40be906b56SAndreas Gohr
41be906b56SAndreas Gohr    /** @inheritdoc */
42be906b56SAndreas Gohr    public function getSort()
43be906b56SAndreas Gohr    {
44be906b56SAndreas Gohr        return 330;
45be906b56SAndreas Gohr    }
465c99934fSAnna Dabrowska
475c99934fSAnna Dabrowska    /**
485c99934fSAnna Dabrowska     * @return array
495c99934fSAnna Dabrowska     */
505c99934fSAnna Dabrowska    public function getPatterns()
515c99934fSAnna Dabrowska    {
525c99934fSAnna Dabrowska        return $this->patterns;
535c99934fSAnna Dabrowska    }
54be906b56SAndreas Gohr}
55