Acquisition capable blocks as an interface

This commit is contained in:
Aidan Woods 2019-02-10 16:33:10 +00:00
parent 6ac6b7f7f7
commit a681cf631c
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
15 changed files with 38 additions and 47 deletions

View File

@ -0,0 +1,13 @@
<?php
namespace Erusev\Parsedown\Components;
interface AcquisitioningBlock extends Block
{
/**
* Return true if the block was built encompassing the previous block
* $Block given to static::build, return false otherwise.
* @return bool
*/
public function acquiredPrevious();
}

View File

@ -19,11 +19,4 @@ interface Block extends Component
Block $Block = null,
State $State = null
);
/**
* Return true if the block was build encompassing the previous block
* $Block given to static::build, return false otherwise.
* @return bool
*/
public function acquiredPrevious();
}

View File

@ -1,15 +0,0 @@
<?php
namespace Erusev\Parsedown\Components\Blocks;
trait BlockAcquisition
{
/** @var bool */
private $acquired = false;
/** @return bool */
public function acquiredPrevious()
{
return $this->acquired;
}
}

View File

@ -16,8 +16,6 @@ use Erusev\Parsedown\State;
final class BlockQuote implements ContinuableBlock
{
use BlockAcquisition;
/** @var Lines */
private $Lines;

View File

@ -12,8 +12,6 @@ use Erusev\Parsedown\State;
final class FencedCode implements ContinuableBlock
{
use BlockAcquisition;
/** @var string */
private $code;

View File

@ -13,8 +13,6 @@ use Erusev\Parsedown\State;
final class Header implements Block
{
use BlockAcquisition;
/** @var string */
private $text;

View File

@ -13,8 +13,6 @@ use Erusev\Parsedown\State;
final class IndentedCode implements ContinuableBlock
{
use BlockAcquisition;
/** @var string */
private $code;

View File

@ -15,8 +15,6 @@ use Erusev\Parsedown\State;
final class Markup implements ContinuableBlock
{
use BlockAcquisition;
const REGEX_HTML_ATTRIBUTE = '[a-zA-Z_:][\w:.-]*+(?:\s*+=\s*+(?:[^"\'=<>`\s]+|"[^"]*+"|\'[^\']*+\'))?+';
/** @var array{2: string, 3: string, 4: string, 5: string} */

View File

@ -13,8 +13,6 @@ use Erusev\Parsedown\State;
final class Paragraph implements ContinuableBlock
{
use BlockAcquisition;
/** @var string */
private $text;

View File

@ -12,8 +12,6 @@ use Erusev\Parsedown\State;
final class Reference implements StateUpdatingBlock
{
use BlockAcquisition;
/** @var State */
private $State;

View File

@ -10,8 +10,6 @@ use Erusev\Parsedown\State;
final class Rule implements Block
{
use BlockAcquisition;
/**
* @param Context $Context
* @param Block|null $Block

View File

@ -4,16 +4,15 @@ namespace Erusev\Parsedown\Components\Blocks;
use Erusev\Parsedown\AST\Handler;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\AcquisitioningBlock;
use Erusev\Parsedown\Components\Block;
use Erusev\Parsedown\Html\Renderables\Element;
use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\State;
final class SetextHeader implements Block
final class SetextHeader implements AcquisitioningBlock
{
use BlockAcquisition;
/** @var string */
private $text;
@ -28,7 +27,6 @@ final class SetextHeader implements Block
{
$this->text = $text;
$this->level = $level;
$this->acquired = true;
}
/**
@ -64,6 +62,12 @@ final class SetextHeader implements Block
return null;
}
/** @return bool */
public function acquiredPrevious()
{
return true;
}
/**
* @return Handler<Element>
*/

View File

@ -15,8 +15,6 @@ use Erusev\Parsedown\State;
final class TList implements ContinuableBlock
{
use BlockAcquisition;
/** @var Lines[] */
private $Lis;

View File

@ -4,6 +4,7 @@ namespace Erusev\Parsedown\Components\Blocks;
use Erusev\Parsedown\AST\Handler;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\AcquisitioningBlock;
use Erusev\Parsedown\Components\Block;
use Erusev\Parsedown\Components\ContinuableBlock;
use Erusev\Parsedown\Html\Renderables\Element;
@ -14,9 +15,10 @@ use Erusev\Parsedown\State;
/**
* @psalm-type _Alignment='left'|'center'|'right'
*/
final class Table implements ContinuableBlock
final class Table implements AcquisitioningBlock, ContinuableBlock
{
use BlockAcquisition;
/** @var bool */
private $acquired;
/** @var array<int, _Alignment|null> */
private $alignments;
@ -165,6 +167,12 @@ final class Table implements ContinuableBlock
return $alignments;
}
/** @return bool */
public function acquiredPrevious()
{
return true;
}
/**
* @return Handler<Element>
*/

View File

@ -3,6 +3,7 @@
namespace Erusev\Parsedown;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\AcquisitioningBlock;
use Erusev\Parsedown\Components\Block;
use Erusev\Parsedown\Components\Blocks\Paragraph;
use Erusev\Parsedown\Components\ContinuableBlock;
@ -113,7 +114,12 @@ final class Parsedown
$State = $Block->latestState();
}
if (isset($CurrentBlock) && ! $Block->acquiredPrevious()) {
if (isset($CurrentBlock)
&& (
! $Block instanceof AcquisitioningBlock
|| ! $Block->acquiredPrevious()
)
) {
$Blocks[] = $CurrentBlock;
}