mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Implement SetextHeader
This commit is contained in:
parent
af97e99b39
commit
edc004f503
74
src/Components/Blocks/SetextHeader.php
Normal file
74
src/Components/Blocks/SetextHeader.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Erusev\Parsedown\Components\Blocks;
|
||||
|
||||
use Erusev\Parsedown\AST\Handler;
|
||||
use Erusev\Parsedown\AST\StateRenderable;
|
||||
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
|
||||
{
|
||||
use BlockAcquisition;
|
||||
|
||||
/** @var string */
|
||||
private $text;
|
||||
|
||||
/** @var 1|2 */
|
||||
private $level;
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
* @param 1|2 $level
|
||||
*/
|
||||
public function __construct($text, $level)
|
||||
{
|
||||
$this->text = $text;
|
||||
$this->level = $level;
|
||||
$this->acquired = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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) || ! $Block instanceof Paragraph || $Context->previousEmptyLines() > 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($Context->line()->indent() < 4 and \chop(\chop($Context->line()->text(), " \t"), $Context->line()->text()[0]) === '') {
|
||||
$level = $Context->line()->text()[0] === '=' ? 1 : 2;
|
||||
|
||||
return new self($Block->text(), $level);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Handler<Element>
|
||||
*/
|
||||
public function stateRenderable(Parsedown $Parsedown)
|
||||
{
|
||||
return new Handler(
|
||||
/** @return Element */
|
||||
function (State $State) use ($Parsedown) {
|
||||
return new Element(
|
||||
'h' . \strval($this->level),
|
||||
[],
|
||||
$State->applyTo($Parsedown->lineElements($this->text))
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
@ -334,22 +334,6 @@ class Parsedown
|
||||
return $Block;
|
||||
}
|
||||
|
||||
#
|
||||
# Setext
|
||||
|
||||
protected function blockSetextHeader(Context $Context, array $Block = null)
|
||||
{
|
||||
if (! isset($Block) or $Block['type'] !== 'Paragraph' or $Context->previousEmptyLines() > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($Context->line()->indent() < 4 and \chop(\chop($Context->line()->text(), " \t"), $Context->line()->text()[0]) === '') {
|
||||
$Block['element']['name'] = $Context->line()->text()[0] === '=' ? 'h1' : 'h2';
|
||||
|
||||
return $Block;
|
||||
}
|
||||
}
|
||||
|
||||
#
|
||||
# Markup
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user