Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <colinodell@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
 
14
namespace League\CommonMark\Extension\CommonMark\Parser\Block;
15
 
16
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
17
use League\CommonMark\Extension\CommonMark\Node\Block\ListData;
18
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
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 ListBlockParser extends AbstractBlockContinueParser
26
{
27
    /** @psalm-readonly */
28
    private ListBlock $block;
29
 
30
    private bool $hadBlankLine = false;
31
 
32
    private int $linesAfterBlank = 0;
33
 
34
    public function __construct(ListData $listData)
35
    {
36
        $this->block = new ListBlock($listData);
37
    }
38
 
39
    public function getBlock(): ListBlock
40
    {
41
        return $this->block;
42
    }
43
 
44
    public function isContainer(): bool
45
    {
46
        return true;
47
    }
48
 
49
    public function canContain(AbstractBlock $childBlock): bool
50
    {
51
        if (! $childBlock instanceof ListItem) {
52
            return false;
53
        }
54
 
55
        // Another list item is being added to this list block.
56
        // If the previous line was blank, that means this list
57
        // block is "loose" (not tight).
58
        if ($this->hadBlankLine && $this->linesAfterBlank === 1) {
59
            $this->block->setTight(false);
60
            $this->hadBlankLine = false;
61
        }
62
 
63
        return true;
64
    }
65
 
66
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
67
    {
68
        if ($cursor->isBlank()) {
69
            $this->hadBlankLine    = true;
70
            $this->linesAfterBlank = 0;
71
        } elseif ($this->hadBlankLine) {
72
            $this->linesAfterBlank++;
73
        }
74
 
75
        // List blocks themselves don't have any markers, only list items. So try to stay in the list.
76
        // If there is a block start other than list item, canContain makes sure that this list is closed.
77
        return BlockContinue::at($cursor);
78
    }
79
}