Skip to content

Commit b6b231e

Browse files
committed
Fix rendering
1 parent bb0dd93 commit b6b231e

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

src/Markdown/Block/ContextSpecificBlock.php

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
use League\CommonMark\Block\Element\AbstractStringContainerBlock;
1111
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
1212
use PhpSchool\PhpWorkshop\Markdown\Parser\ContextSpecificBlockParser;
13+
use League\CommonMark\Block\Element\InlineContainerInterface;
1314

14-
final class ContextSpecificBlock extends AbstractStringContainerBlock
15+
final class ContextSpecificBlock extends AbstractBlock
1516
{
1617
/**
1718
* @var string
@@ -25,8 +26,6 @@ public function __construct(string $type)
2526
}
2627

2728
$this->type = $type;
28-
29-
parent::__construct();
3029
}
3130

3231
public function getType(): string
@@ -36,36 +35,17 @@ public function getType(): string
3635

3736
public function canContain(AbstractBlock $block): bool
3837
{
39-
return false;
38+
return true;
4039
}
4140

4241
public function isCode(): bool
4342
{
44-
return true;
43+
return false;
4544
}
4645

4746
public function matchesNextLine(Cursor $cursor): bool
4847
{
49-
return true;
50-
}
51-
52-
public function finalize(ContextInterface $context, int $endLineNumber)
53-
{
54-
parent::finalize($context, $endLineNumber);
55-
56-
$this->finalStringContents = \implode("\n", $this->strings->toArray());
57-
}
58-
59-
public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
60-
{
61-
$tagged = $cursor->match(ContextSpecificBlockParser::getParserRegex());
62-
if ($tagged !== null) {
63-
$this->finalize($context, $context->getLineNumber());
64-
return;
65-
}
66-
67-
/** @var self $tip */
68-
$tip = $context->getTip();
69-
$tip->addLine($cursor->getRemainder());
48+
$content = $cursor->match(ContextSpecificBlockParser::getEndBlockRegex($this->type));
49+
return $content === null;
7050
}
7151
}

src/Markdown/Parser/ContextSpecificBlockParser.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpSchool\PhpWorkshop\Markdown\Parser;
66

77
use League\CommonMark\Block\Parser\BlockParserInterface;
8+
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
89
use PhpSchool\PhpWorkshop\Markdown\Block\ContextSpecificBlock;
910
use League\CommonMark\ContextInterface;
1011
use League\CommonMark\Cursor;
@@ -19,6 +20,15 @@ public static function getParserRegex(): string
1920
return '/^{{\s?(' . implode('|', self::TYPES) . ')\s?}}/';
2021
}
2122

23+
public static function getEndBlockRegex(string $type): string
24+
{
25+
if (!in_array($type, self::TYPES, true)) {
26+
throw InvalidArgumentException::notValidParameter('type', self::TYPES, $type);
27+
}
28+
29+
return '/^{{\s?(' . $type . ')\s?}}/';
30+
}
31+
2232
public function parse(ContextInterface $context, Cursor $cursor): bool
2333
{
2434
if ($cursor->getCharacter() !== '{') {
@@ -34,8 +44,6 @@ public function parse(ContextInterface $context, Cursor $cursor): bool
3444

3545
$context->addBlock(new ContextSpecificBlock($type));
3646

37-
// TODO: How to handle the entire block?!?
38-
3947
return true;
4048
}
4149
}

src/Markdown/Renderer/ContextSpecificRenderer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function render(AbstractBlock $block, ElementRendererInterface $renderer,
3535
return '';
3636
}
3737

38-
return $renderer->renderInlines($block->children());
38+
/** @var iterable<AbstractBlock> $children */
39+
$children = $block->children();
40+
return $renderer->renderBlocks($children);
3941
}
4042
}

0 commit comments

Comments
 (0)