Implement Url

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

View File

@ -0,0 +1,74 @@
<?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 Url implements Inline
{
use WidthTrait;
/** @var string */
private $url;
/** @var int */
private $position;
/**
* @param string $url
* @param int $position
*/
public function __construct($url, $position)
{
$this->url = $url;
$this->width = \strlen($url);
$this->position = $position;
}
/**
* @param Excerpt $Excerpt
* @param State $State
* @return static|null
*/
public static function build(Excerpt $Excerpt, State $State)
{
$text = $Excerpt->text();
if (\strlen($text) < 2 or \substr($text, 2, 1) !== '/') {
return null;
}
if (\strpos($Excerpt->context(), 'http') !== false
and \preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $Excerpt->context(), $matches, \PREG_OFFSET_CAPTURE)
) {
return new self($matches[0][0], \intval($matches[0][1]));
}
return null;
}
/**
* Return an integer to declare that the inline should be treated as if it
* started from that position in the excerpt given to static::build.
* Return null to use the excerpt offset value.
* @return int|null
* */
public function modifyStartPositionTo()
{
return $this->position;
}
/**
* @return Element
*/
public function stateRenderable(Parsedown $_)
{
return new Element('a', ['href' => $this->url], [new Text($this->url)]);
}
}

View File

@ -411,33 +411,6 @@ class Parsedown
return $Elements;
}
protected function inlineUrl($Excerpt)
{
if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/') {
return;
}
if (\strpos($Excerpt['context'], 'http') !== false
and \preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $Excerpt['context'], $matches, \PREG_OFFSET_CAPTURE)
) {
$url = $matches[0][0];
$Inline = [
'extent' => \strlen($matches[0][0]),
'position' => $matches[0][1],
'element' => [
'name' => 'a',
'text' => $url,
'attributes' => [
'href' => $url,
],
],
];
return $Inline;
}
}
protected function inlineUrlTag($Excerpt)
{
if (\strpos($Excerpt['text'], '>') !== false and \preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $Excerpt['text'], $matches)) {