| 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\FencedCode;
|
|
|
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 |
use League\CommonMark\Util\RegexHelper;
|
|
|
23 |
|
|
|
24 |
final class FencedCodeParser extends AbstractBlockContinueParser
|
|
|
25 |
{
|
|
|
26 |
/** @psalm-readonly */
|
|
|
27 |
private FencedCode $block;
|
|
|
28 |
|
|
|
29 |
/** @var ArrayCollection<string> */
|
|
|
30 |
private ArrayCollection $strings;
|
|
|
31 |
|
|
|
32 |
public function __construct(int $fenceLength, string $fenceChar, int $fenceOffset)
|
|
|
33 |
{
|
|
|
34 |
$this->block = new FencedCode($fenceLength, $fenceChar, $fenceOffset);
|
|
|
35 |
$this->strings = new ArrayCollection();
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public function getBlock(): FencedCode
|
|
|
39 |
{
|
|
|
40 |
return $this->block;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
|
|
|
44 |
{
|
|
|
45 |
// Check for closing code fence
|
|
|
46 |
if (! $cursor->isIndented() && $cursor->getNextNonSpaceCharacter() === $this->block->getChar()) {
|
|
|
47 |
$match = RegexHelper::matchFirst('/^(?:`{3,}|~{3,})(?= *$)/', $cursor->getLine(), $cursor->getNextNonSpacePosition());
|
|
|
48 |
if ($match !== null && \strlen($match[0]) >= $this->block->getLength()) {
|
|
|
49 |
// closing fence - we're at end of line, so we can finalize now
|
|
|
50 |
return BlockContinue::finished();
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
// Skip optional spaces of fence offset
|
|
|
55 |
// Optimization: don't attempt to match if we're at a non-space position
|
|
|
56 |
if ($cursor->getNextNonSpacePosition() > $cursor->getPosition()) {
|
|
|
57 |
$cursor->match('/^ {0,' . $this->block->getOffset() . '}/');
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
return BlockContinue::at($cursor);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
public function addLine(string $line): void
|
|
|
64 |
{
|
|
|
65 |
$this->strings[] = $line;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public function closeBlock(): void
|
|
|
69 |
{
|
|
|
70 |
// first line becomes info string
|
|
|
71 |
$firstLine = $this->strings->first();
|
|
|
72 |
if ($firstLine === false) {
|
|
|
73 |
$firstLine = '';
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
$this->block->setInfo(RegexHelper::unescape(\trim($firstLine)));
|
|
|
77 |
|
|
|
78 |
if ($this->strings->count() === 1) {
|
|
|
79 |
$this->block->setLiteral('');
|
|
|
80 |
} else {
|
|
|
81 |
$this->block->setLiteral(\implode("\n", $this->strings->slice(1)) . "\n");
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
}
|