Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <colinodell@gmail.com>
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
    public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
26
    {
27
        if ($cursor->isIndented()) {
28
            return BlockStart::none();
29
        }
30
 
31
        $cursor->advanceToNextNonSpaceOrTab();
32
        if ($cursor->match('/^:[ \t]+/') === null) {
33
            return BlockStart::none();
34
        }
35
 
36
        $terms = $parserState->getParagraphContent();
37
 
38
        $activeBlock = $parserState->getActiveBlockParser()->getBlock();
39
 
40
        if ($terms !== null && $terms !== '') {
41
            // New description; tight; term(s) sitting in pending block that we will replace
42
            return BlockStart::of(...[new DescriptionListContinueParser()], ...self::splitTerms($terms), ...[new DescriptionContinueParser(true, $cursor->getPosition())])
43
                ->at($cursor)
44
                ->replaceActiveBlockParser();
45
        }
46
 
47
        if ($activeBlock instanceof Paragraph && $activeBlock->parent() instanceof Description) {
48
            // Additional description in the same list as the parent description
49
            return BlockStart::of(new DescriptionContinueParser(true, $cursor->getPosition()))->at($cursor);
50
        }
51
 
52
        if ($activeBlock->lastChild() instanceof Paragraph) {
53
            // New description; loose; term(s) sitting in previous closed paragraph block
54
            return BlockStart::of(new DescriptionContinueParser(false, $cursor->getPosition()))->at($cursor);
55
        }
56
 
57
        // No preceding terms
58
        return BlockStart::none();
59
    }
60
 
61
    /**
62
     * @return array<int, DescriptionTermContinueParser>
63
     */
64
    private static function splitTerms(string $terms): array
65
    {
66
        $ret = [];
67
        foreach (\explode("\n", $terms) as $term) {
68
            $ret[] = new DescriptionTermContinueParser($term);
69
        }
70
 
71
        return $ret;
72
    }
73
}