我正在尝试使用 League 的CommonMark 包创建一个扰流板。
该块由三个倒置的感叹号打开,可选地后跟摘要;三个正常的感叹号结束该块。
这是我到目前为止的代码:
元素
<?php
use League\CommonMark\Block\Element\AbstractBlock;
use League\CommonMark\Cursor;
class Spoiler extends AbstractBlock {
private $summary;
public function __construct($summary = null) {
parent::__construct();
$this->summary = $summary;
}
public function getSummary() { return $this->summary; }
public function canContain(AbstractBlock $block) { return true; }
public function acceptsLines() { return true; }
public function isCode() { return false; }
public function matchesNextLine(Cursor $cursor) {
if ($cursor->match('(^!!!$)')) {
$this->lastLineBlank = true;
return false;
}
return true;
}
}
解析器
<?php
use League\CommonMark\Block\Parser\AbstractBlockParser;
use League\CommonMark\ContextInterface;
use League\CommonMark\Cursor;
class SpoilerParser extends AbstractBlockParser {
public function parse(ContextInterface $context, Cursor $cursor) {
if ($cursor->isIndented()) return false;
$previousState = $cursor->saveState();
$spoiler = $cursor->match('(^¡¡¡(\s*.+)?)');
if (is_null($spoiler)) {
$cursor->restoreState($previousState);
return false;
}
$summary = trim(mb_substr($spoiler, mb_strlen('¡¡¡')));
if ($summary !== '') {
$context->addBlock(new Spoiler($summary));
} else {
$context->addBlock(new Spoiler());
}
return true;
}
}
渲染器
<?php
use League\CommonMark\Block\Element\AbstractBlock;
use League\CommonMark\Block\Renderer\BlockRendererInterface;
use League\CommonMark\ElementRendererInterface;
use League\CommonMark\HtmlElement;
class SpoilerRenderer implements BlockRendererInterface {
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false) {
if (!($block instanceof Spoiler)) throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
$summary = new HtmlElement('summary', [], $block->getSummary() ?: 'Click to expand spoiler');
$content = $summary . "\n" . $htmlRenderer->renderBlocks($block->children());
return new HtmlElement('details', [], $content);
}
}
当我嵌套多个扰流块时会出现问题:第一个终止符关闭所有块。
¡¡¡
1
¡¡¡
2
¡¡¡
Hello
!!!
3
!!!
4
!!!
这是解析的 AST:
League\CommonMark\Block\Element\Document
App\Helpers\Formatting\Element\Spoiler
League\CommonMark\Block\Element\Paragraph
League\CommonMark\Inline\Element\Text "1"
App\Helpers\Formatting\Element\Spoiler
League\CommonMark\Block\Element\Paragraph
League\CommonMark\Inline\Element\Text "2"
App\Helpers\Formatting\Element\Spoiler
League\CommonMark\Block\Element\Paragraph
League\CommonMark\Inline\Element\Text "Hello"
League\CommonMark\Block\Element\Paragraph
League\CommonMark\Inline\Element\Text "3"
League\CommonMark\Inline\Element\Newline
League\CommonMark\Inline\Element\Text "!!!"
League\CommonMark\Inline\Element\Newline
League\CommonMark\Inline\Element\Text "4"
League\CommonMark\Inline\Element\Newline
League\CommonMark\Inline\Element\Text "!!!"
这是预期的 AST:
League\CommonMark\Block\Element\Document
App\Helpers\Formatting\Element\Spoiler
League\CommonMark\Block\Element\Paragraph
League\CommonMark\Inline\Element\Text "1"
App\Helpers\Formatting\Element\Spoiler
League\CommonMark\Block\Element\Paragraph
League\CommonMark\Inline\Element\Text "2"
App\Helpers\Formatting\Element\Spoiler
League\CommonMark\Block\Element\Paragraph
League\CommonMark\Inline\Element\Text "Hello"
League\CommonMark\Block\Element\Paragraph
League\CommonMark\Inline\Element\Text "3"
League\CommonMark\Block\Element\Paragraph
League\CommonMark\Inline\Element\Text "4"