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