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

Implement IndentedCode

This commit is contained in:
Aidan Woods 2019-01-20 02:29:40 +00:00
parent ee094cb397
commit 565c8dd3cc
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
2 changed files with 81 additions and 46 deletions

View File

@ -0,0 +1,81 @@
<?php
namespace Erusev\Parsedown\Components\Blocks;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\Block;
use Erusev\Parsedown\Components\ContinuableBlock;
use Erusev\Parsedown\Html\Renderables\Element;
use Erusev\Parsedown\Html\Renderables\Text;
use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\State;
final class IndentedCode implements ContinuableBlock
{
use ContinuableBlockDefaultInterrupt, BlockAcquisition;
/** @var string */
private $code;
/**
* @param string $code
*/
public function __construct($code)
{
$this->code = $code;
}
/**
* @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 (isset($Block) and $Block instanceof Paragraph and ! $Context->previousEmptyLines() > 0) {
return null;
}
if ($Context->line()->indent() < 4) {
return null;
}
return new self($Context->line()->ltrimBodyUpto(4));
}
/**
* @param Context $Context
* @return self|null
*/
public function continue(Context $Context)
{
if ($Context->line()->indent() < 4) {
return null;
}
$newCode = $this->code;
if ($Context->previousEmptyLines() > 0) {
$newCode .= \str_repeat("\n", $Context->previousEmptyLines());
}
return new self($newCode . "\n" . $Context->line()->ltrimBodyUpto(4));
}
/**
* @return Element
*/
public function stateRenderable(Parsedown $_)
{
return new Element(
'pre',
[],
[new Element('code', [], [new Text($this->code)])]
);
}
}

View File

@ -288,52 +288,6 @@ class Parsedown
return \method_exists($this, 'block' . $Type . 'Complete');
}
#
# Code
protected function blockCode(Context $Context, $Block = null)
{
if (isset($Block) and $Block['type'] === 'Paragraph' and ! $Context->previousEmptyLines() > 0) {
return;
}
if ($Context->line()->indent() >= 4) {
$Block = [
'element' => [
'name' => 'pre',
'element' => [
'name' => 'code',
'text' => $Context->line()->ltrimBodyUpto(4),
],
],
];
return $Block;
}
}
protected function blockCodeContinue(Context $Context, $Block)
{
if ($Context->line()->indent() >= 4) {
if ($Context->previousEmptyLines() > 0) {
$Block['element']['element']['text'] .= \str_repeat("\n", $Context->previousEmptyLines());
unset($Block['interrupted']);
}
$Block['element']['element']['text'] .= "\n";
$Block['element']['element']['text'] .= $Context->line()->ltrimBodyUpto(4);
return $Block;
}
}
protected function blockCodeComplete($Block)
{
return $Block;
}
#
# Reference