1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
7
|
|
|
* (c) 2015 Martin Hasoň <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace League\CommonMark\Extension\Attributes\Parser; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Extension\Attributes\Node\Attributes; |
18
|
|
|
use League\CommonMark\Extension\Attributes\Util\AttributesHelper; |
19
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
20
|
|
|
use League\CommonMark\Parser\Block\AbstractBlockContinueParser; |
21
|
|
|
use League\CommonMark\Parser\Block\BlockContinue; |
22
|
|
|
use League\CommonMark\Parser\Block\BlockContinueParserInterface; |
23
|
|
|
use League\CommonMark\Parser\Cursor; |
24
|
|
|
|
25
|
|
|
final class AttributesBlockContinueParser extends AbstractBlockContinueParser |
26
|
|
|
{ |
27
|
|
|
/** @var Attributes */ |
28
|
|
|
private $block; |
29
|
|
|
|
30
|
|
|
/** @var AbstractBlock */ |
31
|
|
|
private $container; |
32
|
|
|
|
33
|
|
|
/** @var bool */ |
34
|
|
|
private $hasSubsequentLine = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array<string, mixed> $attributes The attributes identified by the block start parser |
38
|
|
|
* @param AbstractBlock $container The node we were in when these attributes were discovered |
39
|
|
|
*/ |
40
|
15 |
|
public function __construct(array $attributes, AbstractBlock $container) |
41
|
|
|
{ |
42
|
15 |
|
$this->block = new Attributes($attributes); |
43
|
|
|
|
44
|
15 |
|
$this->container = $container; |
45
|
15 |
|
} |
46
|
|
|
|
47
|
15 |
|
public function getBlock(): AbstractBlock |
48
|
|
|
{ |
49
|
15 |
|
return $this->block; |
50
|
|
|
} |
51
|
|
|
|
52
|
12 |
|
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue |
53
|
|
|
{ |
54
|
12 |
|
$this->hasSubsequentLine = true; |
55
|
|
|
|
56
|
12 |
|
$cursor->advanceToNextNonSpaceOrTab(); |
57
|
|
|
|
58
|
|
|
// Does this next line also have attributes? |
59
|
12 |
|
$attributes = AttributesHelper::parseAttributes($cursor); |
60
|
12 |
|
$cursor->advanceToNextNonSpaceOrTab(); |
61
|
12 |
|
if ($cursor->isAtEnd() && $attributes !== []) { |
62
|
|
|
// It does! Merge them into what we parsed previously |
63
|
3 |
|
$this->block->setAttributes(AttributesHelper::mergeAttributes( |
64
|
3 |
|
$this->block->getAttributes(), |
65
|
3 |
|
$attributes |
66
|
|
|
)); |
67
|
|
|
|
68
|
|
|
// Tell the core parser we've consumed everything |
69
|
3 |
|
return BlockContinue::at($cursor); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Okay, so there are no attributes on the next line |
73
|
|
|
// If this next line is blank we know we can't target the next node, it must be a previous one |
74
|
12 |
|
if ($cursor->isBlank()) { |
75
|
9 |
|
$this->block->setTarget(Attributes::TARGET_PREVIOUS); |
76
|
|
|
} |
77
|
|
|
|
78
|
12 |
|
return BlockContinue::none(); |
79
|
|
|
} |
80
|
|
|
|
81
|
15 |
|
public function closeBlock(): void |
82
|
|
|
{ |
83
|
|
|
// Attributes appearing at the very end of the document won't have any last lines to check |
84
|
|
|
// so we can make that determination here |
85
|
15 |
|
if (! $this->hasSubsequentLine) { |
86
|
6 |
|
$this->block->setTarget(Attributes::TARGET_PREVIOUS); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// We know this block must apply to the "previous" block, but that could be a sibling or parent, |
90
|
|
|
// so we check the containing block to see which one it might be. |
91
|
15 |
|
if ($this->block->getTarget() === Attributes::TARGET_PREVIOUS && $this->block->parent() === $this->container) { |
92
|
3 |
|
$this->block->setTarget(Attributes::TARGET_PARENT); |
93
|
|
|
} |
94
|
15 |
|
} |
95
|
|
|
} |
96
|
|
|
|