1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace League\CommonMark\Extension\DescriptionList\Parser; |
15
|
|
|
|
16
|
|
|
use League\CommonMark\Extension\DescriptionList\Node\Description; |
17
|
|
|
use League\CommonMark\Node\Block\Paragraph; |
18
|
|
|
use League\CommonMark\Parser\Block\BlockStart; |
19
|
|
|
use League\CommonMark\Parser\Block\BlockStartParserInterface; |
20
|
|
|
use League\CommonMark\Parser\Cursor; |
21
|
|
|
use League\CommonMark\Parser\MarkdownParserStateInterface; |
22
|
|
|
|
23
|
|
|
final class DescriptionStartParser implements BlockStartParserInterface |
24
|
|
|
{ |
25
|
42 |
|
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart |
26
|
|
|
{ |
27
|
42 |
|
if ($cursor->isIndented()) { |
28
|
9 |
|
return BlockStart::none(); |
29
|
|
|
} |
30
|
|
|
|
31
|
42 |
|
$cursor->advanceToNextNonSpaceOrTab(); |
32
|
42 |
|
if ($cursor->match('/:[ \t]+/') === null) { |
33
|
9 |
|
return BlockStart::none(); |
34
|
|
|
} |
35
|
|
|
|
36
|
36 |
|
$terms = $parserState->getParagraphContent(); |
37
|
|
|
|
38
|
36 |
|
$activeBlock = $parserState->getActiveBlockParser()->getBlock(); |
39
|
|
|
|
40
|
36 |
|
if ($terms !== null && $terms !== '') { |
41
|
|
|
// New description; tight; term(s) sitting in pending block that we will replace |
42
|
30 |
|
return BlockStart::of(...[new DescriptionListContinueParser()], ...self::splitTerms($terms), ...[new DescriptionContinueParser(true, $cursor->getPosition())]) |
43
|
30 |
|
->at($cursor) |
44
|
30 |
|
->replaceActiveBlockParser(); |
45
|
|
|
} |
46
|
|
|
|
47
|
24 |
|
if ($activeBlock instanceof Paragraph && $activeBlock->parent() instanceof Description) { |
48
|
|
|
// Additional description in the same list as the parent description |
49
|
9 |
|
return BlockStart::of(new DescriptionContinueParser(true, $cursor->getPosition()))->at($cursor); |
50
|
|
|
} |
51
|
|
|
|
52
|
18 |
|
if ($activeBlock->lastChild() instanceof Paragraph) { |
53
|
|
|
// New description; loose; term(s) sitting in previous closed paragraph block |
54
|
15 |
|
return BlockStart::of(new DescriptionContinueParser(false, $cursor->getPosition()))->at($cursor); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// No preceding terms |
58
|
3 |
|
return BlockStart::none(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array<int, DescriptionTermContinueParser> |
63
|
|
|
*/ |
64
|
30 |
|
private static function splitTerms(string $terms): array |
65
|
|
|
{ |
66
|
30 |
|
$ret = []; |
67
|
30 |
|
foreach (\explode("\n", $terms) as $term) { |
68
|
30 |
|
$ret[] = new DescriptionTermContinueParser($term); |
69
|
|
|
} |
70
|
|
|
|
71
|
30 |
|
return $ret; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|