Skip to content

Commit 4708f1d

Browse files
committed
Optimize lexer and re-render compiling
1 parent a860c9b commit 4708f1d

File tree

5 files changed

+51
-22
lines changed

5 files changed

+51
-22
lines changed

packages/guides-cli/src/DevServer/RerenderListener.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,9 @@ public function __invoke(FileModifiedEvent $event): void
7070
);
7171
assert($document instanceof DocumentNode);
7272

73-
$documents = $this->documents;
74-
$documents[$file] = $document;
75-
7673
/** @var array<string, DocumentNode> $documents */
77-
$documents = $this->commandBus->handle(new CompileDocumentsCommand($documents, new CompilerContext($this->projectNode)));
78-
$this->documents = $documents;
74+
$documents = $this->commandBus->handle(new CompileDocumentsCommand([$file => $document], new CompilerContext($this->projectNode)));
75+
$this->documents[$file] = $documents[$file];
7976
$destinationFileSystem = FlySystemAdapter::createForPath($this->settings->getOutput());
8077

8178
$documentIterator = DocumentListIterator::create(

packages/guides-restructured-text/src/RestructuredText/Directives/CsvTableDirective.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use function explode;
3434
use function implode;
3535
use function is_string;
36+
use function method_exists;
3637
use function strval;
3738
use function trim;
3839

@@ -78,10 +79,18 @@ public function processNode(
7879
return new GenericNode('csv-table');
7980
}
8081

81-
$csv = Reader::createFromStream($csvStream);
82+
if (method_exists(Reader::class, 'from')) {
83+
$csv = Reader::from($csvStream);
84+
} else {
85+
$csv = Reader::createFromStream($csvStream);
86+
}
8287
} else {
8388
$lines = $blockContext->getDocumentIterator()->toArray();
84-
$csv = Reader::createFromString(implode("\n", $lines));
89+
if (method_exists(Reader::class, 'fromString')) {
90+
$csv = Reader::fromString(implode("\n", $lines));
91+
} else {
92+
$csv = Reader::createFromString(implode("\n", $lines));
93+
}
8594
}
8695

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

91100
$header = null;
92101
if ($directive->hasOption('header')) {
93-
$headerCsv = Reader::createFromString($directive->getOption('header')->toString());
102+
if (method_exists(Reader::class, 'fromString')) {
103+
$headerCsv = Reader::fromString($directive->getOption('header')->toString());
104+
} else {
105+
$headerCsv = Reader::createFromString($directive->getOption('header')->toString());
106+
}
107+
94108
$header = new TableRow();
95109
foreach ($headerCsv->first() as $column) {
96110
$columnNode = new TableColumn($column, 1, []);

packages/guides-restructured-text/src/RestructuredText/Parser/InlineLexer.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919

2020
use function array_column;
2121
use function array_flip;
22+
use function ctype_alnum;
23+
use function ctype_space;
2224
use function parse_url;
2325
use function preg_match;
26+
use function str_ends_with;
27+
use function str_replace;
28+
use function strlen;
29+
use function substr;
2430

2531
use const PHP_URL_SCHEME;
2632

@@ -111,6 +117,7 @@ protected function getType(string &$value)
111117
{
112118
$type = match ($value) {
113119
'`' => self::BACKTICK,
120+
'``' => self::DOUBLE_BACKTICK,
114121
'**' => self::STRONG_DELIMITER,
115122
'*' => self::EMPHASIS_DELIMITER,
116123
'|' => self::VARIABLE_DELIMITER,
@@ -130,10 +137,22 @@ protected function getType(string &$value)
130137
}
131138

132139
// $value is already a tokenized part. Therefore, we have to match against the complete String here.
133-
if (preg_match('/^\\\\[\s\S]$/i', $value)) {
140+
if (str_ends_with($value, '__') && ctype_alnum(str_replace('-', '', substr($value, 0, -2)))) {
141+
return self::ANONYMOUSE_REFERENCE;
142+
}
143+
144+
if (str_ends_with($value, '_') && ctype_alnum(str_replace('-', '', substr($value, 0, -1)))) {
145+
return self::NAMED_REFERENCE;
146+
}
147+
148+
if (strlen($value) === 2 && $value[0] === '\\') {
134149
return self::ESCAPED_SIGN;
135150
}
136151

152+
if (strlen($value) === 1 && ctype_space($value)) {
153+
return self::WHITESPACE;
154+
}
155+
137156
if (preg_match('/^``.+``(?!`)$/i', $value)) {
138157
return self::LITERAL;
139158
}
@@ -146,18 +165,6 @@ protected function getType(string &$value)
146165
return self::EMAIL;
147166
}
148167

149-
if (preg_match('/^[a-z0-9-]+_{2}$/i', $value)) {
150-
return self::ANONYMOUSE_REFERENCE;
151-
}
152-
153-
if (preg_match('/^[a-z0-9-]+_{1}$/i', $value)) {
154-
return self::NAMED_REFERENCE;
155-
}
156-
157-
if (preg_match('/^\s$/i', $value)) {
158-
return self::WHITESPACE;
159-
}
160-
161168
return self::WORD;
162169
}
163170
}

packages/guides-restructured-text/tests/benchmarks/InlineLexerBench.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,15 @@ public function benchInlineLexer(): void
2626
$lexer = new InlineLexer();
2727
$lexer->setInput('This is a `link`_ to a section.');
2828
}
29+
30+
#[Revs([1000, 10_000])]
31+
#[Iterations(5)]
32+
public function benchFullParagraph(): void
33+
{
34+
$lexer = new InlineLexer();
35+
$lexer->setInput('
36+
With :issue:`103894` the new data processor :ref:`PageContentFetchingProcessor <feature-103894-1716544976>`
37+
has been introduced, to allow fetching page content based on the current page
38+
layout, taking the configured :php:`SlideMode` into account.');
39+
}
2940
}

packages/guides/src/Files.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
use Iterator;
1919
use IteratorAggregate;
2020

21+
use function array_key_exists;
2122
use function count;
22-
use function in_array;
2323
use function sort;
2424

2525
use const SORT_FLAG_CASE;

0 commit comments

Comments
 (0)