トリックは、あなたのブロックは、常にreturn true;
canContain()
とmatchesNextLine()
メソッドを持っていなければならないということである - これらを次の行を常に得ることが保証されます子ブロックとして追加されました。 (FencedCode
とListBlock
の実装を見てみましょう。)
はここで働いべきいくつかのコードです:
ObjectBlock.php:
class ObjectBlock extends AbstractBlock
{
private $objectId;
public function __construct($objectId)
{
$this->objectId = $objectId;
}
public function getObjectId()
{
return $this->objectId;
}
public function canContain(AbstractBlock $block)
{
return true;
}
public function acceptsLines()
{
return false;
}
public function isCode()
{
return false;
}
public function matchesNextLine(Cursor $cursor)
{
return true;
}
}
ObjectParser.php:
class ObjectParser extends AbstractBlockParser
{
public function parse(ContextInterface $context, Cursor $cursor)
{
// Look for the starting syntax
if ($cursor->match('/^{{ /')) {
$id = $cursor->getRemainder();
$cursor->advanceToEnd();
$context->addBlock(new ObjectBlock($id));
return true;
// Look for the ending syntax
} elseif ($cursor->match('/^}} +$/')) {
// TODO: I don't know if this is the best approach, but it should work
// Basically, we're going to locate a parent ObjectBlock in the AST...
$container = $context->getContainer();
while ($container) {
if ($container instanceof ObjectBlock) {
$cursor->advanceToEnd();
// Found it! Now we'll close everything up to (and including) it
$context->getBlockCloser()->setLastMatchedContainer($container->parent());
$context->getBlockCloser()->closeUnmatchedBlocks();
$context->setBlocksParsed(true);
return true;
}
$container = $container->parent();
}
}
return false;
}
}
Objec tRenderer:
class ObjectRenderer implements BlockRendererInterface
{
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
{
$span = sprintf('<span>%s</span>', $block->getObjectId());
$contents = $htmlRenderer->renderBlocks($block->children());
return new HtmlElement('div', ['class' => 'object'],
$span . $contents
);
}
}
免責事項:私はこのライブラリの作者だが、特定のロジック(コンテナ、ヒント、およびブロック・クロージング)は、主にJS版からそのままでフォークれたと私はたったの約75の理解%of - それだけで十分な私のフォークを維持し、働くアプローチを理解してください:)
あなたは解析したいMarkdownの例と、結果のHTMLはどんなものになりますか? –
@ ColinO'Dell - 完了! – Kurucu