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

Implement Reference

This commit is contained in:
Aidan Woods 2019-01-20 02:30:12 +00:00
parent 565c8dd3cc
commit 0c730e0dc5
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
2 changed files with 80 additions and 25 deletions

View File

@ -0,0 +1,80 @@
<?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\Parsedown;
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->getOrDefault(DefinitionBook::class)->setting($id, $Data)
);
// if ($id === 'single quotes') {
// var_dump($State);
// }
return new self($State);
}
return null;
}
/** @return State */
public function latestState()
{
return $this->State;
}
/**
* @return Invisible
*/
public function stateRenderable(Parsedown $Parsedown)
{
return new Invisible;
}
}

View File

@ -288,31 +288,6 @@ class Parsedown
return \method_exists($this, 'block' . $Type . 'Complete');
}
#
# Reference
protected function blockReference(Context $Context)
{
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,
];
$this->DefinitionData['Reference'][$id] = $Data;
$Block = [
'element' => [],
];
return $Block;
}
}
#
# Table