1<?php 2 3/* 4 * This file is part of the league/commonmark package. 5 * 6 * (c) Colin O'Dell <colinodell@gmail.com> 7 * (c) Rezo Zero / Ambroise Maupate 8 * 9 * For the full copyright and license information, please view the LICENSE 10 * file that was distributed with this source code. 11 */ 12 13declare(strict_types=1); 14 15namespace League\CommonMark\Extension\Footnote\Parser; 16 17use League\CommonMark\Block\Parser\BlockParserInterface; 18use League\CommonMark\ContextInterface; 19use League\CommonMark\Cursor; 20use League\CommonMark\Extension\Footnote\Node\Footnote; 21use League\CommonMark\Reference\Reference; 22use League\CommonMark\Util\RegexHelper; 23 24final class FootnoteParser implements BlockParserInterface 25{ 26 public function parse(ContextInterface $context, Cursor $cursor): bool 27 { 28 if ($cursor->isIndented()) { 29 return false; 30 } 31 32 $match = RegexHelper::matchFirst( 33 '/^\[\^([^\n^\]]+)\]\:\s/', 34 $cursor->getLine(), 35 $cursor->getNextNonSpacePosition() 36 ); 37 38 if (!$match) { 39 return false; 40 } 41 42 $cursor->advanceToNextNonSpaceOrTab(); 43 $cursor->advanceBy(\strlen($match[0])); 44 $str = $cursor->getRemainder(); 45 \preg_replace('/^\[\^([^\n^\]]+)\]\:\s/', '', $str); 46 47 if (\preg_match('/^\[\^([^\n^\]]+)\]\:\s/', $match[0], $matches) > 0) { 48 $context->addBlock($this->createFootnote($matches[1])); 49 $context->setBlocksParsed(true); 50 51 return true; 52 } 53 54 return false; 55 } 56 57 private function createFootnote(string $label): Footnote 58 { 59 return new Footnote( 60 new Reference($label, $label, $label) 61 ); 62 } 63} 64