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
|
|
|
|
{
|
|
|
|
/** @var State */
|
|
|
|
private $State;
|
|
|
|
|
2019-02-16 15:37:22 +03:00
|
|
|
private function __construct(State $State)
|
2019-01-20 05:30:12 +03:00
|
|
|
{
|
|
|
|
$this->State = $State;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Context $Context
|
2019-02-10 21:32:28 +03:00
|
|
|
* @param State $State
|
2019-01-20 05:30:12 +03:00
|
|
|
* @param Block|null $Block
|
|
|
|
* @return static|null
|
|
|
|
*/
|
|
|
|
public static function build(
|
|
|
|
Context $Context,
|
2019-02-10 21:32:28 +03:00
|
|
|
State $State,
|
|
|
|
Block $Block = null
|
2019-01-20 05:30:12 +03:00
|
|
|
) {
|
2019-02-10 20:49:26 +03:00
|
|
|
if (\preg_match(
|
|
|
|
'/^\[(.+?)\]:[ ]*+<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*+$/',
|
|
|
|
$Context->line()->text(),
|
|
|
|
$matches
|
|
|
|
)) {
|
2019-01-20 05:30:12 +03:00
|
|
|
$id = \strtolower($matches[1]);
|
|
|
|
|
|
|
|
$Data = [
|
|
|
|
'url' => $matches[2],
|
|
|
|
'title' => isset($matches[3]) ? $matches[3] : null,
|
|
|
|
];
|
|
|
|
|
|
|
|
$State = $State->setting(
|
2019-01-25 22:48:03 +03:00
|
|
|
$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
|
|
|
|
*/
|
2019-01-26 00:39:37 +03:00
|
|
|
public function stateRenderable()
|
2019-01-20 05:30:12 +03:00
|
|
|
{
|
|
|
|
return new Invisible;
|
|
|
|
}
|
|
|
|
}
|