1<?php
2
3/*
4 * This file is part of the league/commonmark package.
5 *
6 * (c) Colin O'Dell <colinodell@gmail.com>
7 * (c) 2015 Martin Hasoň <martin.hason@gmail.com>
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13declare(strict_types=1);
14
15namespace League\CommonMark\Extension\Attributes\Parser;
16
17use League\CommonMark\Block\Parser\BlockParserInterface;
18use League\CommonMark\ContextInterface;
19use League\CommonMark\Cursor;
20use League\CommonMark\Extension\Attributes\Node\Attributes;
21use League\CommonMark\Extension\Attributes\Util\AttributesHelper;
22
23final class AttributesBlockParser implements BlockParserInterface
24{
25    public function parse(ContextInterface $context, Cursor $cursor): bool
26    {
27        $state = $cursor->saveState();
28        $attributes = AttributesHelper::parseAttributes($cursor);
29        if ($attributes === []) {
30            return false;
31        }
32
33        if ($cursor->getNextNonSpaceCharacter() !== null) {
34            $cursor->restoreState($state);
35
36            return false;
37        }
38
39        $context->addBlock(new Attributes($attributes));
40        $context->setBlocksParsed(true);
41
42        return true;
43    }
44}
45