mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Implement Email
This commit is contained in:
parent
497045d25b
commit
f2a3a2fb08
67
src/Components/Inlines/Email.php
Normal file
67
src/Components/Inlines/Email.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?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 Email implements Inline
|
||||
{
|
||||
use WidthTrait, DefaultBeginPosition;
|
||||
|
||||
/** @var string */
|
||||
private $text;
|
||||
|
||||
/** @var string */
|
||||
private $url;
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
* @param string $url
|
||||
* @param int $width
|
||||
*/
|
||||
public function __construct($text, $url, $width)
|
||||
{
|
||||
$this->text = $text;
|
||||
$this->url = $url;
|
||||
$this->width = $width;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Excerpt $Excerpt
|
||||
* @param State $State
|
||||
* @return static|null
|
||||
*/
|
||||
public static function build(Excerpt $Excerpt, State $State)
|
||||
{
|
||||
$hostnameLabel = '[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?';
|
||||
|
||||
$commonMarkEmail = '[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]++@'
|
||||
. $hostnameLabel . '(?:\.' . $hostnameLabel . ')*';
|
||||
|
||||
if (\strpos($Excerpt->text(), '>') !== false
|
||||
and \preg_match("/^<((mailto:)?$commonMarkEmail)>/i", $Excerpt->text(), $matches)
|
||||
) {
|
||||
$url = $matches[1];
|
||||
|
||||
if (! isset($matches[2])) {
|
||||
$url = "mailto:$url";
|
||||
}
|
||||
|
||||
return new self($matches[1], $url, \strlen($matches[0]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Element
|
||||
*/
|
||||
public function stateRenderable(Parsedown $_)
|
||||
{
|
||||
return new Element('a', ['href' => $this->url], [new Text($this->text)]);
|
||||
}
|
||||
}
|
@ -411,35 +411,6 @@ class Parsedown
|
||||
return $Elements;
|
||||
}
|
||||
|
||||
protected function inlineEmailTag($Excerpt)
|
||||
{
|
||||
$hostnameLabel = '[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?';
|
||||
|
||||
$commonMarkEmail = '[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]++@'
|
||||
. $hostnameLabel . '(?:\.' . $hostnameLabel . ')*';
|
||||
|
||||
if (\strpos($Excerpt['text'], '>') !== false
|
||||
and \preg_match("/^<((mailto:)?$commonMarkEmail)>/i", $Excerpt['text'], $matches)
|
||||
) {
|
||||
$url = $matches[1];
|
||||
|
||||
if (! isset($matches[2])) {
|
||||
$url = "mailto:$url";
|
||||
}
|
||||
|
||||
return [
|
||||
'extent' => \strlen($matches[0]),
|
||||
'element' => [
|
||||
'name' => 'a',
|
||||
'text' => $matches[1],
|
||||
'attributes' => [
|
||||
'href' => $url,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
protected function inlineEmphasis($Excerpt)
|
||||
{
|
||||
if (! isset($Excerpt['text'][1])) {
|
||||
|
Loading…
Reference in New Issue
Block a user