xref: /dokuwiki/inc/Parsing/ParserMode/Externallink.php (revision a0c85aa6fc6c2fa53bb0b6c3e29b94e86a2e0e5d)
1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5class Externallink extends AbstractMode
6{
7    protected $schemes = array();
8    protected $patterns = array();
9
10    /** @inheritdoc */
11    public function preConnect()
12    {
13        if (count($this->patterns)) return;
14
15        $ltrs = '\w';
16        $gunk = '/\#~:.?+=&%@!\-\[\]';
17        $punc = '.:?\-;,';
18        $host = $ltrs.$punc;
19        $any  = $ltrs.$gunk.$punc;
20
21        $this->schemes = getSchemes();
22        foreach ($this->schemes as $scheme) {
23            $this->patterns[] = '\b(?i)'.$scheme.'(?-i)://['.$any.']+?(?=['.$punc.']*[^'.$any.'])';
24        }
25
26        $this->patterns[] = '(?<=\s)(?i)www?(?-i)\.['.$host.']+?\.['.$host.']+?['.$any.']+?(?=['.$punc.']*[^'.$any.'])';
27        $this->patterns[] = '(?<=\s)(?i)ftp?(?-i)\.['.$host.']+?\.['.$host.']+?['.$any.']+?(?=['.$punc.']*[^'.$any.'])';
28    }
29
30    /** @inheritdoc */
31    public function connectTo($mode)
32    {
33
34        foreach ($this->patterns as $pattern) {
35            $this->Lexer->addSpecialPattern($pattern, $mode, 'externallink');
36        }
37    }
38
39    /** @inheritdoc */
40    public function getSort()
41    {
42        return 330;
43    }
44
45    /**
46     * @return array
47     */
48    public function getPatterns()
49    {
50        return $this->patterns;
51    }
52}
53