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:
parent
4501a094db
commit
4adbd0b8a7
16
src/Components/BacktrackingInline.php
Normal file
16
src/Components/BacktrackingInline.php
Normal 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();
|
||||
}
|
@ -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
|
||||
*/
|
||||
|
@ -11,7 +11,7 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class Code implements Inline
|
||||
{
|
||||
use WidthTrait, DefaultBeginPosition;
|
||||
use WidthTrait;
|
||||
|
||||
/** @var string */
|
||||
private $text;
|
||||
|
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Erusev\Parsedown\Components\Inlines;
|
||||
|
||||
trait DefaultBeginPosition
|
||||
{
|
||||
/** @return int|null */
|
||||
public function modifyStartPositionTo()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class Email implements Inline
|
||||
{
|
||||
use WidthTrait, DefaultBeginPosition;
|
||||
use WidthTrait;
|
||||
|
||||
/** @var string */
|
||||
private $text;
|
||||
|
@ -13,7 +13,7 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class Emphasis implements Inline
|
||||
{
|
||||
use WidthTrait, DefaultBeginPosition;
|
||||
use WidthTrait;
|
||||
|
||||
/** @var string */
|
||||
private $text;
|
||||
|
@ -10,7 +10,7 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class EscapeSequence implements Inline
|
||||
{
|
||||
use WidthTrait, DefaultBeginPosition;
|
||||
use WidthTrait;
|
||||
|
||||
const SPECIALS = '!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~';
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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]+|"[^"]*+"|\'[^\']*+\'))?+';
|
||||
|
||||
|
@ -10,7 +10,7 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class PlainText implements Inline
|
||||
{
|
||||
use WidthTrait, DefaultBeginPosition;
|
||||
use WidthTrait;
|
||||
|
||||
/** @var string */
|
||||
private $text;
|
||||
|
@ -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;
|
||||
|
||||
|
@ -11,7 +11,7 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class SpecialCharacter implements Inline
|
||||
{
|
||||
use WidthTrait, DefaultBeginPosition;
|
||||
use WidthTrait;
|
||||
|
||||
/** @var string */
|
||||
private $charCodeHtml;
|
||||
|
@ -13,7 +13,7 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class Strikethrough implements Inline
|
||||
{
|
||||
use WidthTrait, DefaultBeginPosition;
|
||||
use WidthTrait;
|
||||
|
||||
/** @var string */
|
||||
private $text;
|
||||
|
@ -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;
|
||||
|
||||
|
@ -11,7 +11,7 @@ use Erusev\Parsedown\State;
|
||||
|
||||
final class UrlTag implements Inline
|
||||
{
|
||||
use WidthTrait, DefaultBeginPosition;
|
||||
use WidthTrait;
|
||||
|
||||
/** @var string */
|
||||
private $url;
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user