mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Implement Comment
This commit is contained in:
89
src/Components/Blocks/Comment.php
Normal file
89
src/Components/Blocks/Comment.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -334,46 +334,6 @@ class Parsedown
|
|||||||
return $Block;
|
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
|
# Fenced Code
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user