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\Block\Element; 16 17use League\CommonMark\ContextInterface; 18use League\CommonMark\Cursor; 19 20class Paragraph extends AbstractStringContainerBlock implements InlineContainerInterface 21{ 22 public function canContain(AbstractBlock $block): bool 23 { 24 return false; 25 } 26 27 public function isCode(): bool 28 { 29 return false; 30 } 31 32 public function matchesNextLine(Cursor $cursor): bool 33 { 34 if ($cursor->isBlank()) { 35 $this->lastLineBlank = true; 36 37 return false; 38 } 39 40 return true; 41 } 42 43 public function finalize(ContextInterface $context, int $endLineNumber) 44 { 45 parent::finalize($context, $endLineNumber); 46 47 $this->finalStringContents = \preg_replace('/^ */m', '', \implode("\n", $this->getStrings())); 48 49 // Short-circuit 50 if ($this->finalStringContents === '' || $this->finalStringContents[0] !== '[') { 51 return; 52 } 53 54 $cursor = new Cursor($this->finalStringContents); 55 56 $referenceFound = $this->parseReferences($context, $cursor); 57 58 $this->finalStringContents = $cursor->getRemainder(); 59 60 if ($referenceFound && $cursor->isAtEnd()) { 61 $this->detach(); 62 } 63 } 64 65 /** 66 * @param ContextInterface $context 67 * @param Cursor $cursor 68 * 69 * @return bool 70 */ 71 protected function parseReferences(ContextInterface $context, Cursor $cursor) 72 { 73 $referenceFound = false; 74 while ($cursor->getCharacter() === '[' && $context->getReferenceParser()->parse($cursor)) { 75 $this->finalStringContents = $cursor->getRemainder(); 76 $referenceFound = true; 77 } 78 79 return $referenceFound; 80 } 81 82 public function handleRemainingContents(ContextInterface $context, Cursor $cursor) 83 { 84 $cursor->advanceToNextNonSpaceOrTab(); 85 86 /** @var self $tip */ 87 $tip = $context->getTip(); 88 $tip->addLine($cursor->getRemainder()); 89 } 90 91 /** 92 * @return string[] 93 */ 94 public function getStrings(): array 95 { 96 return $this->strings->toArray(); 97 } 98} 99