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

76 lines
1.7 KiB
PHP
Raw Normal View History

2019-01-20 05:30:12 +03:00
<?php
namespace Erusev\Parsedown\Components\Blocks;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\Block;
use Erusev\Parsedown\Components\StateUpdatingBlock;
use Erusev\Parsedown\Configurables\DefinitionBook;
use Erusev\Parsedown\Html\Renderables\Invisible;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\State;
final class Reference implements StateUpdatingBlock
{
use BlockAcquisition;
/** @var State */
private $State;
public function __construct(State $State)
{
$this->State = $State;
}
/**
* @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;
if (\strpos($Context->line()->text(), ']') !== false
and \preg_match(
'/^\[(.+?)\]:[ ]*+<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*+$/',
$Context->line()->text(),
$matches
)
) {
$id = \strtolower($matches[1]);
$Data = [
'url' => $matches[2],
'title' => isset($matches[3]) ? $matches[3] : null,
];
$State = $State->setting(
$State->get(DefinitionBook::class)->setting($id, $Data)
2019-01-20 05:30:12 +03:00
);
return new self($State);
}
return null;
}
/** @return State */
public function latestState()
{
return $this->State;
}
/**
* @return Invisible
*/
public function stateRenderable()
2019-01-20 05:30:12 +03:00
{
return new Invisible;
}
}