訣竅是,你的格擋應該始終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它的% - 只是足以讓我的叉子工作,並找出可行的方法:)
你能提供一個你想要解析的Markdown的例子,以及生成的HTML應該是什麼? –
@ ColinO'Dell - 完成! – Kurucu