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

Backtracking capable inlines better expressed by interface

This commit is contained in:
Aidan Woods 2019-02-22 14:56:22 +00:00
parent 4501a094db
commit 4adbd0b8a7
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
18 changed files with 41 additions and 35 deletions

View File

@ -0,0 +1,16 @@
<?php
namespace Erusev\Parsedown\Components;
use Erusev\Parsedown\Parsing\Excerpt;
interface BacktrackingInline extends Inline
{
/**
* 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();
}

View File

@ -22,14 +22,6 @@ interface Inline extends Component
* */
public function width();
/**
* 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 Text
*/

View File

@ -11,7 +11,7 @@ use Erusev\Parsedown\State;
final class Code implements Inline
{
use WidthTrait, DefaultBeginPosition;
use WidthTrait;
/** @var string */
private $text;

View File

@ -1,12 +0,0 @@
<?php
namespace Erusev\Parsedown\Components\Inlines;
trait DefaultBeginPosition
{
/** @return int|null */
public function modifyStartPositionTo()
{
return null;
}
}

View File

@ -11,7 +11,7 @@ use Erusev\Parsedown\State;
final class Email implements Inline
{
use WidthTrait, DefaultBeginPosition;
use WidthTrait;
/** @var string */
private $text;

View File

@ -13,7 +13,7 @@ use Erusev\Parsedown\State;
final class Emphasis implements Inline
{
use WidthTrait, DefaultBeginPosition;
use WidthTrait;
/** @var string */
private $text;

View File

@ -10,7 +10,7 @@ use Erusev\Parsedown\State;
final class EscapeSequence implements Inline
{
use WidthTrait, DefaultBeginPosition;
use WidthTrait;
const SPECIALS = '!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~';

View File

@ -3,13 +3,14 @@
namespace Erusev\Parsedown\Components\Inlines;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\BacktrackingInline;
use Erusev\Parsedown\Components\Inline;
use Erusev\Parsedown\Html\Renderables\Element;
use Erusev\Parsedown\Html\Renderables\Text;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
final class HardBreak implements Inline
final class HardBreak implements BacktrackingInline
{
use WidthTrait;

View File

@ -16,7 +16,7 @@ use Erusev\Parsedown\State;
/** @psalm-type _Metadata=array{href: string, title?: string} */
final class Image implements Inline
{
use WidthTrait, DefaultBeginPosition;
use WidthTrait;
/** @var Link */
private $Link;

View File

@ -18,7 +18,7 @@ use Erusev\Parsedown\State;
/** @psalm-type _Metadata=array{href: string, title?: string} */
final class Link implements Inline
{
use WidthTrait, DefaultBeginPosition;
use WidthTrait;
/** @var string */
private $label;

View File

@ -13,7 +13,7 @@ use Erusev\Parsedown\State;
final class Markup implements Inline
{
use WidthTrait, DefaultBeginPosition;
use WidthTrait;
const HTML_ATT_REGEX = '[a-zA-Z_:][\w:.-]*+(?:\s*+=\s*+(?:[^"\'=<>`\s]+|"[^"]*+"|\'[^\']*+\'))?+';

View File

@ -10,7 +10,7 @@ use Erusev\Parsedown\State;
final class PlainText implements Inline
{
use WidthTrait, DefaultBeginPosition;
use WidthTrait;
/** @var string */
private $text;

View File

@ -4,6 +4,7 @@ namespace Erusev\Parsedown\Components\Inlines;
use Erusev\Parsedown\AST\Handler;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\BacktrackingInline;
use Erusev\Parsedown\Components\Inline;
use Erusev\Parsedown\Configurables\Breaks;
use Erusev\Parsedown\Html\Renderables\Container;
@ -12,7 +13,7 @@ use Erusev\Parsedown\Html\Renderables\Text;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
final class SoftBreak implements Inline
final class SoftBreak implements BacktrackingInline
{
use WidthTrait;

View File

@ -11,7 +11,7 @@ use Erusev\Parsedown\State;
final class SpecialCharacter implements Inline
{
use WidthTrait, DefaultBeginPosition;
use WidthTrait;
/** @var string */
private $charCodeHtml;

View File

@ -13,7 +13,7 @@ use Erusev\Parsedown\State;
final class Strikethrough implements Inline
{
use WidthTrait, DefaultBeginPosition;
use WidthTrait;
/** @var string */
private $text;

View File

@ -3,13 +3,14 @@
namespace Erusev\Parsedown\Components\Inlines;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\BacktrackingInline;
use Erusev\Parsedown\Components\Inline;
use Erusev\Parsedown\Html\Renderables\Element;
use Erusev\Parsedown\Html\Renderables\Text;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
final class Url implements Inline
final class Url implements BacktrackingInline
{
use WidthTrait;

View File

@ -11,7 +11,7 @@ use Erusev\Parsedown\State;
final class UrlTag implements Inline
{
use WidthTrait, DefaultBeginPosition;
use WidthTrait;
/** @var string */
private $url;

View File

@ -4,6 +4,7 @@ namespace Erusev\Parsedown;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\AcquisitioningBlock;
use Erusev\Parsedown\Components\BacktrackingInline;
use Erusev\Parsedown\Components\Block;
use Erusev\Parsedown\Components\Blocks\Paragraph;
use Erusev\Parsedown\Components\ContinuableBlock;
@ -202,7 +203,13 @@ final class Parsedown
}
$markerPosition = $Excerpt->offset();
$startPosition = $Inline->modifyStartPositionTo();
/** @var int|null */
$startPosition = null;
if ($Inline instanceof BacktrackingInline) {
$startPosition = $Inline->modifyStartPositionTo();
}
if (! isset($startPosition)) {
$startPosition = $markerPosition;