|
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\Inline\Parser; |
|
16
|
|
|
|
|
17
|
|
|
use League\CommonMark\Inline\Element\Code; |
|
18
|
|
|
use League\CommonMark\Inline\Element\Text; |
|
19
|
|
|
use League\CommonMark\InlineParserContext; |
|
20
|
|
|
use League\CommonMark\Util\RegexHelper; |
|
21
|
|
|
|
|
22
|
|
|
class BacktickParser extends AbstractInlineParser |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @return string[] |
|
26
|
|
|
*/ |
|
27
|
1941 |
|
public function getCharacters() |
|
28
|
|
|
{ |
|
29
|
1941 |
|
return ['`']; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param InlineParserContext $inlineContext |
|
34
|
|
|
* |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
111 |
|
public function parse(InlineParserContext $inlineContext) |
|
38
|
|
|
{ |
|
39
|
111 |
|
$cursor = $inlineContext->getCursor(); |
|
40
|
|
|
|
|
41
|
111 |
|
$ticks = $cursor->match('/^`+/'); |
|
42
|
111 |
|
if ($ticks === '') { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
111 |
|
$currentPosition = $cursor->getPosition(); |
|
47
|
111 |
|
$previousState = $cursor->saveState(); |
|
48
|
|
|
|
|
49
|
111 |
|
while ($matchingTicks = $cursor->match('/`+/m')) { |
|
50
|
93 |
|
if ($matchingTicks === $ticks) { |
|
51
|
90 |
|
$code = mb_substr($cursor->getLine(), $currentPosition, $cursor->getPosition() - $currentPosition - strlen($ticks), 'utf-8'); |
|
52
|
90 |
|
$c = preg_replace(RegexHelper::REGEX_WHITESPACE, ' ', $code); |
|
53
|
90 |
|
$inlineContext->getContainer()->appendChild(new Code(trim($c))); |
|
54
|
|
|
|
|
55
|
90 |
|
return true; |
|
56
|
|
|
} |
|
57
|
6 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
// If we got here, we didn't match a closing backtick sequence |
|
60
|
36 |
|
$cursor->restoreState($previousState); |
|
61
|
36 |
|
$inlineContext->getContainer()->appendChild(new Text($ticks)); |
|
62
|
|
|
|
|
63
|
36 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|