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\IndentedCode;
17
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
18
use League\CommonMark\Parser\Block\BlockContinue;
19
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
20
use League\CommonMark\Parser\Cursor;
21
use League\CommonMark\Util\ArrayCollection;
22
 
23
final class IndentedCodeParser extends AbstractBlockContinueParser
24
{
25
    /** @psalm-readonly */
26
    private IndentedCode $block;
27
 
28
    /** @var ArrayCollection<string> */
29
    private ArrayCollection $strings;
30
 
31
    public function __construct()
32
    {
33
        $this->block   = new IndentedCode();
34
        $this->strings = new ArrayCollection();
35
    }
36
 
37
    public function getBlock(): IndentedCode
38
    {
39
        return $this->block;
40
    }
41
 
42
    public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
43
    {
44
        if ($cursor->isIndented()) {
45
            $cursor->advanceBy(Cursor::INDENT_LEVEL, true);
46
 
47
            return BlockContinue::at($cursor);
48
        }
49
 
50
        if ($cursor->isBlank()) {
51
            $cursor->advanceToNextNonSpaceOrTab();
52
 
53
            return BlockContinue::at($cursor);
54
        }
55
 
56
        return BlockContinue::none();
57
    }
58
 
59
    public function addLine(string $line): void
60
    {
61
        $this->strings[] = $line;
62
    }
63
 
64
    public function closeBlock(): void
65
    {
66
        $reversed = \array_reverse($this->strings->toArray(), true);
67
        foreach ($reversed as $index => $line) {
68
            if ($line !== '' && $line !== "\n" && ! \preg_match('/^(\n *)$/', $line)) {
69
                break;
70
            }
71
 
72
            unset($reversed[$index]);
73
        }
74
 
75
        $fixed = \array_reverse($reversed);
76
        $tmp   = \implode("\n", $fixed);
77
        if (\substr($tmp, -1) !== "\n") {
78
            $tmp .= "\n";
79
        }
80
 
81
        $this->block->setLiteral($tmp);
82
    }
83
}