From 7b9f82ceb1d7aec54bd7a8b44450a5f9555b9c8f Mon Sep 17 00:00:00 2001 From: "lina.wolf" Date: Sat, 17 Feb 2024 17:47:26 +0100 Subject: [PATCH] [TASK] Allow to omit whitespace before reference target While reST expects a whitespace here, Sphinx allows to omit the whitespace and still interprets them correctly. --- .../References/EmbeddedReferenceParser.php | 16 +++++++++++----- .../reference-without-space/expected/index.html | 9 +++++++++ .../reference-without-space/input/index.rst | 6 ++++++ 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 tests/Integration/tests/references/reference-without-space/expected/index.html create mode 100644 tests/Integration/tests/references/reference-without-space/input/index.rst diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/References/EmbeddedReferenceParser.php b/packages/guides-restructured-text/src/RestructuredText/Parser/References/EmbeddedReferenceParser.php index b57631c21..bc6b294d6 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Parser/References/EmbeddedReferenceParser.php +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/References/EmbeddedReferenceParser.php @@ -14,19 +14,25 @@ namespace phpDocumentor\Guides\RestructuredText\Parser\References; use function preg_match; +use function trim; trait EmbeddedReferenceParser { + /** + * https://regex101.com/r/KadqKx/1 + */ + private string $referenceRegex = '/^(.*?)(<([^<]+)>)?$/s'; + private function extractEmbeddedReference(string $text): ReferenceData { - preg_match('/^(.*?)(?:(?:\s|^)<([^<]+)>)?$/s', $text, $matches); + preg_match($this->referenceRegex, $text, $matches); - $text = $matches[1] === '' ? null : $matches[1]; - $reference = $matches[1]; + $text = $matches[1] === '' ? null : trim($matches[1]); + $reference = trim($matches[1]); - if (isset($matches[2])) { + if (isset($matches[3])) { // there is an embedded URI, text and URI are different - $reference = $matches[2]; + $reference = $matches[3]; } else { $text = null; } diff --git a/tests/Integration/tests/references/reference-without-space/expected/index.html b/tests/Integration/tests/references/reference-without-space/expected/index.html new file mode 100644 index 000000000..436ab108b --- /dev/null +++ b/tests/Integration/tests/references/reference-without-space/expected/index.html @@ -0,0 +1,9 @@ + +
+ +

the <head>

+ +

See also This is the anchor.

+
+ + diff --git a/tests/Integration/tests/references/reference-without-space/input/index.rst b/tests/Integration/tests/references/reference-without-space/input/index.rst new file mode 100644 index 000000000..404e830b2 --- /dev/null +++ b/tests/Integration/tests/references/reference-without-space/input/index.rst @@ -0,0 +1,6 @@ +.. _anchor: + +the \ +============ + +See also :ref:`This is the anchor`.