Skip to content

Commit a48668a

Browse files
authored
Merge pull request #1325 from phpDocumentor/tilde-nbsp-deprecation
Tilde nbsp deprecation
2 parents 3a9048d + 81ad5ac commit a48668a

4 files changed

Lines changed: 29 additions & 4 deletions

File tree

packages/guides-restructured-text/resources/config/guides-restructured-text.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@
373373
->set(DocumentRule::class)
374374
->set(InlineParser::class)
375375
->arg('$inlineRules', tagged_iterator('phpdoc.guides.parser.rst.inline_rule'))
376+
->arg('$disableLegacyTilde', false)
376377
->set(GlobSearcher::class)
377378
->set(ToctreeBuilder::class)
378379
->set(InlineMarkupRule::class)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
/** @extends AbstractLexer<int, string> */
3535
final class InlineLexer extends AbstractLexer
3636
{
37+
public function __construct(private readonly bool $disableLegacyTilde = false)
38+
{
39+
}
40+
3741
public const WORD = 1;
3842
public const UNDERSCORE = 2;
3943
public const ANONYMOUS_END = 3;
@@ -131,7 +135,7 @@ protected function getType(string &$value)
131135
'#' => self::OCTOTHORPE,
132136
'[' => self::ANNOTATION_START,
133137
']' => self::ANNOTATION_END,
134-
'~' => self::NBSP,
138+
'~' => $this->disableLegacyTilde ? null : self::NBSP,
135139
'\\' => self::BACKSLASH,
136140
default => null,
137141
};

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ class InlineParser
3333
private array $cache = [];
3434

3535
/** @param iterable<InlineRule> $inlineRules */
36-
public function __construct(iterable $inlineRules)
37-
{
36+
public function __construct(
37+
iterable $inlineRules,
38+
private readonly bool $disableLegacyTilde = false,
39+
) {
3840
$this->rules = array_filter([...$inlineRules], static fn ($rule) => $rule instanceof CachableInlineRule === false);
3941
usort($this->rules, static fn (InlineRule $a, InlineRule $b): int => $a->getPriority() > $b->getPriority() ? -1 : 1);
4042
foreach ($inlineRules as $rule) {
@@ -48,7 +50,7 @@ public function __construct(iterable $inlineRules)
4850

4951
public function parse(string $content, BlockContext $blockContext): InlineCompoundNode
5052
{
51-
$lexer = new InlineLexer();
53+
$lexer = new InlineLexer($this->disableLegacyTilde);
5254
$lexer->setInput($content);
5355
$lexer->moveNext();
5456
$lexer->moveNext();

packages/guides-restructured-text/tests/unit/Parser/InlineLexerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ public function testLexer(string $input, array $result): void
3434
}
3535
}
3636

37+
public function testTildeIsNbspByDefault(): void
38+
{
39+
$lexer = new InlineLexer();
40+
$lexer->setInput('~');
41+
$lexer->moveNext();
42+
$lexer->moveNext();
43+
assertEquals(InlineLexer::NBSP, $lexer->token?->type);
44+
}
45+
46+
public function testTildeIsWordWhenLegacyTildeDisabled(): void
47+
{
48+
$lexer = new InlineLexer(disableLegacyTilde: true);
49+
$lexer->setInput('~');
50+
$lexer->moveNext();
51+
$lexer->moveNext();
52+
assertEquals(InlineLexer::WORD, $lexer->token?->type);
53+
}
54+
3755
/** @return array<string, array<string | int[]>> */
3856
public static function inlineLexerProvider(): array
3957
{

0 commit comments

Comments
 (0)