Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions packages/guides-cli/src/DevServer/RerenderListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,9 @@ public function __invoke(FileModifiedEvent $event): void
);
assert($document instanceof DocumentNode);

$documents = $this->documents;
$documents[$file] = $document;

/** @var array<string, DocumentNode> $documents */
$documents = $this->commandBus->handle(new CompileDocumentsCommand($documents, new CompilerContext($this->projectNode)));
$this->documents = $documents;
$documents = $this->commandBus->handle(new CompileDocumentsCommand([$file => $document], new CompilerContext($this->projectNode)));
$this->documents[$file] = $documents[$file];
$destinationFileSystem = FlySystemAdapter::createForPath($this->settings->getOutput());

$documentIterator = DocumentListIterator::create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use function explode;
use function implode;
use function is_string;
use function method_exists;
use function strval;
use function trim;

Expand Down Expand Up @@ -78,10 +79,18 @@ public function processNode(
return new GenericNode('csv-table');
}

$csv = Reader::createFromStream($csvStream);
if (method_exists(Reader::class, 'from')) {
$csv = Reader::from($csvStream);
} else {
$csv = Reader::createFromStream($csvStream);
}
} else {
$lines = $blockContext->getDocumentIterator()->toArray();
$csv = Reader::createFromString(implode("\n", $lines));
if (method_exists(Reader::class, 'fromString')) {
$csv = Reader::fromString(implode("\n", $lines));
} else {
$csv = Reader::createFromString(implode("\n", $lines));
}
}

if ($directive->getOption('header-rows')->getValue() !== null) {
Expand All @@ -90,7 +99,12 @@ public function processNode(

$header = null;
if ($directive->hasOption('header')) {
$headerCsv = Reader::createFromString($directive->getOption('header')->toString());
if (method_exists(Reader::class, 'fromString')) {
$headerCsv = Reader::fromString($directive->getOption('header')->toString());
} else {
$headerCsv = Reader::createFromString($directive->getOption('header')->toString());
}

$header = new TableRow();
foreach ($headerCsv->first() as $column) {
$columnNode = new TableColumn($column, 1, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace phpDocumentor\Guides\RestructuredText\Parser;

use function array_pop;
use function array_walk;
use function count;
use function implode;
use function ltrim;
Expand Down Expand Up @@ -103,21 +102,29 @@ public function clear(): void

public function trimLines(): void
{
array_walk($this->lines, static function (&$value): void {
$value = trim($value);
});
foreach ($this->lines as $i => $line) {
$this->lines[$i] = trim($line);
}
}

private function unIndent(): void
{
if ($this->unindentStrategy === UnindentStrategy::NONE) {
return;
}

$indentation = $this->detectIndentation();
array_walk($this->lines, static function (&$value) use ($indentation): void {
if (strlen($value) < $indentation) {
return;
if ($indentation === 0) {
return;
}

foreach ($this->lines as $i => $line) {
if (strlen($line) < $indentation) {
continue;
}

$value = substr($value, $indentation);
});
$this->lines[$i] = substr($line, $indentation);
}
}

private function detectIndentation(): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@

use function array_column;
use function array_flip;
use function ctype_alnum;
use function ctype_space;
use function parse_url;
use function preg_match;
use function str_ends_with;
use function str_replace;
use function strlen;
use function substr;

use const PHP_URL_SCHEME;

Expand Down Expand Up @@ -111,6 +117,7 @@ protected function getType(string &$value)
{
$type = match ($value) {
'`' => self::BACKTICK,
'``' => self::DOUBLE_BACKTICK,
'**' => self::STRONG_DELIMITER,
'*' => self::EMPHASIS_DELIMITER,
'|' => self::VARIABLE_DELIMITER,
Expand All @@ -130,10 +137,22 @@ protected function getType(string &$value)
}

// $value is already a tokenized part. Therefore, we have to match against the complete String here.
if (preg_match('/^\\\\[\s\S]$/i', $value)) {
if (str_ends_with($value, '__') && ctype_alnum(str_replace('-', '', substr($value, 0, -2)))) {
return self::ANONYMOUSE_REFERENCE;
}

if (str_ends_with($value, '_') && ctype_alnum(str_replace('-', '', substr($value, 0, -1)))) {
return self::NAMED_REFERENCE;
}

if (strlen($value) === 2 && $value[0] === '\\') {
return self::ESCAPED_SIGN;
}

if (strlen($value) === 1 && ctype_space($value)) {
return self::WHITESPACE;
}

if (preg_match('/^``.+``(?!`)$/i', $value)) {
return self::LITERAL;
}
Expand All @@ -146,18 +165,6 @@ protected function getType(string &$value)
return self::EMAIL;
}

if (preg_match('/^[a-z0-9-]+_{2}$/i', $value)) {
return self::ANONYMOUSE_REFERENCE;
}

if (preg_match('/^[a-z0-9-]+_{1}$/i', $value)) {
return self::NAMED_REFERENCE;
}

if (preg_match('/^\s$/i', $value)) {
return self::WHITESPACE;
}

return self::WORD;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
use Exception;
use phpDocumentor\Guides\Nodes\Inline\PlainTextInlineNode;
use phpDocumentor\Guides\Nodes\InlineCompoundNode;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\CachableInlineRule;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\InlineRule;

use function array_filter;
use function array_key_exists;
use function usort;

/** @internal */
Expand All @@ -26,11 +29,21 @@ class InlineParser
/** @var InlineRule[] */
private array $rules;

/** @var array<InlineLexer::*, CachableInlineRule> */
private array $cache = [];

/** @param iterable<InlineRule> $inlineRules */
public function __construct(iterable $inlineRules)
{
$this->rules = [...$inlineRules];
$this->rules = array_filter([...$inlineRules], static fn ($rule) => $rule instanceof CachableInlineRule === false);
usort($this->rules, static fn (InlineRule $a, InlineRule $b): int => $a->getPriority() > $b->getPriority() ? -1 : 1);
foreach ($inlineRules as $rule) {
if (!($rule instanceof CachableInlineRule)) {
continue;
}

$this->cache[$rule->getToken()] = $rule;
}
}

public function parse(string $content, BlockContext $blockContext): InlineCompoundNode
Expand All @@ -44,7 +57,9 @@ public function parse(string $content, BlockContext $blockContext): InlineCompou
while ($lexer->token !== null) {
foreach ($this->rules as $inlineRule) {
$node = null;
if ($inlineRule->applies($lexer)) {
if (array_key_exists($lexer->token->type ?? -1, $this->cache)) {
$node = $this->cache[$lexer->token->type]->apply($blockContext, $lexer);
} elseif ($inlineRule->applies($lexer)) {
$node = $inlineRule->apply($blockContext, $lexer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@
*
* @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#anonymous-hyperlinks
*/
final class AnonymousReferenceRule extends ReferenceRule
final class AnonymousReferenceRule extends ReferenceRule implements CachableInlineRule
{
public function getToken(): int
{
return InlineLexer::ANONYMOUSE_REFERENCE;
}

public function applies(InlineLexer $lexer): bool
{
return $lexer->token?->type === InlineLexer::ANONYMOUSE_REFERENCE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

interface CachableInlineRule extends InlineRule
{
/** @return InlineLexer::* */
public function getToken(): int;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
/**
* Rule for literals such as ``something``
*/
final class LiteralRule extends AbstractInlineRule
final class LiteralRule extends AbstractInlineRule implements CachableInlineRule
{
public function getToken(): int
{
return InlineLexer::LITERAL;
}

public function applies(InlineLexer $lexer): bool
{
return $lexer->token?->type === InlineLexer::LITERAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@
*
* @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#hyperlink-references
*/
final class NamedReferenceRule extends ReferenceRule
final class NamedReferenceRule extends ReferenceRule implements CachableInlineRule
{
public function getToken(): int
{
return InlineLexer::NAMED_REFERENCE;
}

public function applies(InlineLexer $lexer): bool
{
return $lexer->token?->type === InlineLexer::NAMED_REFERENCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
/**
* Rule to parse for non-breaking spaces: a~b
*/
final class NbspRule extends ReferenceRule
final class NbspRule extends AbstractInlineRule implements CachableInlineRule
{
public function getToken(): int
{
return InlineLexer::NBSP;
}

public function applies(InlineLexer $lexer): bool
{
return $lexer->token?->type === InlineLexer::NBSP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
*
* @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#standalone-hyperlinks
*/
final class StandaloneEmailRule extends ReferenceRule
final class StandaloneEmailRule extends ReferenceRule implements CachableInlineRule
{
public function getToken(): int
{
return InlineLexer::EMAIL;
}

public function applies(InlineLexer $lexer): bool
{
return $lexer->token?->type === InlineLexer::EMAIL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
*
* @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#standalone-hyperlinks
*/
final class StandaloneHyperlinkRule extends ReferenceRule
final class StandaloneHyperlinkRule extends ReferenceRule implements CachableInlineRule
{
public function getToken(): int
{
return InlineLexer::HYPERLINK;
}

public function applies(InlineLexer $lexer): bool
{
return $lexer->token?->type === InlineLexer::HYPERLINK;
Expand Down
39 changes: 39 additions & 0 deletions packages/guides-restructured-text/tests/benchmarks/BufferBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Guides\RestructuredText;

use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\Revs;
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;

final class BufferBench
{
private Buffer $buffer;

public function __construct()
{
$this->buffer = new Buffer();
$this->buffer->push(' This is a line with leading spaces. ');
$this->buffer->push(' This is another line.');
$this->buffer->push(' Yet another line with spaces. ');
$this->buffer->push(' Final line.');
}

#[Revs([1000, 10_000])]
#[Iterations(5)]
public function benchGetLines(): void
{
$this->buffer->getLines();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ public function benchInlineLexer(): void
$lexer = new InlineLexer();
$lexer->setInput('This is a `link`_ to a section.');
}

#[Revs([1000, 10_000])]
#[Iterations(5)]
public function benchFullParagraph(): void
{
$lexer = new InlineLexer();
$lexer->setInput('
With :issue:`103894` the new data processor :ref:`PageContentFetchingProcessor <feature-103894-1716544976>`
has been introduced, to allow fetching page content based on the current page
layout, taking the configured :php:`SlideMode` into account.');
}
}
Loading
Loading