Skip to content

Commit 561ddac

Browse files
authored
Merge pull request #628 from phpDocumentor/fix/anoymous_link_processing
[FIX]: be more strict on anonymous links
2 parents 08e2c37 + 09fa6ff commit 561ddac

File tree

14 files changed

+47
-55
lines changed

14 files changed

+47
-55
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use RuntimeException;
2222

2323
use function array_merge;
24+
use function array_shift;
25+
use function strtolower;
26+
use function trim;
2427

2528
/**
2629
* Our document parser contains
@@ -42,6 +45,12 @@ class DocumentParserContext
4245
/** @var string[] */
4346
private array $titleLetters = [];
4447

48+
/** @var array<string, string> */
49+
private array $links = [];
50+
51+
/** @var string[] */
52+
private array $anonymous = [];
53+
4554
public function __construct(
4655
private readonly ParserContext $context,
4756
TextRoleFactory $textRoleFactory,
@@ -110,6 +119,33 @@ public function setCodeBlockDefaultLanguage(string $codeBlockDefaultLanguage): v
110119
$this->codeBlockDefaultLanguage = $codeBlockDefaultLanguage;
111120
}
112121

122+
public function setLink(string $name, string $url): void
123+
{
124+
$name = strtolower(trim($name));
125+
126+
if ($name === '_') {
127+
$name = array_shift($this->anonymous);
128+
}
129+
130+
$this->links[$name] = trim($url);
131+
}
132+
133+
public function resetAnonymousStack(): void
134+
{
135+
$this->anonymous = [];
136+
}
137+
138+
public function pushAnonymous(string $name): void
139+
{
140+
$this->anonymous[] = strtolower(trim($name));
141+
}
142+
143+
/** @return array<string, string> */
144+
public function getLinks(): array
145+
{
146+
return $this->links;
147+
}
148+
113149
/** @return array<string, string> */
114150
public function getLoggerInformation(): array
115151
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ protected function getCatchablePatterns(): array
5757
'\\\\``', // must be a separate case, as the next pattern would split in "\`" + "`", causing it to become a intepreted text
5858
'\\\\[\s\S]', // Escaping hell... needs escaped slash in regex, but also in php.
5959
'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}',
60-
'[a-z0-9-]+_{2}', //Inline href.
61-
'[a-z0-9-]+_{1}(?=[\s\.+]|$)', //Inline href.
60+
'(?<=^|\s)[a-z0-9-]+_{2}', //Inline href.
61+
'(?<=^|\s)[a-z0-9-]+_{1}(?=[\s\.+]|$)', //Inline href.
6262
'``.+?``(?!`)',
6363
'_{2}',
6464
'_',

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/DocumentRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public function apply(BlockContext $blockContext, CompoundNode|null $on = null):
5252
$this->structuralElements->apply($blockContext, $on);
5353
}
5454

55+
$on->setLinks($blockContext->getDocumentParserContext()->getLinks());
56+
5557
return $on;
5658
}
5759
}

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/AnonymousPhraseRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ public function apply(BlockContext $blockContext, InlineLexer $lexer): HyperLink
6666

6767
private function createAnonymousReference(BlockContext $blockContext, string $link, string|null $embeddedUrl): HyperLinkNode
6868
{
69-
$blockContext->getDocumentParserContext()->getContext()->resetAnonymousStack();
7069
$node = $this->createReference($blockContext, $link, $embeddedUrl, false);
71-
$blockContext->getDocumentParserContext()->getContext()->pushAnonymous($link);
70+
$blockContext->getDocumentParserContext()->pushAnonymous($link);
7271

7372
return $node;
7473
}

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/AnonymousReferenceRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ public function apply(BlockContext $blockContext, InlineLexer $lexer): HyperLink
3939

4040
private function createAnonymousReference(BlockContext $blockContext, string $link): HyperLinkNode
4141
{
42-
$blockContext->getDocumentParserContext()->getContext()->resetAnonymousStack();
4342
$node = $this->createReference($blockContext, $link, null, false);
44-
$blockContext->getDocumentParserContext()->getContext()->pushAnonymous($link);
43+
$blockContext->getDocumentParserContext()->pushAnonymous($link);
4544

4645
return $node;
4746
}

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/ReferenceRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function createReference(BlockContext $blockContext, string $link, str
2222
$link = trim(preg_replace('/\s+/', ' ', $link) ?? '');
2323

2424
if ($registerLink && $embeddedUrl !== null) {
25-
$blockContext->getDocumentParserContext()->getContext()->setLink($link, $embeddedUrl);
25+
$blockContext->getDocumentParserContext()->setLink($link, $embeddedUrl);
2626
}
2727

2828
return new HyperLinkNode($link, $embeddedUrl ?? $link);

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/LinkRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function apply(BlockContext $blockContext, CompoundNode|null $on = null):
5353
}
5454

5555
//TODO: pass link object to setLink
56-
$blockContext->getDocumentParserContext()->getContext()->setLink($link->getName(), $link->getUrl());
56+
$blockContext->getDocumentParserContext()->setLink($link->getName(), $link->getUrl());
5757

5858
return $node;
5959
}

packages/guides-restructured-text/tests/unit/Parser/Productions/LinkRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testParseLink(
2929
self::assertTrue($this->rule->applies($blockContext));
3030
self::assertEquals($expectedNode, $this->rule->apply($blockContext));
3131

32-
self::assertSame([$expectedLabel => $expectedUrl], $blockContext->getDocumentParserContext()->getContext()->getLinks());
32+
self::assertSame([$expectedLabel => $expectedUrl], $blockContext->getDocumentParserContext()->getLinks());
3333
}
3434

3535
/** @return Generator<string, array{string, string, string, AnchorNode|null}> */

packages/guides/src/Parser.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ public function parse(
8888
$parser = $this->determineParser($inputFormat);
8989

9090
$document = $parser->parse($this->parserContext, $text);
91-
$document->setLinks($this->parserContext->getLinks());
9291

9392
$this->parserContext = null;
9493

packages/guides/src/ParserContext.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,11 @@
99
use League\Uri\UriInfo;
1010
use phpDocumentor\Guides\Nodes\ProjectNode;
1111

12-
use function array_shift;
1312
use function dirname;
1413
use function ltrim;
15-
use function strtolower;
16-
use function trim;
1714

1815
class ParserContext
1916
{
20-
/** @var array<string, string> */
21-
private array $links = [];
22-
23-
/** @var string[] */
24-
private array $anonymous = [];
25-
2617
public function __construct(
2718
private readonly ProjectNode $projectNode,
2819
private readonly string $currentFileName,
@@ -43,33 +34,6 @@ public function getInitialHeaderLevel(): int
4334
return $this->initialHeaderLevel;
4435
}
4536

46-
public function setLink(string $name, string $url): void
47-
{
48-
$name = strtolower(trim($name));
49-
50-
if ($name === '_') {
51-
$name = array_shift($this->anonymous);
52-
}
53-
54-
$this->links[$name] = trim($url);
55-
}
56-
57-
public function resetAnonymousStack(): void
58-
{
59-
$this->anonymous = [];
60-
}
61-
62-
public function pushAnonymous(string $name): void
63-
{
64-
$this->anonymous[] = strtolower(trim($name));
65-
}
66-
67-
/** @return array<string, string> */
68-
public function getLinks(): array
69-
{
70-
return $this->links;
71-
}
72-
7337
public function absoluteRelativePath(string $url): string
7438
{
7539
$uri = Uri::createFromString($url);

0 commit comments

Comments
 (0)