1
0
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:
Aidan Woods 2019-01-25 18:52:49 +00:00
parent 714ae50211
commit 1fd2e14b72
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
5 changed files with 222 additions and 95 deletions

View File

@ -27,6 +27,7 @@
<referencedMethod name="Erusev\Parsedown\Parsing\Lines::last" /> <referencedMethod name="Erusev\Parsedown\Parsing\Lines::last" />
<referencedMethod name="Erusev\Parsedown\Configurables\StrictMode::enabled" /> <referencedMethod name="Erusev\Parsedown\Configurables\StrictMode::enabled" />
<referencedMethod name="Erusev\Parsedown\Configurables\SafeMode::enabled" /> <referencedMethod name="Erusev\Parsedown\Configurables\SafeMode::enabled" />
<referencedMethod name="Erusev\Parsedown\Html\Renderables\Container::__construct" />
</errorLevel> </errorLevel>
</PossiblyUnusedMethod> </PossiblyUnusedMethod>
</issueHandlers> </issueHandlers>

View 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");
}
}

View File

@ -2,11 +2,8 @@
namespace Erusev\Parsedown\Components\Inlines; namespace Erusev\Parsedown\Components\Inlines;
use Erusev\Parsedown\AST\Handler;
use Erusev\Parsedown\AST\StateRenderable; use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\Inline; 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\Html\Renderables\Text;
use Erusev\Parsedown\Parsedown; use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\Parsing\Excerpt; use Erusev\Parsedown\Parsing\Excerpt;
@ -45,34 +42,11 @@ final class PlainText implements Inline
} }
/** /**
* @return Handler<Container> * @return Text
*/ */
public function stateRenderable(Parsedown $_) public function stateRenderable(Parsedown $_)
{ {
return new Handler( return new Text($this->text);
/** @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);
}
);
} }
/** /**

View 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");
}
}

View File

@ -22,10 +22,12 @@ use Erusev\Parsedown\Components\Inlines\Code;
use Erusev\Parsedown\Components\Inlines\Email; use Erusev\Parsedown\Components\Inlines\Email;
use Erusev\Parsedown\Components\Inlines\Emphasis; use Erusev\Parsedown\Components\Inlines\Emphasis;
use Erusev\Parsedown\Components\Inlines\EscapeSequence; use Erusev\Parsedown\Components\Inlines\EscapeSequence;
use Erusev\Parsedown\Components\Inlines\HardBreak;
use Erusev\Parsedown\Components\Inlines\Image; use Erusev\Parsedown\Components\Inlines\Image;
use Erusev\Parsedown\Components\Inlines\Link; use Erusev\Parsedown\Components\Inlines\Link;
use Erusev\Parsedown\Components\Inlines\Markup as InlineMarkup; use Erusev\Parsedown\Components\Inlines\Markup as InlineMarkup;
use Erusev\Parsedown\Components\Inlines\PlainText; use Erusev\Parsedown\Components\Inlines\PlainText;
use Erusev\Parsedown\Components\Inlines\SoftBreak;
use Erusev\Parsedown\Components\Inlines\SpecialCharacter; use Erusev\Parsedown\Components\Inlines\SpecialCharacter;
use Erusev\Parsedown\Components\Inlines\Strikethrough; use Erusev\Parsedown\Components\Inlines\Strikethrough;
use Erusev\Parsedown\Components\Inlines\Url; use Erusev\Parsedown\Components\Inlines\Url;
@ -41,48 +43,13 @@ use Erusev\Parsedown\Parsing\Lines;
final class Parsedown final class Parsedown
{ {
# ~
const version = '2.0.0-dev'; const version = '2.0.0-dev';
# ~
/** @var State */ /** @var State */
private $State; private $State;
public function __construct(State $State = null)
{
$this->State = $State ?: new State;
}
/**
* @param string $text
* @return string
*/
public function text($text)
{
$InitialState = $this->State;
$StateRenderables = $this->lines(Lines::fromTextLines($text, 0));
$Renderables = $this->State->applyTo($StateRenderables);
$this->State = $InitialState;
$html = self::render($Renderables);
# trim line breaks
$html = \trim($html, "\n");
return $html;
}
#
# Lines
#
/** @var array<array-key, class-string<Block>[]> */ /** @var array<array-key, class-string<Block>[]> */
protected $BlockTypes = [ private $BlockTypes = [
'#' => [Header::class], '#' => [Header::class],
'*' => [Rule::class, TList::class], '*' => [Rule::class, TList::class],
'+' => [TList::class], '+' => [TList::class],
@ -108,16 +75,57 @@ final class Parsedown
'~' => [FencedCode::class], '~' => [FencedCode::class],
]; ];
# ~
/** @var class-string<Block>[] */ /** @var class-string<Block>[] */
protected $unmarkedBlockTypes = [ private $unmarkedBlockTypes = [
IndentedCode::class, IndentedCode::class,
]; ];
# /** @var array<array-key, class-string<Inline>[]> */
# Blocks private $InlineTypes = [
# '!' => [Image::class],
'*' => [Emphasis::class],
'_' => [Emphasis::class],
'&' => [SpecialCharacter::class],
'[' => [Link::class],
':' => [Url::class],
'<' => [UrlTag::class, Email::class, InlineMarkup::class],
'`' => [Code::class],
'~' => [Strikethrough::class],
'\\' => [EscapeSequence::class],
"\n" => [HardBreak::class, SoftBreak::class],
];
/** @var string */
private $inlineMarkers;
public function __construct(State $State = null)
{
$this->State = $State ?: new State;
$this->inlineMarkers = \implode('', \array_keys($this->InlineTypes));
}
/**
* @param string $text
* @return string
*/
public function text($text)
{
$InitialState = $this->State;
$StateRenderables = $this->lines(Lines::fromTextLines($text, 0));
$Renderables = $this->State->applyTo($StateRenderables);
$this->State = $InitialState;
$html = self::render($Renderables);
# trim line breaks
$html = \trim($html, "\n");
return $html;
}
/** /**
* @return StateRenderable[] * @return StateRenderable[]
@ -225,29 +233,6 @@ final class Parsedown
return $Blocks; return $Blocks;
} }
#
# Inline Elements
#
/** @var array<array-key, class-string<Inline>[]> */
protected $InlineTypes = [
'!' => [Image::class],
'&' => [SpecialCharacter::class],
'*' => [Emphasis::class],
':' => [Url::class],
'<' => [UrlTag::class, Email::class, InlineMarkup::class],
'[' => [Link::class],
'_' => [Emphasis::class],
'`' => [Code::class],
'~' => [Strikethrough::class],
'\\' => [EscapeSequence::class],
];
# ~
/** @var string */
protected $inlineMarkerList = '!*_&[:<`~\\';
# #
# ~ # ~
# #
@ -280,9 +265,9 @@ final class Parsedown
# $excerpt is based on the first occurrence of a marker # $excerpt is based on the first occurrence of a marker
for ( for (
$Excerpt = (new Excerpt($text, 0))->pushingOffsetTo($this->inlineMarkerList); $Excerpt = (new Excerpt($text, 0))->pushingOffsetTo($this->inlineMarkers);
$Excerpt->text() !== ''; $Excerpt->text() !== '';
$Excerpt = $Excerpt->pushingOffsetTo($this->inlineMarkerList) $Excerpt = $Excerpt->pushingOffsetTo($this->inlineMarkers)
) { ) {
$marker = \substr($Excerpt->text(), 0, 1); $marker = \substr($Excerpt->text(), 0, 1);