Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <colinodell@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
 
14
namespace League\CommonMark\Extension\Autolink;
15
 
16
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
17
use League\CommonMark\Parser\Inline\InlineParserInterface;
18
use League\CommonMark\Parser\Inline\InlineParserMatch;
19
use League\CommonMark\Parser\InlineParserContext;
20
 
21
final class EmailAutolinkParser implements InlineParserInterface
22
{
23
    private const REGEX = '[A-Za-z0-9.\-_+]+@[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_.]+';
24
 
25
    public function getMatchDefinition(): InlineParserMatch
26
    {
27
        return InlineParserMatch::regex(self::REGEX);
28
    }
29
 
30
    public function parse(InlineParserContext $inlineContext): bool
31
    {
32
        $email = $inlineContext->getFullMatch();
33
        // The last character cannot be - or _
34
        if (\in_array(\substr($email, -1), ['-', '_'], true)) {
35
            return false;
36
        }
37
 
38
        // Does the URL end with punctuation that should be stripped?
39
        if (\substr($email, -1) === '.') {
40
            $email = \substr($email, 0, -1);
41
        }
42
 
43
        $inlineContext->getCursor()->advanceBy(\strlen($email));
44
        $inlineContext->getContainer()->appendChild(new Link('mailto:' . $email, $email));
45
 
46
        return true;
47
    }
48
}