From 8f09af6ad644414af3ed60f40a473ae4d8340f4f Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 26 Aug 2024 13:07:21 +0200 Subject: [PATCH 01/13] Highlight nested PHP comments --- tests/IntegrationTest.php | 4 ++ .../code-blocks/php-nested-comments.html | 72 +++++++++++++++++++ .../code-blocks/php-nested-comments.rst | 12 ++++ 3 files changed, 88 insertions(+) create mode 100644 tests/fixtures/expected/blocks/code-blocks/php-nested-comments.html create mode 100644 tests/fixtures/source/blocks/code-blocks/php-nested-comments.rst diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index f121ba3..95fef1d 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -294,6 +294,10 @@ public function parserUnitBlockProvider() 'blockName' => 'code-blocks/php-attributes', ]; + yield 'code-block-php-nested-comments' => [ + 'blockName' => 'code-blocks/php-nested-comments', + ]; + yield 'code-block-text' => [ 'blockName' => 'code-blocks/text', ]; diff --git a/tests/fixtures/expected/blocks/code-blocks/php-nested-comments.html b/tests/fixtures/expected/blocks/code-blocks/php-nested-comments.html new file mode 100644 index 0000000..e1f7078 --- /dev/null +++ b/tests/fixtures/expected/blocks/code-blocks/php-nested-comments.html @@ -0,0 +1,72 @@ +

You can do that by adding a "stamp" to your message:

+
+
+
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+
+            
+                use
+                Symfony
+                \
+                Component
+                \
+                Messenger
+                \
+                MessageBusInterface
+                ;
+                use
+                Symfony
+                \
+                Component
+                \
+                Messenger
+                \
+                Stamp
+                \
+                DelayStamp
+                ;
+                public
+                
+                    function
+                    index
+                    
+                        (MessageBusInterface
+                        
+                            $
+                            bus
+                        
+                        )
+                    
+                
+                {
+                
+                    $
+                    bus
+                
+                ->
+                dispatch
+                (
+                new
+                SmsNotification
+                (
+                '...'
+                ), [
+                // wait 5 seconds before processing
+                new
+                DelayStamp
+                (
+                5000
+                ), ]);
+}
+            
+        
+
+
diff --git a/tests/fixtures/source/blocks/code-blocks/php-nested-comments.rst b/tests/fixtures/source/blocks/code-blocks/php-nested-comments.rst new file mode 100644 index 0000000..ea8dc0f --- /dev/null +++ b/tests/fixtures/source/blocks/code-blocks/php-nested-comments.rst @@ -0,0 +1,12 @@ +You can do that by adding a "stamp" to your message:: + + use Symfony\Component\Messenger\MessageBusInterface; + use Symfony\Component\Messenger\Stamp\DelayStamp; + + public function index(MessageBusInterface $bus) + { + $bus->dispatch(new SmsNotification('...'), [ + // wait 5 seconds before processing + new DelayStamp(5000), + ]); + } From 3a300ac19a9338c4e05fd1a8434309eb83805269 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 11 Sep 2024 11:19:33 +0200 Subject: [PATCH 02/13] Add support for tilde characters --- tests/IntegrationTest.php | 4 ++++ tests/fixtures/expected/blocks/nodes/text.html | 7 +++++++ tests/fixtures/source/blocks/nodes/text.rst | 7 +++++++ 3 files changed, 18 insertions(+) create mode 100644 tests/fixtures/expected/blocks/nodes/text.html create mode 100644 tests/fixtures/source/blocks/nodes/text.rst diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index f121ba3..3045334 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -142,6 +142,10 @@ public function parserUnitBlockProvider() 'blockName' => 'nodes/span-link', ]; + yield 'text' => [ + 'blockName' => 'nodes/text', + ]; + yield 'title' => [ 'blockName' => 'nodes/title', ]; diff --git a/tests/fixtures/expected/blocks/nodes/text.html b/tests/fixtures/expected/blocks/nodes/text.html new file mode 100644 index 0000000..afc3337 --- /dev/null +++ b/tests/fixtures/expected/blocks/nodes/text.html @@ -0,0 +1,7 @@ +

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut enim ad minim +veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea +commodo consequat. Duis aute irure dolor in reprehenderit in voluptate +velit esse cillum dolore eu fugiat nulla pariatur.

+ +

Excepteur ~1,000 sint! Occaecat cupidatat non proident, sunt in culpa qui officia +deserunt mollit anim id est laborum. áàâäãåéèêëíìîïóòôöõúùûüñçÿ

diff --git a/tests/fixtures/source/blocks/nodes/text.rst b/tests/fixtures/source/blocks/nodes/text.rst new file mode 100644 index 0000000..f5dbb33 --- /dev/null +++ b/tests/fixtures/source/blocks/nodes/text.rst @@ -0,0 +1,7 @@ +**Lorem ipsum** dolor sit amet, consectetur adipisicing elit. Ut enim ad minim +veniam, quis nostrud *exercitation ullamco laboris* nisi ut aliquip ex ea +commodo consequat. ``Duis aute irure dolor`` in reprehenderit in voluptate +velit esse cillum dolore eu fugiat nulla pariatur. + +Excepteur ~1,000 sint! Occaecat cupidatat non proident, sunt in culpa qui officia +deserunt mollit anim id est laborum. áàâäãåéèêëíìîïóòôöõúùûüñçÿ From 9223ccc89e6c7b0ab78c0af755ce85ebaa918668 Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Wed, 11 Sep 2024 15:51:17 +0200 Subject: [PATCH 03/13] Temporary replace tildes to avoid it being parsed as NBSP --- src/Renderers/SpanNodeRenderer.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Renderers/SpanNodeRenderer.php b/src/Renderers/SpanNodeRenderer.php index 65598bd..f9db721 100644 --- a/src/Renderers/SpanNodeRenderer.php +++ b/src/Renderers/SpanNodeRenderer.php @@ -42,6 +42,19 @@ public function __construct( $this->symfonyVersion = $symfonyVersion; } + public function render(): string + { + // Work around "~" being parsed as non-breaking space by rst-parser, + // while this is not part of the specification. + $spanValue = $this->span->getValue(); + $spanValue = str_replace('~', '__TILDE__', $spanValue); + $this->span->setValue($spanValue); + + $rendered = parent::render(); + + return str_replace('__TILDE__', '~', $rendered); + } + /** @inheritDoc */ public function link(?string $url, string $title, array $attributes = []): string { From caacb35c69edfe26451c4bb6e3cabb4156938c07 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 12 Sep 2024 11:27:42 +0200 Subject: [PATCH 04/13] Minor tweak --- src/Renderers/SpanNodeRenderer.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Renderers/SpanNodeRenderer.php b/src/Renderers/SpanNodeRenderer.php index f9db721..927c3fa 100644 --- a/src/Renderers/SpanNodeRenderer.php +++ b/src/Renderers/SpanNodeRenderer.php @@ -47,6 +47,11 @@ public function render(): string // Work around "~" being parsed as non-breaking space by rst-parser, // while this is not part of the specification. $spanValue = $this->span->getValue(); + + if (str_contains($spanValue, '__TILDE__')) { + throw new \Exception('Cannot render content containing the text "__TILDE__" as it is used as a special placeholder in the build.'); + } + $spanValue = str_replace('~', '__TILDE__', $spanValue); $this->span->setValue($spanValue); From f34b7a73d98a77db5b492c9a53fe8d520cda8279 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 12 Sep 2024 12:03:00 +0200 Subject: [PATCH 05/13] Don't use the deprecated spaceless filter --- src/Templates/default/html/figure.html.twig | 8 +------- src/Templates/default/html/link.html.twig | 2 -- src/Templates/default/html/toc-level.html.twig | 12 +++++------- src/Templates/default/html/toc.html.twig | 10 ++++------ tests/fixtures/expected/build-pdf/book.html | 4 ++-- tests/fixtures/expected/main/index.html | 2 +- tests/fixtures/expected/toctree/index.html | 10 +++++----- 7 files changed, 18 insertions(+), 30 deletions(-) diff --git a/src/Templates/default/html/figure.html.twig b/src/Templates/default/html/figure.html.twig index e1786c0..5836788 100644 --- a/src/Templates/default/html/figure.html.twig +++ b/src/Templates/default/html/figure.html.twig @@ -1,12 +1,7 @@ {# Overridden to fix lack of figclass support (class attribute on +
{{ figureNode.image.render()|raw }} {% if figureNode.document %} @@ -17,4 +12,3 @@ Overridden to fix lack of figclass support (class attribute on -{% endapply %} diff --git a/src/Templates/default/html/link.html.twig b/src/Templates/default/html/link.html.twig index 8b9ca5b..7c82a66 100644 --- a/src/Templates/default/html/link.html.twig +++ b/src/Templates/default/html/link.html.twig @@ -1,5 +1,3 @@ -{% apply spaceless %} {% set domElement = attributes.domElement|default %} {% set class = (attributes.class|default ? attributes.class ~ ' ' : '') ~ 'reference ' ~ (('://' in url) ? 'external' : 'internal') %} key != 'domElement' and key != 'class') %} {{ key }}="{{ value }}"{% endfor %}>{% if domElement %}<{{ domElement }}>{% endif %}{{ title|raw }}{% if domElement %}{% endif %} -{% endapply %} diff --git a/src/Templates/default/html/toc-level.html.twig b/src/Templates/default/html/toc-level.html.twig index 1bdaef8..8c3eb34 100644 --- a/src/Templates/default/html/toc-level.html.twig +++ b/src/Templates/default/html/toc-level.html.twig @@ -1,7 +1,5 @@ -{% apply spaceless %} -
    - {% for tocItem in tocItems %} - {% include "toc-item.html.twig" with { toc_deep_level: 1 } %} - {% endfor %} -
-{% endapply %} +
    + {% for tocItem in tocItems %} + {% include "toc-item.html.twig" with { toc_deep_level: 1 } %} + {% endfor %} +
diff --git a/src/Templates/default/html/toc.html.twig b/src/Templates/default/html/toc.html.twig index 55b8737..8b9c883 100644 --- a/src/Templates/default/html/toc.html.twig +++ b/src/Templates/default/html/toc.html.twig @@ -1,6 +1,4 @@ -{% apply spaceless %} - {% set toc_options = toc_options(tocItems) %} -
- {% include "toc-level.html.twig" %} -
-{% endapply %} +{% set toc_options = toc_options(tocItems) %} +
+ {% include "toc-level.html.twig" %} +
diff --git a/tests/fixtures/expected/build-pdf/book.html b/tests/fixtures/expected/build-pdf/book.html index a214463..4454f94 100644 --- a/tests/fixtures/expected/build-pdf/book.html +++ b/tests/fixtures/expected/build-pdf/book.html @@ -8,8 +8,8 @@

Book

Here is a link to the main index

diff --git a/tests/fixtures/expected/main/index.html b/tests/fixtures/expected/main/index.html index 1346373..628cc91 100644 --- a/tests/fixtures/expected/main/index.html +++ b/tests/fixtures/expected/main/index.html @@ -10,7 +10,7 @@

Some Test Docs!

- +

A header

diff --git a/tests/fixtures/expected/toctree/index.html b/tests/fixtures/expected/toctree/index.html index e07a28e..676005a 100644 --- a/tests/fixtures/expected/toctree/index.html +++ b/tests/fixtures/expected/toctree/index.html @@ -9,11 +9,11 @@ From b4a10315d9e2074d490a01cb78706e0015b65b60 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Thu, 26 Sep 2024 22:09:28 +0200 Subject: [PATCH 06/13] Enhancement: Drop PHP 7.4 support (#183) --- .github/workflows/ci.yaml | 4 ++-- composer.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bc16178..4db2db0 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -20,7 +20,7 @@ jobs: uses: shivammathur/setup-php@v2 with: coverage: none - php-version: 7.4 + php-version: 8.3 tools: composer:v2 ini-values: date.timezone=UTC @@ -37,7 +37,7 @@ jobs: # add here only the PHP versions and OS used in GitHub CI (for tests) # and on the symfony.com server (where the Symfony Docs are built) operating-system: ['ubuntu-latest'] - php-version: ['7.4', '8.3'] + php-version: ['8.3'] steps: - name: 'Checkout code' diff --git a/composer.json b/composer.json index dde5a06..8e66ada 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } }, "require": { - "php": ">=7.4", + "php": ">=8.3", "ext-json": "*", "ext-curl": "*", "doctrine/rst-parser": "^0.5", From 126cfbff31822a56f39935f64d8da06265804a2b Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 27 Sep 2024 09:31:34 +0200 Subject: [PATCH 07/13] Add support for inline Twig comments Co-authored-by: Wouter de Jong --- src/Templates/highlight.php/twig.json | 18 +++++++++-- .../expected/blocks/code-blocks/twig.html | 30 +++++++++++++++++-- .../source/blocks/code-blocks/twig.rst | 10 ++++++- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/Templates/highlight.php/twig.json b/src/Templates/highlight.php/twig.json index b961028..ceb675d 100644 --- a/src/Templates/highlight.php/twig.json +++ b/src/Templates/highlight.php/twig.json @@ -25,6 +25,12 @@ "begin": "\\{%", "end": "%}", "contains": [ + { + "className": "comment", + "begin": "#", + "end": "$", + "endsWithParent": true + }, { "className": "name", "begin": "\\w+", @@ -32,6 +38,9 @@ "starts": { "endsWithParent": true, "contains": [ + { + "$ref": "#contains.1.contains.0" + }, { "begin": "\\|[A-Za-z_]+:?", "keywords": "abs batch capitalize column convert_encoding date date_modify default escape filter first format inky_to_html inline_css join json_encode keys last length lower map markdown merge nl2br number_format raw reduce replace reverse round slice sort spaceless split striptags title trim upper url_encode", @@ -53,7 +62,7 @@ ] }, { - "$ref": "#contains.1.contains.0.starts.contains.0.contains.0" + "$ref": "#contains.1.contains.1.starts.contains.1.contains.0" } ], "relevance": 0 @@ -68,10 +77,13 @@ "contains": [ "self", { - "$ref": "#contains.1.contains.0.starts.contains.0" + "$ref": "#contains.1.contains.0" + }, + { + "$ref": "#contains.1.contains.1.starts.contains.1" }, { - "$ref": "#contains.1.contains.0.starts.contains.0.contains.0" + "$ref": "#contains.1.contains.1.starts.contains.1.contains.0" } ] } diff --git a/tests/fixtures/expected/blocks/code-blocks/twig.html b/tests/fixtures/expected/blocks/code-blocks/twig.html index fab139d..83c841c 100644 --- a/tests/fixtures/expected/blocks/code-blocks/twig.html +++ b/tests/fixtures/expected/blocks/code-blocks/twig.html @@ -1,6 +1,30 @@ -
+
-
1
-
{# some code #}
+
1
+2
+3
+4
+5
+6
+7
+8
+9
+
+            
+                {# some code #}
+                
+                
+                    {%
+                        set some_var = 'some value' # some inline comment
+                    %}
+                
+                
+                {{
+                    # another inline comment
+                    'Lorem Ipsum'|uppercase
+                    # final inline comment
+                }}
+            
+        
diff --git a/tests/fixtures/source/blocks/code-blocks/twig.rst b/tests/fixtures/source/blocks/code-blocks/twig.rst index 01d0685..229041d 100644 --- a/tests/fixtures/source/blocks/code-blocks/twig.rst +++ b/tests/fixtures/source/blocks/code-blocks/twig.rst @@ -1,3 +1,11 @@ - .. code-block:: twig + {# some code #} + {% + set some_var = 'some value' # some inline comment + %} + {{ + # another inline comment + 'Lorem Ipsum'|uppercase + # final inline comment + }} From d05cf4202737306c383016e18f6454865a2492a1 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 12 Dec 2024 11:19:15 +0100 Subject: [PATCH 08/13] Fix PHP 8.4 deprecations --- src/Renderers/SpanNodeRenderer.php | 2 +- src/SymfonyHTMLFormat.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Renderers/SpanNodeRenderer.php b/src/Renderers/SpanNodeRenderer.php index 927c3fa..e1e7775 100644 --- a/src/Renderers/SpanNodeRenderer.php +++ b/src/Renderers/SpanNodeRenderer.php @@ -32,7 +32,7 @@ public function __construct( SpanNode $span, BaseSpanNodeRenderer $decoratedSpanNodeRenderer, ?UrlChecker $urlChecker = null, - string $symfonyVersion = null + ?string $symfonyVersion = null ) { parent::__construct($environment, $span); diff --git a/src/SymfonyHTMLFormat.php b/src/SymfonyHTMLFormat.php index 9c17ca9..16ea957 100644 --- a/src/SymfonyHTMLFormat.php +++ b/src/SymfonyHTMLFormat.php @@ -29,7 +29,7 @@ final class SymfonyHTMLFormat implements Format private $urlChecker; private $symfonyVersion; - public function __construct(TemplateRenderer $templateRenderer, Format $HTMLFormat, ?UrlChecker $urlChecker = null, string $symfonyVersion = null) + public function __construct(TemplateRenderer $templateRenderer, Format $HTMLFormat, ?UrlChecker $urlChecker = null, ?string $symfonyVersion = null) { $this->templateRenderer = $templateRenderer; $this->htmlFormat = $HTMLFormat; From f05792a72a09602f9c992d40d00c205b8d139ab9 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 12 Dec 2024 12:37:03 +0100 Subject: [PATCH 09/13] Fix a Twig deprecation (#186) --- src/Templates/default/html/image.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Templates/default/html/image.html.twig b/src/Templates/default/html/image.html.twig index f7ff3ff..14f090e 100644 --- a/src/Templates/default/html/image.html.twig +++ b/src/Templates/default/html/image.html.twig @@ -2,7 +2,7 @@ Overridden so we can control the path to the image based on our copying logic. See CopyImagesListener. #} -{% set wrap_image_with_browser = 'with-browser' in imageNode.options.class ?? '' %} +{% set wrap_image_with_browser = 'with-browser' in (imageNode.options.class ?? '') %} {% if wrap_image_with_browser %}
{% endif %} {% if wrap_image_with_browser %}
{% endif %} From b5a4aed0e702ce8cc30a82632e9dc6775d54510f Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 12 Dec 2024 13:41:00 +0100 Subject: [PATCH 10/13] Test code in PHP 8.4 too (#187) --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4db2db0..f85af4b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -37,7 +37,7 @@ jobs: # add here only the PHP versions and OS used in GitHub CI (for tests) # and on the symfony.com server (where the Symfony Docs are built) operating-system: ['ubuntu-latest'] - php-version: ['8.3'] + php-version: ['8.3', '8.4'] steps: - name: 'Checkout code' From af7ee47654250144e2c41b7ee9f869d50a7196a1 Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Tue, 31 Dec 2024 15:49:09 +0100 Subject: [PATCH 11/13] Allow one line comments in argument list --- src/Templates/highlight.php/php.json | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Templates/highlight.php/php.json b/src/Templates/highlight.php/php.json index 6f69970..0db2cb8 100644 --- a/src/Templates/highlight.php/php.json +++ b/src/Templates/highlight.php/php.json @@ -204,7 +204,15 @@ "_": "variable" }, { - "$ref": "#contains.10.contains.3.contains.2", + "$ref": "#contains.3", + "_": "comment" + }, + { + "$ref": "#contains.4", + "_": "comment" + }, + { + "$ref": "#contains.5", "_": "comment" }, { @@ -258,19 +266,8 @@ "_": "variable" }, { - "className": "comment", - "begin": "/\\*", - "end": "\\*/", - "contains": [ - { - "$ref": "#contains.2.contains.0" - }, - { - "className": "doctag", - "begin": "(?:TODO|FIXME|NOTE|BUG|XXX):", - "relevance": 0 - } - ] + "$ref": "#contains.5", + "_": "comment" }, { "className": "string", From 9a75cd7ebc7b195831b1a9f218e34c3da6d8fa24 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Thu, 20 Mar 2025 21:44:07 +0100 Subject: [PATCH 12/13] Add support for phpclass reference with namespace --- src/Reference/PhpClassReference.php | 3 ++- tests/fixtures/expected/blocks/references/php-class.html | 1 + tests/fixtures/source/blocks/references/php-class.rst | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Reference/PhpClassReference.php b/src/Reference/PhpClassReference.php index 4e104e1..0bb0ba8 100644 --- a/src/Reference/PhpClassReference.php +++ b/src/Reference/PhpClassReference.php @@ -29,10 +29,11 @@ public function getName(): string public function resolve(Environment $environment, string $data): ResolvedReference { + $classnamePath = str_replace('\\', '-', strtolower($data)); return new ResolvedReference( $environment->getCurrentFileName(), $data, - sprintf('%s/class.%s.php', $this->phpDocUrl, strtolower($data)), + sprintf('%s/class.%s.php', $this->phpDocUrl, $classnamePath), [], [ 'title' => $data, diff --git a/tests/fixtures/expected/blocks/references/php-class.html b/tests/fixtures/expected/blocks/references/php-class.html index 271b968..b1e018e 100644 --- a/tests/fixtures/expected/blocks/references/php-class.html +++ b/tests/fixtures/expected/blocks/references/php-class.html @@ -1 +1,2 @@

ArrayAccess

+

BcMath\Number

diff --git a/tests/fixtures/source/blocks/references/php-class.rst b/tests/fixtures/source/blocks/references/php-class.rst index 5708c64..f0782b4 100644 --- a/tests/fixtures/source/blocks/references/php-class.rst +++ b/tests/fixtures/source/blocks/references/php-class.rst @@ -1,2 +1,4 @@ :phpclass:`ArrayAccess` + +:phpclass:`BcMath\Number` From 44b5818ee8c238b07ec8a16f8a863ed644d0b8f5 Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Mon, 7 Apr 2025 13:02:32 +0200 Subject: [PATCH 13/13] Make behavior of PHP and GitHub references consistent (#191) --- src/Reference/MethodReference.php | 11 +++++------ src/Reference/PhpClassReference.php | 10 ++++++---- src/Reference/PhpMethodReference.php | 15 +++++++++++---- .../expected/blocks/references/php-class.html | 2 +- .../expected/blocks/references/php-method.html | 3 ++- tests/fixtures/expected/main/datetime.html | 2 +- .../source/blocks/references/php-class.rst | 2 +- .../source/blocks/references/php-method.rst | 2 ++ 8 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/Reference/MethodReference.php b/src/Reference/MethodReference.php index 632aaa2..b068f33 100644 --- a/src/Reference/MethodReference.php +++ b/src/Reference/MethodReference.php @@ -30,20 +30,19 @@ public function getName(): string public function resolve(Environment $environment, string $data): ResolvedReference { - $className = explode('::', $data)[0]; - $className = str_replace('\\\\', '\\', $className); - - if (!u($data)->containsAny('::')) { + $data = u($data); + if (!$data->containsAny('::')) { throw new \RuntimeException(sprintf('Malformed method reference "%s" in file "%s"', $data, $environment->getCurrentFileName())); } - $methodName = explode('::', $data)[1]; + [$className, $methodName] = $data->split('::', 2); + $className = $className->replace('\\\\', '\\'); $scrollTextFragment = sprintf('#:~:text=%s', rawurlencode('function '.$methodName)); return new ResolvedReference( $environment->getCurrentFileName(), $methodName.'()', - sprintf('%s/%s.php%s', $this->symfonyRepositoryUrl, str_replace('\\', '/', $className), $scrollTextFragment), + sprintf('%s/%s.php%s', $this->symfonyRepositoryUrl, $className->replace('\\', '/'), $scrollTextFragment), [], [ 'title' => sprintf('%s::%s()', $className, $methodName), diff --git a/src/Reference/PhpClassReference.php b/src/Reference/PhpClassReference.php index 0bb0ba8..fbaabe4 100644 --- a/src/Reference/PhpClassReference.php +++ b/src/Reference/PhpClassReference.php @@ -12,6 +12,7 @@ use Doctrine\RST\Environment; use Doctrine\RST\References\Reference; use Doctrine\RST\References\ResolvedReference; +use function Symfony\Component\String\u; class PhpClassReference extends Reference { @@ -29,14 +30,15 @@ public function getName(): string public function resolve(Environment $environment, string $data): ResolvedReference { - $classnamePath = str_replace('\\', '-', strtolower($data)); + $className = u($data)->replace('\\\\', '\\'); + return new ResolvedReference( $environment->getCurrentFileName(), - $data, - sprintf('%s/class.%s.php', $this->phpDocUrl, $classnamePath), + $className->afterLast('\\'), + sprintf('%s/class.%s.php', $this->phpDocUrl, $className->replace('\\', '-')->lower()), [], [ - 'title' => $data, + 'title' => $className, ] ); } diff --git a/src/Reference/PhpMethodReference.php b/src/Reference/PhpMethodReference.php index ac53744..9a85a91 100644 --- a/src/Reference/PhpMethodReference.php +++ b/src/Reference/PhpMethodReference.php @@ -12,6 +12,7 @@ use Doctrine\RST\Environment; use Doctrine\RST\References\Reference; use Doctrine\RST\References\ResolvedReference; +use function Symfony\Component\String\u; class PhpMethodReference extends Reference { @@ -29,15 +30,21 @@ public function getName(): string public function resolve(Environment $environment, string $data): ResolvedReference { - [$class, $method] = explode('::', $data, 2); + $data = u($data); + if (!$data->containsAny('::')) { + throw new \RuntimeException(sprintf('Malformed method reference "%s" in file "%s"', $data, $environment->getCurrentFileName())); + } + + [$className, $methodName] = $data->split('::', 2); + $className = $className->replace('\\\\', '\\'); return new ResolvedReference( $environment->getCurrentFileName(), - $data.'()', - sprintf('%s/%s.%s.php', $this->phpDocUrl, strtolower($class), strtolower($method)), + $methodName.'()', + sprintf('%s/%s.%s.php', $this->phpDocUrl, $className->replace('\\', '-')->lower(), $methodName->lower()), [], [ - 'title' => $class, + 'title' => sprintf('%s::%s()', $className, $methodName), ] ); } diff --git a/tests/fixtures/expected/blocks/references/php-class.html b/tests/fixtures/expected/blocks/references/php-class.html index b1e018e..a91b1fd 100644 --- a/tests/fixtures/expected/blocks/references/php-class.html +++ b/tests/fixtures/expected/blocks/references/php-class.html @@ -1,2 +1,2 @@

ArrayAccess

-

BcMath\Number

+

Number

diff --git a/tests/fixtures/expected/blocks/references/php-method.html b/tests/fixtures/expected/blocks/references/php-method.html index e86c571..59323ad 100644 --- a/tests/fixtures/expected/blocks/references/php-method.html +++ b/tests/fixtures/expected/blocks/references/php-method.html @@ -1 +1,2 @@ -

Locale::getDefault()

+

getDefault()

+

add()

diff --git a/tests/fixtures/expected/main/datetime.html b/tests/fixtures/expected/main/datetime.html index f36ddc3..d6892fa 100644 --- a/tests/fixtures/expected/main/datetime.html +++ b/tests/fixtures/expected/main/datetime.html @@ -79,7 +79,7 @@

methods: doRequest(). Or a namespace: Constraints. Or a PHP function: parse_ini_file. -Or a PHP method! Locale::getDefault().

+Or a PHP method! getDefault().

diff --git a/tests/fixtures/source/blocks/references/php-class.rst b/tests/fixtures/source/blocks/references/php-class.rst index f0782b4..284a901 100644 --- a/tests/fixtures/source/blocks/references/php-class.rst +++ b/tests/fixtures/source/blocks/references/php-class.rst @@ -1,4 +1,4 @@ :phpclass:`ArrayAccess` -:phpclass:`BcMath\Number` +:phpclass:`BcMath\\Number` diff --git a/tests/fixtures/source/blocks/references/php-method.rst b/tests/fixtures/source/blocks/references/php-method.rst index 11705cd..26d782d 100644 --- a/tests/fixtures/source/blocks/references/php-method.rst +++ b/tests/fixtures/source/blocks/references/php-method.rst @@ -1,2 +1,4 @@ :phpmethod:`Locale::getDefault` + +:phpmethod:`BcMath\\Number::add`