1<?php 2 3/* 4 * This file is part of the league/commonmark package. 5 * 6 * (c) Colin O'Dell <colinodell@gmail.com> 7 * 8 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) 9 * - (c) John MacFarlane 10 * 11 * For the full copyright and license information, please view the LICENSE 12 * file that was distributed with this source code. 13 */ 14 15namespace League\CommonMark\Inline\Parser; 16 17use League\CommonMark\Inline\Element\Link; 18use League\CommonMark\InlineParserContext; 19use League\CommonMark\Util\UrlEncoder; 20 21final class AutolinkParser implements InlineParserInterface 22{ 23 const EMAIL_REGEX = '/^<([a-zA-Z0-9.!#$%&\'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)>/'; 24 const OTHER_LINK_REGEX = '/^<[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]*>/i'; 25 26 public function getCharacters(): array 27 { 28 return ['<']; 29 } 30 31 public function parse(InlineParserContext $inlineContext): bool 32 { 33 $cursor = $inlineContext->getCursor(); 34 if ($m = $cursor->match(self::EMAIL_REGEX)) { 35 $email = \substr($m, 1, -1); 36 $inlineContext->getContainer()->appendChild(new Link('mailto:' . UrlEncoder::unescapeAndEncode($email), $email)); 37 38 return true; 39 } elseif ($m = $cursor->match(self::OTHER_LINK_REGEX)) { 40 $dest = \substr($m, 1, -1); 41 $inlineContext->getContainer()->appendChild(new Link(UrlEncoder::unescapeAndEncode($dest), $dest)); 42 43 return true; 44 } 45 46 return false; 47 } 48} 49