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\Parser\Block\BlockStart;
17
use League\CommonMark\Parser\Block\BlockStartParserInterface;
18
use League\CommonMark\Parser\Cursor;
19
use League\CommonMark\Parser\MarkdownParserStateInterface;
20
use League\CommonMark\Util\RegexHelper;
21
 
22
class HeadingStartParser implements BlockStartParserInterface
23
{
24
    public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
25
    {
26
        if ($cursor->isIndented() || ! \in_array($cursor->getNextNonSpaceCharacter(), ['#', '-', '='], true)) {
27
            return BlockStart::none();
28
        }
29
 
30
        $cursor->advanceToNextNonSpaceOrTab();
31
 
32
        if ($atxHeading = self::getAtxHeader($cursor)) {
33
            return BlockStart::of($atxHeading)->at($cursor);
34
        }
35
 
36
        $setextHeadingLevel = self::getSetextHeadingLevel($cursor);
37
        if ($setextHeadingLevel > 0) {
38
            $content = $parserState->getParagraphContent();
39
            if ($content !== null) {
40
                $cursor->advanceToEnd();
41
 
42
                return BlockStart::of(new HeadingParser($setextHeadingLevel, $content))
43
                    ->at($cursor)
44
                    ->replaceActiveBlockParser();
45
            }
46
        }
47
 
48
        return BlockStart::none();
49
    }
50
 
51
    private static function getAtxHeader(Cursor $cursor): ?HeadingParser
52
    {
53
        $match = RegexHelper::matchFirst('/^#{1,6}(?:[ \t]+|$)/', $cursor->getRemainder());
54
        if (! $match) {
55
            return null;
56
        }
57
 
58
        $cursor->advanceToNextNonSpaceOrTab();
59
        $cursor->advanceBy(\strlen($match[0]));
60
 
61
        $level = \strlen(\trim($match[0]));
62
        $str   = $cursor->getRemainder();
63
        $str   = \preg_replace('/^[ \t]*#+[ \t]*$/', '', $str);
64
        \assert(\is_string($str));
65
        $str = \preg_replace('/[ \t]+#+[ \t]*$/', '', $str);
66
        \assert(\is_string($str));
67
 
68
        return new HeadingParser($level, $str);
69
    }
70
 
71
    private static function getSetextHeadingLevel(Cursor $cursor): int
72
    {
73
        $match = RegexHelper::matchFirst('/^(?:=+|-+)[ \t]*$/', $cursor->getRemainder());
74
        if ($match === null) {
75
            return 0;
76
        }
77
 
78
        return $match[0][0] === '=' ? 1 : 2;
79
    }
80
}