2019-01-20 05:25:04 +03:00
|
|
|
<?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\Html\Renderables\Element;
|
|
|
|
use Erusev\Parsedown\Parsedown;
|
|
|
|
use Erusev\Parsedown\Parsing\Context;
|
|
|
|
use Erusev\Parsedown\Parsing\Line;
|
|
|
|
use Erusev\Parsedown\Parsing\Lines;
|
|
|
|
use Erusev\Parsedown\State;
|
|
|
|
|
|
|
|
final class BlockQuote implements ContinuableBlock
|
|
|
|
{
|
2019-01-21 21:09:27 +03:00
|
|
|
use BlockAcquisition;
|
2019-01-20 05:25:04 +03:00
|
|
|
|
|
|
|
/** @var Lines */
|
|
|
|
private $Lines;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Lines $Lines
|
|
|
|
*/
|
|
|
|
public function __construct($Lines)
|
|
|
|
{
|
|
|
|
$this->Lines = $Lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 (\preg_match('/^(>[ \t]?+)(.*+)/', $Context->line()->text(), $matches)) {
|
|
|
|
$indentOffset = $Context->line()->indentOffset() + $Context->line()->indent() + \strlen($matches[1]);
|
|
|
|
|
|
|
|
$recoveredSpaces = 0;
|
|
|
|
if (\strlen($matches[1]) === 2 && \substr($matches[1], 1, 1) === "\t") {
|
|
|
|
$recoveredSpaces = Line::tabShortage(2, $Context->line()->indentOffset() + $Context->line()->indent());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new self(
|
|
|
|
Lines::fromTextLines(
|
|
|
|
\str_repeat(' ', $recoveredSpaces) . $matches[2],
|
|
|
|
$indentOffset
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Context $Context
|
|
|
|
* @return self|null
|
|
|
|
*/
|
2019-01-20 18:14:57 +03:00
|
|
|
public function advance(Context $Context)
|
2019-01-20 05:25:04 +03:00
|
|
|
{
|
2019-01-21 21:09:27 +03:00
|
|
|
if ($Context->previousEmptyLines() > 0) {
|
2019-01-20 05:25:04 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($Context->line()->text()[0] === '>' and \preg_match('/^(>[ \t]?+)(.*+)/', $Context->line()->text(), $matches)) {
|
|
|
|
$indentOffset = $Context->line()->indentOffset() + $Context->line()->indent() + \strlen($matches[1]);
|
|
|
|
|
|
|
|
$recoveredSpaces = 0;
|
|
|
|
if (\strlen($matches[1]) === 2 && \substr($matches[1], 1, 1) === "\t") {
|
|
|
|
$recoveredSpaces = Line::tabShortage(2, $Context->line()->indentOffset() + $Context->line()->indent());
|
|
|
|
}
|
|
|
|
|
|
|
|
$Lines = $this->Lines->appendingTextLines(
|
|
|
|
\str_repeat(' ', $recoveredSpaces) . $matches[2],
|
|
|
|
$indentOffset
|
|
|
|
);
|
|
|
|
|
|
|
|
return new self($Lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $Context->previousEmptyLines() > 0) {
|
|
|
|
$indentOffset = $Context->line()->indentOffset() + $Context->line()->indent();
|
|
|
|
$Lines = $this->Lines->appendingTextLines($Context->line()->text(), $indentOffset);
|
|
|
|
|
|
|
|
return new self($Lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Handler<Element>
|
|
|
|
*/
|
|
|
|
public function stateRenderable(Parsedown $Parsedown)
|
|
|
|
{
|
|
|
|
return new Handler(
|
|
|
|
/** @return Element */
|
|
|
|
function (State $State) use ($Parsedown) {
|
|
|
|
return new Element(
|
|
|
|
'blockquote',
|
|
|
|
[],
|
|
|
|
$State->applyTo($Parsedown->lines($this->Lines))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|