mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Add hard and soft breaks
This commit is contained in:
91
src/Components/Inlines/HardBreak.php
Normal file
91
src/Components/Inlines/HardBreak.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?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 HardBreak implements Inline
|
||||
{
|
||||
use WidthTrait;
|
||||
|
||||
/** @var int */
|
||||
private $position;
|
||||
|
||||
/**
|
||||
* @param int $width
|
||||
* @param int $position
|
||||
*/
|
||||
public function __construct($width, $position)
|
||||
{
|
||||
$this->width = $width;
|
||||
$this->position = $position;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Excerpt $Excerpt
|
||||
* @param State $State
|
||||
* @return static|null
|
||||
*/
|
||||
public static function build(Excerpt $Excerpt, State $State)
|
||||
{
|
||||
$context = $Excerpt->context();
|
||||
$offset = $Excerpt->offset();
|
||||
|
||||
if ($offset < 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (\substr($context, $offset -1, 1) === '\\') {
|
||||
$trimTrailingWhitespace = \rtrim(\substr($context, 0, $offset -1));
|
||||
$contentLen = \strlen($trimTrailingWhitespace);
|
||||
|
||||
return new self($offset - $contentLen, $contentLen);
|
||||
}
|
||||
|
||||
if ($offset < 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (\substr($context, $offset -2, 2) === ' ') {
|
||||
$trimTrailingWhitespace = \rtrim(\substr($context, 0, $offset));
|
||||
$contentLen = \strlen($trimTrailingWhitespace);
|
||||
|
||||
return new self($offset - $contentLen, $contentLen);
|
||||
}
|
||||
|
||||
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 Element::selfClosing('br', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Text
|
||||
*/
|
||||
public function bestPlaintext()
|
||||
{
|
||||
return new Text("\n");
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,8 @@
|
||||
|
||||
namespace Erusev\Parsedown\Components\Inlines;
|
||||
|
||||
use Erusev\Parsedown\AST\Handler;
|
||||
use Erusev\Parsedown\AST\StateRenderable;
|
||||
use Erusev\Parsedown\Components\Inline;
|
||||
use Erusev\Parsedown\Html\Renderables\Container;
|
||||
use Erusev\Parsedown\Html\Renderables\Element;
|
||||
use Erusev\Parsedown\Html\Renderables\Text;
|
||||
use Erusev\Parsedown\Parsedown;
|
||||
use Erusev\Parsedown\Parsing\Excerpt;
|
||||
@@ -45,34 +42,11 @@ final class PlainText implements Inline
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Handler<Container>
|
||||
* @return Text
|
||||
*/
|
||||
public function stateRenderable(Parsedown $_)
|
||||
{
|
||||
return new Handler(
|
||||
/** @return Container */
|
||||
function (State $_) {
|
||||
$Renderables = [];
|
||||
$text = $this->text;
|
||||
|
||||
$text = \preg_replace('/(?<![ \t])[ ]\n/', "$1\n", $text);
|
||||
|
||||
while (\preg_match('/(?:[ ]*+[\\\]|[ ]{2,}+)\n/', $text, $matches, \PREG_OFFSET_CAPTURE)) {
|
||||
$offset = \intval($matches[0][1]);
|
||||
$before = \substr($text, 0, $offset);
|
||||
$after = \substr($text, $offset + \strlen($matches[0][0]));
|
||||
$Renderables[] = new Text($before);
|
||||
$Renderables[] = Element::selfClosing('br', []);
|
||||
$Renderables[] = new Text("\n");
|
||||
|
||||
$text = $after;
|
||||
}
|
||||
|
||||
$Renderables[] = new Text($text);
|
||||
|
||||
return new Container($Renderables);
|
||||
}
|
||||
);
|
||||
return new Text($this->text);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
76
src/Components/Inlines/SoftBreak.php
Normal file
76
src/Components/Inlines/SoftBreak.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Erusev\Parsedown\Components\Inlines;
|
||||
|
||||
use Erusev\Parsedown\AST\StateRenderable;
|
||||
use Erusev\Parsedown\Components\Inline;
|
||||
use Erusev\Parsedown\Html\Renderables\Text;
|
||||
use Erusev\Parsedown\Parsedown;
|
||||
use Erusev\Parsedown\Parsing\Excerpt;
|
||||
use Erusev\Parsedown\State;
|
||||
|
||||
final class SoftBreak implements Inline
|
||||
{
|
||||
use WidthTrait;
|
||||
|
||||
/** @var int */
|
||||
private $position;
|
||||
|
||||
/**
|
||||
* @param int $width
|
||||
* @param int $position
|
||||
*/
|
||||
public function __construct($width, $position)
|
||||
{
|
||||
$this->width = $width;
|
||||
$this->position = $position;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Excerpt $Excerpt
|
||||
* @param State $State
|
||||
* @return static|null
|
||||
*/
|
||||
public static function build(Excerpt $Excerpt, State $State)
|
||||
{
|
||||
$context = $Excerpt->context();
|
||||
$offset = $Excerpt->offset();
|
||||
|
||||
$trimTrailingWhitespaceBefore = \rtrim(\substr($context, 0, $offset));
|
||||
$trimLeadingWhitespaceAfter = \ltrim(\substr($context, $offset + 1));
|
||||
$contentLenBefore = \strlen($trimTrailingWhitespaceBefore);
|
||||
$contentLenAfter = \strlen($trimLeadingWhitespaceAfter);
|
||||
|
||||
$originalLen = \strlen($context);
|
||||
$afterWidth = $originalLen - $offset - $contentLenAfter;
|
||||
|
||||
return new self($offset + $afterWidth - $contentLenBefore, $contentLenBefore);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 Text
|
||||
*/
|
||||
public function stateRenderable(Parsedown $_)
|
||||
{
|
||||
return new Text("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Text
|
||||
*/
|
||||
public function bestPlaintext()
|
||||
{
|
||||
return new Text("\n");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user