mirror of
https://github.com/erusev/parsedown.git
synced 2024-09-20 03:51:29 +02:00
Acquisition capable blocks as an interface
This commit is contained in:
parent
6ac6b7f7f7
commit
a681cf631c
13
src/Components/AcquisitioningBlock.php
Normal file
13
src/Components/AcquisitioningBlock.php
Normal 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();
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -16,8 +16,6 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class BlockQuote implements ContinuableBlock
|
||||
{
|
||||
use BlockAcquisition;
|
||||
|
||||
/** @var Lines */
|
||||
private $Lines;
|
||||
|
||||
|
@ -12,8 +12,6 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class FencedCode implements ContinuableBlock
|
||||
{
|
||||
use BlockAcquisition;
|
||||
|
||||
/** @var string */
|
||||
private $code;
|
||||
|
||||
|
@ -13,8 +13,6 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class Header implements Block
|
||||
{
|
||||
use BlockAcquisition;
|
||||
|
||||
/** @var string */
|
||||
private $text;
|
||||
|
||||
|
@ -13,8 +13,6 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class IndentedCode implements ContinuableBlock
|
||||
{
|
||||
use BlockAcquisition;
|
||||
|
||||
/** @var string */
|
||||
private $code;
|
||||
|
||||
|
@ -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} */
|
||||
|
@ -13,8 +13,6 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class Paragraph implements ContinuableBlock
|
||||
{
|
||||
use BlockAcquisition;
|
||||
|
||||
/** @var string */
|
||||
private $text;
|
||||
|
||||
|
@ -12,8 +12,6 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class Reference implements StateUpdatingBlock
|
||||
{
|
||||
use BlockAcquisition;
|
||||
|
||||
/** @var State */
|
||||
private $State;
|
||||
|
||||
|
@ -10,8 +10,6 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class Rule implements Block
|
||||
{
|
||||
use BlockAcquisition;
|
||||
|
||||
/**
|
||||
* @param Context $Context
|
||||
* @param Block|null $Block
|
||||
|
@ -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>
|
||||
*/
|
||||
|
@ -15,8 +15,6 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class TList implements ContinuableBlock
|
||||
{
|
||||
use BlockAcquisition;
|
||||
|
||||
/** @var Lines[] */
|
||||
private $Lis;
|
||||
|
||||
|
@ -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>
|
||||
*/
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user