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