1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
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
|
|
|
|
15
|
|
|
namespace League\CommonMark\Block\Element; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\ContextInterface; |
18
|
|
|
use League\CommonMark\Cursor; |
19
|
|
|
|
20
|
|
|
class Paragraph extends AbstractBlock implements InlineContainerInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Returns true if this block can contain the given block as a child node |
24
|
|
|
* |
25
|
|
|
* @param AbstractBlock $block |
26
|
|
|
* |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
69 |
|
public function canContain(AbstractBlock $block) |
30
|
|
|
{ |
31
|
69 |
|
return false; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns true if block type can accept lines of text |
36
|
|
|
* |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
1608 |
|
public function acceptsLines() |
40
|
|
|
{ |
41
|
1608 |
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Whether this is a code block |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
357 |
|
public function isCode() |
50
|
|
|
{ |
51
|
357 |
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
687 |
|
public function matchesNextLine(Cursor $cursor) |
55
|
|
|
{ |
56
|
687 |
|
if ($cursor->isBlank()) { |
57
|
429 |
|
$this->lastLineBlank = true; |
58
|
|
|
|
59
|
429 |
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
357 |
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
1572 |
|
public function finalize(ContextInterface $context, $endLineNumber) |
66
|
|
|
{ |
67
|
1572 |
|
parent::finalize($context, $endLineNumber); |
68
|
|
|
|
69
|
1572 |
|
$this->finalStringContents = preg_replace('/^ */m', '', implode("\n", $this->getStrings())); |
70
|
|
|
|
71
|
|
|
// Short-circuit |
72
|
1572 |
|
if ($this->finalStringContents === '' || $this->finalStringContents[0] !== '[') { |
73
|
1260 |
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
390 |
|
$cursor = new Cursor($this->finalStringContents); |
77
|
|
|
|
78
|
390 |
|
$referenceFound = $this->parseReferences($context, $cursor); |
79
|
|
|
|
80
|
390 |
|
$this->finalStringContents = $cursor->getRemainder(); |
81
|
|
|
|
82
|
390 |
|
if ($referenceFound && $cursor->isAtEnd()) { |
83
|
216 |
|
$this->detach(); |
84
|
144 |
|
} |
85
|
390 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param ContextInterface $context |
89
|
|
|
* @param Cursor $cursor |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
390 |
|
protected function parseReferences(ContextInterface $context, Cursor $cursor) |
94
|
|
|
{ |
95
|
390 |
|
$referenceFound = false; |
96
|
390 |
|
while ($cursor->getCharacter() === '[' && $context->getReferenceParser()->parse($cursor)) { |
97
|
222 |
|
$this->finalStringContents = $cursor->getRemainder(); |
98
|
222 |
|
$referenceFound = true; |
99
|
148 |
|
} |
100
|
|
|
|
101
|
390 |
|
return $referenceFound; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param ContextInterface $context |
106
|
|
|
* @param Cursor $cursor |
107
|
|
|
*/ |
108
|
252 |
|
public function handleRemainingContents(ContextInterface $context, Cursor $cursor) |
109
|
|
|
{ |
110
|
252 |
|
$cursor->advanceToNextNonSpaceOrTab(); |
111
|
252 |
|
$context->getTip()->addLine($cursor->getRemainder()); |
112
|
252 |
|
} |
113
|
|
|
} |
114
|
|
|
|