From 3d547081d901dffc162e1edc852dcd109b9f7f82 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Tue, 14 Feb 2023 20:07:19 +0100 Subject: [PATCH] Replace `{version}` in URLs --- src/KernelFactory.php | 3 ++- src/Renderers/SpanNodeRenderer.php | 14 +++++++++++--- src/SymfonyHTMLFormat.php | 7 +++++-- tests/IntegrationTest.php | 4 ++++ .../fixtures/expected/blocks/nodes/span-link.html | 3 +++ tests/fixtures/source/blocks/nodes/span-link.rst | 3 +++ 6 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 tests/fixtures/expected/blocks/nodes/span-link.html create mode 100644 tests/fixtures/source/blocks/nodes/span-link.rst diff --git a/src/KernelFactory.php b/src/KernelFactory.php index 3f808735..8d640d8a 100644 --- a/src/KernelFactory.php +++ b/src/KernelFactory.php @@ -44,7 +44,8 @@ public static function createKernel(BuildConfig $buildConfig, ?UrlChecker $urlCh new SymfonyHTMLFormat( $configuration->getTemplateRenderer(), $configuration->getFormat(), - $urlChecker + $urlChecker, + $buildConfig->getSymfonyVersion() ) ); diff --git a/src/Renderers/SpanNodeRenderer.php b/src/Renderers/SpanNodeRenderer.php index b99efec2..65598bd2 100644 --- a/src/Renderers/SpanNodeRenderer.php +++ b/src/Renderers/SpanNodeRenderer.php @@ -25,23 +25,27 @@ class SpanNodeRenderer extends AbstractSpanNodeRenderer private $decoratedSpanNodeRenderer; /** @var UrlChecker|null */ private $urlChecker; + private $symfonyVersion; public function __construct( Environment $environment, SpanNode $span, BaseSpanNodeRenderer $decoratedSpanNodeRenderer, - ?UrlChecker $urlChecker = null - ) { + ?UrlChecker $urlChecker = null, + string $symfonyVersion = null + ) + { parent::__construct($environment, $span); $this->decoratedSpanNodeRenderer = $decoratedSpanNodeRenderer; $this->urlChecker = $urlChecker; + $this->symfonyVersion = $symfonyVersion; } /** @inheritDoc */ public function link(?string $url, string $title, array $attributes = []): string { - $url = (string)$url; + $url = (string) $url; if ( $this->urlChecker && @@ -55,6 +59,10 @@ public function link(?string $url, string $title, array $attributes = []): strin $attributes = $this->addAttributesForUnsafeUrl($attributes); } + if (null !== $this->symfonyVersion) { + $url = u($url)->replace('{version}', $this->symfonyVersion)->toString(); + } + return $this->decoratedSpanNodeRenderer->link($url, $title, $attributes); } diff --git a/src/SymfonyHTMLFormat.php b/src/SymfonyHTMLFormat.php index bf010904..6dcb04b5 100644 --- a/src/SymfonyHTMLFormat.php +++ b/src/SymfonyHTMLFormat.php @@ -29,12 +29,14 @@ final class SymfonyHTMLFormat implements Format private $htmlFormat; /** @var UrlChecker|null */ private $urlChecker; + private $symfonyVersion; - public function __construct(TemplateRenderer $templateRenderer, Format $HTMLFormat, ?UrlChecker $urlChecker = null) + public function __construct(TemplateRenderer $templateRenderer, Format $HTMLFormat, ?UrlChecker $urlChecker = null, string $symfonyVersion = null) { $this->templateRenderer = $templateRenderer; $this->htmlFormat = $HTMLFormat; $this->urlChecker = $urlChecker; + $this->symfonyVersion = $symfonyVersion; } public function getFileExtension(): string @@ -69,7 +71,8 @@ function (SpanNode $node) { $node->getEnvironment(), $node, new BaseSpanNodeRenderer($node->getEnvironment(), $node, $this->templateRenderer), - $this->urlChecker + $this->urlChecker, + $this->symfonyVersion ); } ); diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 3be62d5d..6e4930db 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -143,6 +143,10 @@ public function parserUnitBlockProvider() 'blockName' => 'nodes/figure', ]; + yield 'span-link' => [ + 'blockName' => 'nodes/span-link', + ]; + yield 'title' => [ 'blockName' => 'nodes/title', ]; diff --git a/tests/fixtures/expected/blocks/nodes/span-link.html b/tests/fixtures/expected/blocks/nodes/span-link.html new file mode 100644 index 00000000..ea8a9d33 --- /dev/null +++ b/tests/fixtures/expected/blocks/nodes/span-link.html @@ -0,0 +1,3 @@ +

README file with version substitution

+ +

README file without version substitution

\ No newline at end of file diff --git a/tests/fixtures/source/blocks/nodes/span-link.rst b/tests/fixtures/source/blocks/nodes/span-link.rst new file mode 100644 index 00000000..3ea20922 --- /dev/null +++ b/tests/fixtures/source/blocks/nodes/span-link.rst @@ -0,0 +1,3 @@ +`README file with version substitution `_ + +`README file without version substitution `_ \ No newline at end of file