Skip to content

Commit

Permalink
[TASK] Allow to omit whitespace before reference target
Browse files Browse the repository at this point in the history
While reST expects a whitespace here, Sphinx allows to omit the whitespace and still interprets them correctly.
  • Loading branch information
linawolf committed Feb 18, 2024
1 parent c50c94a commit 7b9f82c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- content start -->
<div class="section" id="the-head">
<a id="anchor"></a>
<h1>the &lt;head&gt;</h1>

<p>See also <a href="/index.html#anchor">This is the anchor</a>.</p>
</div>

<!-- content end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _anchor:

the \<head\>
============

See also :ref:`This is the anchor<anchor>`.

0 comments on commit 7b9f82c

Please sign in to comment.