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

Implement Header

This commit is contained in:
Aidan Woods 2019-01-20 02:27:10 +00:00
parent 57c6350184
commit 07c2566042
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
2 changed files with 84 additions and 33 deletions

View File

@ -0,0 +1,84 @@
<?php
namespace Erusev\Parsedown\Components\Blocks;
use Erusev\Parsedown\AST\Handler;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\Block;
use Erusev\Parsedown\Configurables\StrictMode;
use Erusev\Parsedown\Html\Renderables\Element;
use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\State;
final class Header implements Block
{
use BlockAcquisition;
/** @var string */
private $text;
/** @var 1|2|3|4|5|6 */
private $level;
/**
* @param string $text
* @param 1|2|3|4|5|6 $level
*/
public function __construct($text, $level)
{
$this->text = $text;
$this->level = $level;
}
/**
* @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
) {
$State = $State ?: new State;
$level = \strspn($Context->line()->text(), '#');
if ($level > 6 || $level < 1) {
return null;
}
/** @var 1|2|3|4|5|6 $level */
$text = \trim($Context->line()->text(), '#');
$StrictMode = $State->getOrDefault(StrictMode::class);
if ($StrictMode->enabled() && isset($text[0]) and $text[0] !== ' ') {
return null;
}
$text = \trim($text, ' ');
return new self($text, $level);
}
/**
* @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))
);
}
);
}
}

View File

@ -334,39 +334,6 @@ class Parsedown
return $Block;
}
#
# Header
protected function blockHeader(Context $Context)
{
$level = \strspn($Context->line()->text(), '#');
if ($level > 6) {
return;
}
$text = \trim($Context->line()->text(), '#');
if ($this->strictMode and isset($text[0]) and $text[0] !== ' ') {
return;
}
$text = \trim($text, ' ');
$Block = [
'element' => [
'name' => 'h' . $level,
'handler' => [
'function' => 'lineElements',
'argument' => $text,
'destination' => 'elements',
]
],
];
return $Block;
}
#
# List