Commaaa2
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -14,30 +16,25 @@
|
||||
|
||||
namespace League\CommonMark\Reference;
|
||||
|
||||
use League\CommonMark\Normalizer\TextNormalizer;
|
||||
|
||||
/**
|
||||
* @psalm-immutable
|
||||
*/
|
||||
final class Reference implements ReferenceInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $label;
|
||||
/** @psalm-readonly */
|
||||
private string $label;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $destination;
|
||||
/** @psalm-readonly */
|
||||
private string $destination;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $title;
|
||||
/** @psalm-readonly */
|
||||
private string $title;
|
||||
|
||||
public function __construct(string $label, string $destination, string $title)
|
||||
{
|
||||
$this->label = $label;
|
||||
$this->label = $label;
|
||||
$this->destination = $destination;
|
||||
$this->title = $title;
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
public function getLabel(): string
|
||||
@@ -54,23 +51,4 @@ final class Reference implements ReferenceInterface
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize reference label
|
||||
*
|
||||
* This enables case-insensitive label matching
|
||||
*
|
||||
* @param string $string
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @deprecated Use TextNormalizer::normalize() instead
|
||||
* @group legacy
|
||||
*/
|
||||
public static function normalizeReference(string $string): string
|
||||
{
|
||||
@trigger_error(sprintf('%s::normlizeReference() is deprecated; use %s::normalize() instead', self::class, TextNormalizer::class), E_USER_DEPRECATED);
|
||||
|
||||
return (new TextNormalizer())->normalize($string);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -21,23 +23,26 @@ use League\CommonMark\Normalizer\TextNormalizer;
|
||||
*/
|
||||
final class ReferenceMap implements ReferenceMapInterface
|
||||
{
|
||||
/** @var TextNormalizer */
|
||||
private $normalizer;
|
||||
/** @psalm-readonly */
|
||||
private TextNormalizer $normalizer;
|
||||
|
||||
/**
|
||||
* @var ReferenceInterface[]
|
||||
* @var array<string, ReferenceInterface>
|
||||
*
|
||||
* @psalm-readonly-allow-private-mutation
|
||||
*/
|
||||
private $references = [];
|
||||
private array $references = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->normalizer = new TextNormalizer();
|
||||
}
|
||||
|
||||
public function addReference(ReferenceInterface $reference): void
|
||||
public function add(ReferenceInterface $reference): void
|
||||
{
|
||||
// Normalize the key
|
||||
$key = $this->normalizer->normalize($reference->getLabel());
|
||||
|
||||
// Store the reference
|
||||
$this->references[$key] = $reference;
|
||||
}
|
||||
|
||||
@@ -48,19 +53,25 @@ final class ReferenceMap implements ReferenceMapInterface
|
||||
return isset($this->references[$label]);
|
||||
}
|
||||
|
||||
public function getReference(string $label): ?ReferenceInterface
|
||||
public function get(string $label): ?ReferenceInterface
|
||||
{
|
||||
$label = $this->normalizer->normalize($label);
|
||||
|
||||
if (!isset($this->references[$label])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->references[$label];
|
||||
return $this->references[$label] ?? null;
|
||||
}
|
||||
|
||||
public function listReferences(): iterable
|
||||
/**
|
||||
* @return \Traversable<string, ReferenceInterface>
|
||||
*/
|
||||
public function getIterator(): \Traversable
|
||||
{
|
||||
return \array_values($this->references);
|
||||
foreach ($this->references as $normalizedLabel => $reference) {
|
||||
yield $normalizedLabel => $reference;
|
||||
}
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return \count($this->references);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -16,34 +18,14 @@ namespace League\CommonMark\Reference;
|
||||
|
||||
/**
|
||||
* A collection of references
|
||||
*
|
||||
* @phpstan-extends \IteratorAggregate<ReferenceInterface>
|
||||
*/
|
||||
interface ReferenceMapInterface
|
||||
interface ReferenceMapInterface extends \IteratorAggregate, \Countable
|
||||
{
|
||||
/**
|
||||
* @param ReferenceInterface $reference
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addReference(ReferenceInterface $reference): void;
|
||||
public function add(ReferenceInterface $reference): void;
|
||||
|
||||
/**
|
||||
* @param string $label
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function contains(string $label): bool;
|
||||
|
||||
/**
|
||||
* @param string $label
|
||||
*
|
||||
* @return ReferenceInterface|null
|
||||
*/
|
||||
public function getReference(string $label): ?ReferenceInterface;
|
||||
|
||||
/**
|
||||
* Lists all registered references.
|
||||
*
|
||||
* @return ReferenceInterface[]
|
||||
*/
|
||||
public function listReferences(): iterable;
|
||||
public function get(string $label): ?ReferenceInterface;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -14,110 +16,309 @@
|
||||
|
||||
namespace League\CommonMark\Reference;
|
||||
|
||||
use League\CommonMark\Cursor;
|
||||
use League\CommonMark\Parser\Cursor;
|
||||
use League\CommonMark\Util\LinkParserHelper;
|
||||
|
||||
final class ReferenceParser
|
||||
{
|
||||
/**
|
||||
* @var ReferenceMapInterface
|
||||
*/
|
||||
private $referenceMap;
|
||||
// Looking for the start of a definition, i.e. `[`
|
||||
private const START_DEFINITION = 0;
|
||||
// Looking for and parsing the label, i.e. `[foo]` within `[foo]`
|
||||
private const LABEL = 1;
|
||||
// Parsing the destination, i.e. `/url` in `[foo]: /url`
|
||||
private const DESTINATION = 2;
|
||||
// Looking for the start of a title, i.e. the first `"` in `[foo]: /url "title"`
|
||||
private const START_TITLE = 3;
|
||||
// Parsing the content of the title, i.e. `title` in `[foo]: /url "title"`
|
||||
private const TITLE = 4;
|
||||
// End state, no matter what kind of lines we add, they won't be references
|
||||
private const PARAGRAPH = 5;
|
||||
|
||||
public function __construct(ReferenceMapInterface $referenceMap)
|
||||
/** @psalm-readonly-allow-private-mutation */
|
||||
private string $paragraph = '';
|
||||
|
||||
/**
|
||||
* @var array<int, ReferenceInterface>
|
||||
*
|
||||
* @psalm-readonly-allow-private-mutation
|
||||
*/
|
||||
private array $references = [];
|
||||
|
||||
/** @psalm-readonly-allow-private-mutation */
|
||||
private int $state = self::START_DEFINITION;
|
||||
|
||||
/** @psalm-readonly-allow-private-mutation */
|
||||
private ?string $label = null;
|
||||
|
||||
/** @psalm-readonly-allow-private-mutation */
|
||||
private ?string $destination = null;
|
||||
|
||||
/**
|
||||
* @var string string
|
||||
*
|
||||
* @psalm-readonly-allow-private-mutation
|
||||
*/
|
||||
private string $title = '';
|
||||
|
||||
/** @psalm-readonly-allow-private-mutation */
|
||||
private ?string $titleDelimiter = null;
|
||||
|
||||
/** @psalm-readonly-allow-private-mutation */
|
||||
private bool $referenceValid = false;
|
||||
|
||||
public function getParagraphContent(): string
|
||||
{
|
||||
$this->referenceMap = $referenceMap;
|
||||
return $this->paragraph;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to parse a link reference, modifying the refmap.
|
||||
*
|
||||
* @param Cursor $cursor
|
||||
*
|
||||
* @return bool
|
||||
* @return ReferenceInterface[]
|
||||
*/
|
||||
public function parse(Cursor $cursor)
|
||||
public function getReferences(): iterable
|
||||
{
|
||||
if ($cursor->isAtEnd()) {
|
||||
return false;
|
||||
$this->finishReference();
|
||||
|
||||
return $this->references;
|
||||
}
|
||||
|
||||
public function hasReferences(): bool
|
||||
{
|
||||
return $this->references !== [];
|
||||
}
|
||||
|
||||
public function parse(string $line): void
|
||||
{
|
||||
if ($this->paragraph !== '') {
|
||||
$this->paragraph .= "\n";
|
||||
}
|
||||
|
||||
$initialState = $cursor->saveState();
|
||||
$this->paragraph .= $line;
|
||||
|
||||
$matchChars = LinkParserHelper::parseLinkLabel($cursor);
|
||||
if ($matchChars === 0) {
|
||||
$cursor->restoreState($initialState);
|
||||
$cursor = new Cursor($line);
|
||||
while (! $cursor->isAtEnd()) {
|
||||
$result = false;
|
||||
switch ($this->state) {
|
||||
case self::PARAGRAPH:
|
||||
// We're in a paragraph now. Link reference definitions can only appear at the beginning, so once
|
||||
// we're in a paragraph, there's no going back.
|
||||
return;
|
||||
case self::START_DEFINITION:
|
||||
$result = $this->parseStartDefinition($cursor);
|
||||
break;
|
||||
case self::LABEL:
|
||||
$result = $this->parseLabel($cursor);
|
||||
break;
|
||||
case self::DESTINATION:
|
||||
$result = $this->parseDestination($cursor);
|
||||
break;
|
||||
case self::START_TITLE:
|
||||
$result = $this->parseStartTitle($cursor);
|
||||
break;
|
||||
case self::TITLE:
|
||||
$result = $this->parseTitle($cursor);
|
||||
break;
|
||||
default:
|
||||
// this should never happen
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
if (! $result) {
|
||||
$this->state = self::PARAGRAPH;
|
||||
|
||||
// We need to trim the opening and closing brackets from the previously-matched text
|
||||
$label = \substr($cursor->getPreviousText(), 1, -1);
|
||||
|
||||
if (\preg_match('/[^\s]/', $label) === 0) {
|
||||
$cursor->restoreState($initialState);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($cursor->getCharacter() !== ':') {
|
||||
$cursor->restoreState($initialState);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Advance past the colon
|
||||
$cursor->advanceBy(1);
|
||||
|
||||
// Link URL
|
||||
$cursor->advanceToNextNonSpaceOrNewline();
|
||||
|
||||
$destination = LinkParserHelper::parseLinkDestination($cursor);
|
||||
if ($destination === null) {
|
||||
$cursor->restoreState($initialState);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$previousState = $cursor->saveState();
|
||||
|
||||
if ($cursor->advanceToNextNonSpaceOrNewline() > 0) {
|
||||
$title = LinkParserHelper::parseLinkTitle($cursor);
|
||||
}
|
||||
|
||||
if (!isset($title)) {
|
||||
$title = '';
|
||||
$cursor->restoreState($previousState);
|
||||
}
|
||||
|
||||
// Make sure we're at line end:
|
||||
$atLineEnd = true;
|
||||
if ($cursor->match('/^ *(?:\n|$)/') === null) {
|
||||
if ($title === '') {
|
||||
$atLineEnd = false;
|
||||
} else {
|
||||
// The potential title we found is not at the line end,
|
||||
// but it could still be a legal link reference if we
|
||||
// discard the title
|
||||
$title = '';
|
||||
// rewind before spaces
|
||||
$cursor->restoreState($previousState);
|
||||
// and instead check if the link URL is at the line end
|
||||
$atLineEnd = $cursor->match('/^ *(?:\n|$)/') !== null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$atLineEnd) {
|
||||
$cursor->restoreState($initialState);
|
||||
|
||||
private function parseStartDefinition(Cursor $cursor): bool
|
||||
{
|
||||
$cursor->advanceToNextNonSpaceOrTab();
|
||||
if ($cursor->isAtEnd() || $cursor->getCurrentCharacter() !== '[') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->referenceMap->contains($label)) {
|
||||
$reference = new Reference($label, $destination, $title);
|
||||
$this->referenceMap->addReference($reference);
|
||||
$this->state = self::LABEL;
|
||||
$this->label = '';
|
||||
|
||||
$cursor->advance();
|
||||
if ($cursor->isAtEnd()) {
|
||||
$this->label .= "\n";
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function parseLabel(Cursor $cursor): bool
|
||||
{
|
||||
$cursor->advanceToNextNonSpaceOrTab();
|
||||
|
||||
$partialLabel = LinkParserHelper::parsePartialLinkLabel($cursor);
|
||||
if ($partialLabel === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
\assert($this->label !== null);
|
||||
$this->label .= $partialLabel;
|
||||
|
||||
if ($cursor->isAtEnd()) {
|
||||
// label might continue on next line
|
||||
$this->label .= "\n";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($cursor->getCurrentCharacter() !== ']') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cursor->advance();
|
||||
|
||||
// end of label
|
||||
if ($cursor->getCurrentCharacter() !== ':') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cursor->advance();
|
||||
|
||||
// spec: A link label can have at most 999 characters inside the square brackets
|
||||
if (\mb_strlen($this->label, 'UTF-8') > 999) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// spec: A link label must contain at least one non-whitespace character
|
||||
if (\trim($this->label) === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cursor->advanceToNextNonSpaceOrTab();
|
||||
|
||||
$this->state = self::DESTINATION;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function parseDestination(Cursor $cursor): bool
|
||||
{
|
||||
$cursor->advanceToNextNonSpaceOrTab();
|
||||
|
||||
$destination = LinkParserHelper::parseLinkDestination($cursor);
|
||||
if ($destination === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->destination = $destination;
|
||||
|
||||
$advanced = $cursor->advanceToNextNonSpaceOrTab();
|
||||
if ($cursor->isAtEnd()) {
|
||||
// Destination was at end of line, so this is a valid reference for sure (and maybe a title).
|
||||
// If not at end of line, wait for title to be valid first.
|
||||
$this->referenceValid = true;
|
||||
$this->paragraph = '';
|
||||
} elseif ($advanced === 0) {
|
||||
// spec: The title must be separated from the link destination by whitespace
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->state = self::START_TITLE;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function parseStartTitle(Cursor $cursor): bool
|
||||
{
|
||||
$cursor->advanceToNextNonSpaceOrTab();
|
||||
if ($cursor->isAtEnd()) {
|
||||
$this->state = self::START_DEFINITION;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->titleDelimiter = null;
|
||||
switch ($c = $cursor->getCurrentCharacter()) {
|
||||
case '"':
|
||||
case "'":
|
||||
$this->titleDelimiter = $c;
|
||||
break;
|
||||
case '(':
|
||||
$this->titleDelimiter = ')';
|
||||
break;
|
||||
default:
|
||||
// no title delimter found
|
||||
break;
|
||||
}
|
||||
|
||||
if ($this->titleDelimiter !== null) {
|
||||
$this->state = self::TITLE;
|
||||
$cursor->advance();
|
||||
if ($cursor->isAtEnd()) {
|
||||
$this->title .= "\n";
|
||||
}
|
||||
} else {
|
||||
$this->finishReference();
|
||||
// There might be another reference instead, try that for the same character.
|
||||
$this->state = self::START_DEFINITION;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function parseTitle(Cursor $cursor): bool
|
||||
{
|
||||
\assert($this->titleDelimiter !== null);
|
||||
$title = LinkParserHelper::parsePartialLinkTitle($cursor, $this->titleDelimiter);
|
||||
|
||||
if ($title === null) {
|
||||
// Invalid title, stop
|
||||
return false;
|
||||
}
|
||||
|
||||
// Did we find the end delimiter?
|
||||
$endDelimiterFound = false;
|
||||
if (\substr($title, -1) === $this->titleDelimiter) {
|
||||
$endDelimiterFound = true;
|
||||
// Chop it off
|
||||
$title = \substr($title, 0, -1);
|
||||
}
|
||||
|
||||
$this->title .= $title;
|
||||
|
||||
if (! $endDelimiterFound && $cursor->isAtEnd()) {
|
||||
// Title still going, continue on next line
|
||||
$this->title .= "\n";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// We either hit the end delimiter or some extra whitespace
|
||||
$cursor->advanceToNextNonSpaceOrTab();
|
||||
if (! $cursor->isAtEnd()) {
|
||||
// spec: No further non-whitespace characters may occur on the line.
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->referenceValid = true;
|
||||
$this->finishReference();
|
||||
$this->paragraph = '';
|
||||
|
||||
// See if there's another definition
|
||||
$this->state = self::START_DEFINITION;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function finishReference(): void
|
||||
{
|
||||
if (! $this->referenceValid) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @psalm-suppress PossiblyNullArgument -- these can't possibly be null if we're in this state */
|
||||
$this->references[] = new Reference($this->label, $this->destination, $this->title);
|
||||
|
||||
$this->label = null;
|
||||
$this->referenceValid = false;
|
||||
$this->destination = null;
|
||||
$this->title = '';
|
||||
$this->titleDelimiter = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user