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

Implement Code

This commit is contained in:
Aidan Woods
2019-01-20 02:33:45 +00:00
parent 760945008b
commit 497045d25b
2 changed files with 61 additions and 18 deletions

View File

@@ -0,0 +1,61 @@
<?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 Code implements Inline
{
use WidthTrait, DefaultBeginPosition;
/** @var string */
private $text;
/**
* @param string $text
* @param int $width
*/
public function __construct($text, $width)
{
$this->text = $text;
$this->width = $width;
}
/**
* @param Excerpt $Excerpt
* @param State $State
* @return static|null
*/
public static function build(Excerpt $Excerpt, State $State)
{
$marker = $Excerpt->text()[0];
if ($marker !== '`') {
return null;
}
if (\preg_match(
'/^(['.$marker.']++)[ ]*+(.+?)[ ]*+(?<!['.$marker.'])\1(?!'.$marker.')/s',
$Excerpt->text(),
$matches
)) {
$text = \preg_replace('/[ ]*+\n/', ' ', $matches[2]);
return new self($text, \strlen($matches[0]));
}
}
/**
* @return Element
*/
public function stateRenderable(Parsedown $_)
{
return new Element('code', [], [new Text($this->text)]);
}
}