1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Implement Comment

This commit is contained in:
Aidan Woods 2019-01-20 02:26:03 +00:00
parent c50deda690
commit 194c916c6a
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
2 changed files with 89 additions and 40 deletions

View File

@ -0,0 +1,89 @@
<?php
namespace Erusev\Parsedown\Components\Blocks;
use Erusev\Parsedown\AST\Handler;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\Block;
use Erusev\Parsedown\Components\ContinuableBlock;
use Erusev\Parsedown\Configurables\SafeMode;
use Erusev\Parsedown\Html\Renderables\RawHtml;
use Erusev\Parsedown\Html\Renderables\Text;
use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\State;
final class Comment implements ContinuableBlock
{
use ContinuableBlockDefaultInterrupt, BlockAcquisition;
/** @var string */
private $html;
/** @var bool */
private $isClosed;
/**
* @param string $html
* @param bool $isClosed
*/
public function __construct($html, $isClosed)
{
$this->html = $html;
$this->isClosed = $isClosed;
}
/**
* @param Context $Context
* @param Block|null $Block
* @param State|null $State
* @return static|null
*/
public static function build(
Context $Context,
Block $Block = null,
State $State = null
) {
if (\strpos($Context->line()->text(), '<!--') === 0) {
return new self(
$Context->line()->rawLine(),
\strpos($Context->line()->text(), '-->') !== false
);
}
return null;
}
/**
* @param Context $Context
* @return self|null
*/
public function continue(Context $Context)
{
if ($this->isClosed) {
return null;
}
return new self(
$this->html . "\n" . $Context->line()->rawLine(),
\strpos($Context->line()->text(), '-->') !== false
);
}
/**
* @return Handler<Text|RawHtml>
*/
public function stateRenderable(Parsedown $Parsedown)
{
return new Handler(
/** @return Text|RawHtml */
function (State $State) {
if ($State->getOrDefault(SafeMode::class)->enabled()) {
return new Text($this->html);
} else {
return new RawHtml($this->html);
}
}
);
}
}

View File

@ -334,46 +334,6 @@ class Parsedown
return $Block;
}
#
# Comment
protected function blockComment(Context $Context)
{
if ($this->markupEscaped or $this->safeMode) {
return;
}
if (\strpos($Context->line()->text(), '<!--') === 0) {
$Block = [
'element' => [
'rawHtml' => $Context->line()->rawLine(),
'autobreak' => true,
],
];
if (\strpos($Context->line()->text(), '-->') !== false) {
$Block['closed'] = true;
}
return $Block;
}
}
protected function blockCommentContinue(Context $Context, array $Block)
{
if (isset($Block['closed'])) {
return;
}
$Block['element']['rawHtml'] .= "\n" . $Context->line()->rawLine();
if (\strpos($Context->line()->text(), '-->') !== false) {
$Block['closed'] = true;
}
return $Block;
}
#
# Fenced Code