diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/DocumentParserContext.php b/packages/guides-restructured-text/src/RestructuredText/Parser/DocumentParserContext.php index 73aa5364c..815bb9ed5 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Parser/DocumentParserContext.php +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/DocumentParserContext.php @@ -21,6 +21,9 @@ use RuntimeException; use function array_merge; +use function array_shift; +use function strtolower; +use function trim; /** * Our document parser contains @@ -42,6 +45,12 @@ class DocumentParserContext /** @var string[] */ private array $titleLetters = []; + /** @var array */ + private array $links = []; + + /** @var string[] */ + private array $anonymous = []; + public function __construct( private readonly ParserContext $context, TextRoleFactory $textRoleFactory, @@ -110,6 +119,33 @@ public function setCodeBlockDefaultLanguage(string $codeBlockDefaultLanguage): v $this->codeBlockDefaultLanguage = $codeBlockDefaultLanguage; } + public function setLink(string $name, string $url): void + { + $name = strtolower(trim($name)); + + if ($name === '_') { + $name = array_shift($this->anonymous); + } + + $this->links[$name] = trim($url); + } + + public function resetAnonymousStack(): void + { + $this->anonymous = []; + } + + public function pushAnonymous(string $name): void + { + $this->anonymous[] = strtolower(trim($name)); + } + + /** @return array */ + public function getLinks(): array + { + return $this->links; + } + /** @return array */ public function getLoggerInformation(): array { diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/InlineLexer.php b/packages/guides-restructured-text/src/RestructuredText/Parser/InlineLexer.php index 0c2bb64e5..133737318 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Parser/InlineLexer.php +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/InlineLexer.php @@ -57,8 +57,8 @@ protected function getCatchablePatterns(): array '\\\\``', // must be a separate case, as the next pattern would split in "\`" + "`", causing it to become a intepreted text '\\\\[\s\S]', // Escaping hell... needs escaped slash in regex, but also in php. '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}', - '[a-z0-9-]+_{2}', //Inline href. - '[a-z0-9-]+_{1}(?=[\s\.+]|$)', //Inline href. + '(?<=^|\s)[a-z0-9-]+_{2}', //Inline href. + '(?<=^|\s)[a-z0-9-]+_{1}(?=[\s\.+]|$)', //Inline href. '``.+?``(?!`)', '_{2}', '_', diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/DocumentRule.php b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/DocumentRule.php index 0d23ae32e..db6db0d97 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/DocumentRule.php +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/DocumentRule.php @@ -52,6 +52,8 @@ public function apply(BlockContext $blockContext, CompoundNode|null $on = null): $this->structuralElements->apply($blockContext, $on); } + $on->setLinks($blockContext->getDocumentParserContext()->getLinks()); + return $on; } } diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/AnonymousPhraseRule.php b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/AnonymousPhraseRule.php index 857cfcd32..697d05742 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/AnonymousPhraseRule.php +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/AnonymousPhraseRule.php @@ -66,9 +66,8 @@ public function apply(BlockContext $blockContext, InlineLexer $lexer): HyperLink private function createAnonymousReference(BlockContext $blockContext, string $link, string|null $embeddedUrl): HyperLinkNode { - $blockContext->getDocumentParserContext()->getContext()->resetAnonymousStack(); $node = $this->createReference($blockContext, $link, $embeddedUrl, false); - $blockContext->getDocumentParserContext()->getContext()->pushAnonymous($link); + $blockContext->getDocumentParserContext()->pushAnonymous($link); return $node; } diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/AnonymousReferenceRule.php b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/AnonymousReferenceRule.php index 6dd2f20e9..4b7bedd8f 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/AnonymousReferenceRule.php +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/AnonymousReferenceRule.php @@ -39,9 +39,8 @@ public function apply(BlockContext $blockContext, InlineLexer $lexer): HyperLink private function createAnonymousReference(BlockContext $blockContext, string $link): HyperLinkNode { - $blockContext->getDocumentParserContext()->getContext()->resetAnonymousStack(); $node = $this->createReference($blockContext, $link, null, false); - $blockContext->getDocumentParserContext()->getContext()->pushAnonymous($link); + $blockContext->getDocumentParserContext()->pushAnonymous($link); return $node; } diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/ReferenceRule.php b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/ReferenceRule.php index 98683f82d..dbaeb8ed2 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/ReferenceRule.php +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/ReferenceRule.php @@ -22,7 +22,7 @@ protected function createReference(BlockContext $blockContext, string $link, str $link = trim(preg_replace('/\s+/', ' ', $link) ?? ''); if ($registerLink && $embeddedUrl !== null) { - $blockContext->getDocumentParserContext()->getContext()->setLink($link, $embeddedUrl); + $blockContext->getDocumentParserContext()->setLink($link, $embeddedUrl); } return new HyperLinkNode($link, $embeddedUrl ?? $link); diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/LinkRule.php b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/LinkRule.php index a66ee7b34..8447a2ee3 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/LinkRule.php +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/LinkRule.php @@ -53,7 +53,7 @@ public function apply(BlockContext $blockContext, CompoundNode|null $on = null): } //TODO: pass link object to setLink - $blockContext->getDocumentParserContext()->getContext()->setLink($link->getName(), $link->getUrl()); + $blockContext->getDocumentParserContext()->setLink($link->getName(), $link->getUrl()); return $node; } diff --git a/packages/guides-restructured-text/tests/unit/Parser/Productions/LinkRuleTest.php b/packages/guides-restructured-text/tests/unit/Parser/Productions/LinkRuleTest.php index 72c76b2c8..f0ece8d46 100644 --- a/packages/guides-restructured-text/tests/unit/Parser/Productions/LinkRuleTest.php +++ b/packages/guides-restructured-text/tests/unit/Parser/Productions/LinkRuleTest.php @@ -29,7 +29,7 @@ public function testParseLink( self::assertTrue($this->rule->applies($blockContext)); self::assertEquals($expectedNode, $this->rule->apply($blockContext)); - self::assertSame([$expectedLabel => $expectedUrl], $blockContext->getDocumentParserContext()->getContext()->getLinks()); + self::assertSame([$expectedLabel => $expectedUrl], $blockContext->getDocumentParserContext()->getLinks()); } /** @return Generator */ diff --git a/packages/guides/src/Parser.php b/packages/guides/src/Parser.php index a1d653681..71b2a5dfb 100644 --- a/packages/guides/src/Parser.php +++ b/packages/guides/src/Parser.php @@ -88,7 +88,6 @@ public function parse( $parser = $this->determineParser($inputFormat); $document = $parser->parse($this->parserContext, $text); - $document->setLinks($this->parserContext->getLinks()); $this->parserContext = null; diff --git a/packages/guides/src/ParserContext.php b/packages/guides/src/ParserContext.php index adf527223..7d333db2c 100644 --- a/packages/guides/src/ParserContext.php +++ b/packages/guides/src/ParserContext.php @@ -9,20 +9,11 @@ use League\Uri\UriInfo; use phpDocumentor\Guides\Nodes\ProjectNode; -use function array_shift; use function dirname; use function ltrim; -use function strtolower; -use function trim; class ParserContext { - /** @var array */ - private array $links = []; - - /** @var string[] */ - private array $anonymous = []; - public function __construct( private readonly ProjectNode $projectNode, private readonly string $currentFileName, @@ -43,33 +34,6 @@ public function getInitialHeaderLevel(): int return $this->initialHeaderLevel; } - public function setLink(string $name, string $url): void - { - $name = strtolower(trim($name)); - - if ($name === '_') { - $name = array_shift($this->anonymous); - } - - $this->links[$name] = trim($url); - } - - public function resetAnonymousStack(): void - { - $this->anonymous = []; - } - - public function pushAnonymous(string $name): void - { - $this->anonymous[] = strtolower(trim($name)); - } - - /** @return array */ - public function getLinks(): array - { - return $this->links; - } - public function absoluteRelativePath(string $url): string { $uri = Uri::createFromString($url); diff --git a/tests/Functional/tests/anonymous/anonymous.html b/tests/Functional/tests/anonymous/anonymous.html index 83602011c..811e84ecc 100644 --- a/tests/Functional/tests/anonymous/anonymous.html +++ b/tests/Functional/tests/anonymous/anonymous.html @@ -1,2 +1 @@ -SKIP anonymous references don't work -

I love GitHub, GitLab and Facebook. But I don't affect references to __CLASS__.

+

I love GitHub, GitLab and Facebook. But I don't affect references to __CLASS__.

diff --git a/tests/Functional/tests/anonymous/anonymous.rst b/tests/Functional/tests/anonymous/anonymous.rst index 938f8ebd0..795e826ba 100644 --- a/tests/Functional/tests/anonymous/anonymous.rst +++ b/tests/Functional/tests/anonymous/anonymous.rst @@ -1,5 +1,5 @@ I love GitHub__, GitLab__ and `Facebook`__. But I don't affect references to __CLASS__. -.. __: https://github.com/ +__ https://github.com/ .. __: https://gitlab.com/ .. __: https://facebook.com/ diff --git a/tests/Functional/tests/image-inline-anonymous/image-inline-anonymous.html b/tests/Functional/tests/image-inline-anonymous/image-inline-anonymous.html deleted file mode 100644 index f784c7c0e..000000000 --- a/tests/Functional/tests/image-inline-anonymous/image-inline-anonymous.html +++ /dev/null @@ -1,2 +0,0 @@ -SKIP anonymous inline images do not work -

This is also animage.

diff --git a/tests/Functional/tests/image-inline-anonymous/image-inline-anonymous.rst b/tests/Functional/tests/image-inline-anonymous/image-inline-anonymous.rst deleted file mode 100644 index d301baf1f..000000000 --- a/tests/Functional/tests/image-inline-anonymous/image-inline-anonymous.rst +++ /dev/null @@ -1,4 +0,0 @@ -SKIP anonymous inline images do not work -This is also an :variable:`inline` image. - -.. |inline| image:: test.jpg