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

Implement UrlTag

This commit is contained in:
Aidan Woods 2019-01-20 02:37:40 +00:00
parent 53bb9a6467
commit a286033f52
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
2 changed files with 51 additions and 18 deletions

View File

@ -0,0 +1,51 @@
<?php
namespace Erusev\Parsedown\Components\Inlines;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\Inline;
use Erusev\Parsedown\Html\Renderables\Element;
use Erusev\Parsedown\Html\Renderables\Text;
use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
final class UrlTag implements Inline
{
use WidthTrait, DefaultBeginPosition;
/** @var string */
private $url;
/**
* @param string $url
* @param int $width
*/
public function __construct($url, $width)
{
$this->url = $url;
$this->width = $width;
}
/**
* @param Excerpt $Excerpt
* @param State $State
* @return static|null
*/
public static function build(Excerpt $Excerpt, State $State)
{
if (\strpos($Excerpt->text(), '>') !== false and \preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $Excerpt->text(), $matches)) {
return new self($matches[1], \strlen($matches[0]));
}
return null;
}
/**
* @return Element
*/
public function stateRenderable(Parsedown $_)
{
return new Element('a', ['href' => $this->url], [new Text($this->url)]);
}
}

View File

@ -411,24 +411,6 @@ class Parsedown
return $Elements;
}
protected function inlineUrlTag($Excerpt)
{
if (\strpos($Excerpt['text'], '>') !== false and \preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $Excerpt['text'], $matches)) {
$url = $matches[1];
return [
'extent' => \strlen($matches[0]),
'element' => [
'name' => 'a',
'text' => $url,
'attributes' => [
'href' => $url,
],
],
];
}
}
# ~
protected function unmarkedText($text)