| 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 |
* (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 |
|
|
|
13 |
declare(strict_types=1);
|
|
|
14 |
|
|
|
15 |
namespace League\CommonMark\Extension\Attributes\Parser;
|
|
|
16 |
|
|
|
17 |
use League\CommonMark\Extension\Attributes\Node\AttributesInline;
|
|
|
18 |
use League\CommonMark\Extension\Attributes\Util\AttributesHelper;
|
|
|
19 |
use League\CommonMark\Node\StringContainerInterface;
|
|
|
20 |
use League\CommonMark\Parser\Inline\InlineParserInterface;
|
|
|
21 |
use League\CommonMark\Parser\Inline\InlineParserMatch;
|
|
|
22 |
use League\CommonMark\Parser\InlineParserContext;
|
|
|
23 |
|
|
|
24 |
final class AttributesInlineParser implements InlineParserInterface
|
|
|
25 |
{
|
|
|
26 |
public function getMatchDefinition(): InlineParserMatch
|
|
|
27 |
{
|
|
|
28 |
return InlineParserMatch::string('{');
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
public function parse(InlineParserContext $inlineContext): bool
|
|
|
32 |
{
|
|
|
33 |
$cursor = $inlineContext->getCursor();
|
|
|
34 |
$char = (string) $cursor->peek(-1);
|
|
|
35 |
|
|
|
36 |
$attributes = AttributesHelper::parseAttributes($cursor);
|
|
|
37 |
if ($attributes === []) {
|
|
|
38 |
return false;
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
if ($char === ' ' && ($prev = $inlineContext->getContainer()->lastChild()) instanceof StringContainerInterface) {
|
|
|
42 |
$prev->setLiteral(\rtrim($prev->getLiteral(), ' '));
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
if ($char === '') {
|
|
|
46 |
$cursor->advanceToNextNonSpaceOrNewline();
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
$node = new AttributesInline($attributes, $char === ' ' || $char === '');
|
|
|
50 |
$inlineContext->getContainer()->appendChild($node);
|
|
|
51 |
|
|
|
52 |
return true;
|
|
|
53 |
}
|
|
|
54 |
}
|