-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #549 from thephpleague/revise-inline-parsing
Revise inline parsing
- Loading branch information
Showing
40 changed files
with
721 additions
and
628 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace League\CommonMark\Delimiter; | ||
|
||
use League\CommonMark\Delimiter\Processor\DelimiterProcessorCollection; | ||
use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface; | ||
use League\CommonMark\Node\Inline\Text; | ||
use League\CommonMark\Parser\Inline\InlineParserInterface; | ||
use League\CommonMark\Parser\Inline\InlineParserMatch; | ||
use League\CommonMark\Parser\InlineParserContext; | ||
use League\CommonMark\Util\RegexHelper; | ||
|
||
/** | ||
* Delimiter parsing is implemented as an Inline Parser with the lowest-possible priority | ||
* | ||
* @internal | ||
*/ | ||
final class DelimiterParser implements InlineParserInterface | ||
{ | ||
/** @var DelimiterProcessorCollection */ | ||
private $collection; | ||
|
||
public function __construct(DelimiterProcessorCollection $collection) | ||
{ | ||
$this->collection = $collection; | ||
} | ||
|
||
public function getMatchDefinition(): InlineParserMatch | ||
{ | ||
return InlineParserMatch::oneOf(...$this->collection->getDelimiterCharacters()); | ||
} | ||
|
||
public function parse(string $match, InlineParserContext $inlineContext): bool | ||
{ | ||
$character = $match; | ||
$numDelims = 0; | ||
$cursor = $inlineContext->getCursor(); | ||
$processor = $this->collection->getDelimiterProcessor($character); | ||
|
||
if ($processor === null) { | ||
throw new \LogicException('Delimiter processor should never be null here'); | ||
} | ||
|
||
$charBefore = $cursor->peek(-1); | ||
if ($charBefore === null) { | ||
$charBefore = "\n"; | ||
} | ||
|
||
while ($cursor->peek($numDelims) === $character) { | ||
++$numDelims; | ||
} | ||
|
||
if ($numDelims < $processor->getMinLength()) { | ||
return false; | ||
} | ||
|
||
$cursor->advanceBy($numDelims); | ||
|
||
$charAfter = $cursor->getCharacter(); | ||
if ($charAfter === null) { | ||
$charAfter = "\n"; | ||
} | ||
|
||
[$canOpen, $canClose] = self::determineCanOpenOrClose($charBefore, $charAfter, $character, $processor); | ||
|
||
$node = new Text(\str_repeat($character, $numDelims), [ | ||
'delim' => true, | ||
]); | ||
$inlineContext->getContainer()->appendChild($node); | ||
|
||
// Add entry to stack to this opener | ||
if ($canOpen || $canClose) { | ||
$delimiter = new Delimiter($character, $numDelims, $node, $canOpen, $canClose); | ||
$inlineContext->getDelimiterStack()->push($delimiter); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @return bool[] | ||
*/ | ||
private static function determineCanOpenOrClose(string $charBefore, string $charAfter, string $character, DelimiterProcessorInterface $delimiterProcessor): array | ||
{ | ||
$afterIsWhitespace = \preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charAfter); | ||
$afterIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter); | ||
$beforeIsWhitespace = \preg_match(RegexHelper::REGEX_UNICODE_WHITESPACE_CHAR, $charBefore); | ||
$beforeIsPunctuation = \preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore); | ||
|
||
$leftFlanking = ! $afterIsWhitespace && (! $afterIsPunctuation || $beforeIsWhitespace || $beforeIsPunctuation); | ||
$rightFlanking = ! $beforeIsWhitespace && (! $beforeIsPunctuation || $afterIsWhitespace || $afterIsPunctuation); | ||
|
||
if ($character === '_') { | ||
$canOpen = $leftFlanking && (! $rightFlanking || $beforeIsPunctuation); | ||
$canClose = $rightFlanking && (! $leftFlanking || $afterIsPunctuation); | ||
} else { | ||
$canOpen = $leftFlanking && $character === $delimiterProcessor->getOpeningCharacter(); | ||
$canClose = $rightFlanking && $character === $delimiterProcessor->getClosingCharacter(); | ||
} | ||
|
||
return [$canOpen, $canClose]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.