|
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\Reference; |
|
16
|
|
|
|
|
17
|
|
|
use League\CommonMark\Cursor; |
|
18
|
|
|
use League\CommonMark\Util\LinkParserHelper; |
|
19
|
|
|
|
|
20
|
|
|
final class ReferenceParser |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var ReferenceMap |
|
24
|
|
|
*/ |
|
25
|
|
|
private $referenceMap; |
|
26
|
|
|
|
|
27
|
2070 |
|
public function __construct(ReferenceMap $referenceMap) |
|
28
|
|
|
{ |
|
29
|
2070 |
|
$this->referenceMap = $referenceMap; |
|
30
|
2070 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Attempt to parse a link reference, modifying the refmap. |
|
34
|
|
|
* |
|
35
|
|
|
* @param Cursor $cursor |
|
36
|
|
|
* |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
420 |
|
public function parse(Cursor $cursor) |
|
40
|
|
|
{ |
|
41
|
420 |
|
if ($cursor->isAtEnd()) { |
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
420 |
|
$initialState = $cursor->saveState(); |
|
46
|
|
|
|
|
47
|
420 |
|
$matchChars = LinkParserHelper::parseLinkLabel($cursor); |
|
48
|
420 |
|
if ($matchChars === 0) { |
|
49
|
48 |
|
$cursor->restoreState($initialState); |
|
50
|
|
|
|
|
51
|
48 |
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// We need to trim the opening and closing brackets from the previously-matched text |
|
55
|
396 |
|
$label = \substr($cursor->getPreviousText(), 1, -1); |
|
56
|
|
|
|
|
57
|
396 |
|
if (\preg_match('/[^\s]/', $label) === 0) { |
|
58
|
6 |
|
$cursor->restoreState($initialState); |
|
59
|
|
|
|
|
60
|
6 |
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
390 |
|
if ($cursor->getCharacter() !== ':') { |
|
64
|
297 |
|
$cursor->restoreState($initialState); |
|
65
|
|
|
|
|
66
|
297 |
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Advance past the colon |
|
70
|
246 |
|
$cursor->advance(); |
|
71
|
|
|
|
|
72
|
|
|
// Link URL |
|
73
|
246 |
|
$cursor->advanceToNextNonSpaceOrNewline(); |
|
74
|
|
|
|
|
75
|
246 |
|
$destination = LinkParserHelper::parseLinkDestination($cursor); |
|
76
|
246 |
|
if ($destination === null) { |
|
77
|
3 |
|
$cursor->restoreState($initialState); |
|
78
|
|
|
|
|
79
|
3 |
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
243 |
|
$previousState = $cursor->saveState(); |
|
83
|
|
|
|
|
84
|
243 |
|
if ($cursor->advanceToNextNonSpaceOrNewline() > 0) { |
|
85
|
129 |
|
$title = LinkParserHelper::parseLinkTitle($cursor); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
243 |
|
if (!isset($title)) { |
|
89
|
138 |
|
$title = ''; |
|
90
|
138 |
|
$cursor->restoreState($previousState); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// Make sure we're at line end: |
|
94
|
243 |
|
$atLineEnd = true; |
|
95
|
243 |
|
if ($cursor->match('/^ *(?:\n|$)/') === null) { |
|
96
|
12 |
|
if ($title === '') { |
|
97
|
6 |
|
$atLineEnd = false; |
|
98
|
|
|
} else { |
|
99
|
|
|
// The potential title we found is not at the line end, |
|
100
|
|
|
// but it could still be a legal link reference if we |
|
101
|
|
|
// discard the title |
|
102
|
6 |
|
$title = ''; |
|
103
|
|
|
// rewind before spaces |
|
104
|
6 |
|
$cursor->restoreState($previousState); |
|
105
|
|
|
// and instead check if the link URL is at the line end |
|
106
|
6 |
|
$atLineEnd = $cursor->match('/^ *(?:\n|$)/') !== null; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
243 |
|
if (!$atLineEnd) { |
|
111
|
9 |
|
$cursor->restoreState($initialState); |
|
112
|
|
|
|
|
113
|
9 |
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
234 |
|
if (!$this->referenceMap->contains($label)) { |
|
117
|
234 |
|
$reference = new Reference($label, $destination, $title); |
|
118
|
234 |
|
$this->referenceMap->addReference($reference); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
234 |
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|