Skip to content

Commit 3b447af

Browse files
lacatoirejaapio
authored andcommitted
Move isExplicitMarkup helper to LineChecker and broaden test coverage
Hosts the helper next to its siblings (isLink, isDirective, isAnnotation), aligns its regex flags with the rest of the file, and exercises sub-section underlines, phrase-reference anchors and stacked anchors.
1 parent 291c226 commit 3b447af

5 files changed

Lines changed: 40 additions & 14 deletions

File tree

packages/guides-restructured-text/src/RestructuredText/Parser/LineChecker.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,15 @@ public static function isAnnotation(string $line): bool
9191
{
9292
return preg_match('/^\.\.\s+\[([#a-zA-Z0-9]*)\]\s(.*)$$/mUsi', $line) > 0;
9393
}
94+
95+
/**
96+
* RST explicit markup blocks (anchors, comments, directives, ...) start with two
97+
* dots followed by whitespace, or are a lonely `..`.
98+
*
99+
* @link https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#explicit-markup-blocks
100+
*/
101+
public static function isExplicitMarkup(string $line): bool
102+
{
103+
return preg_match('/^\.\.(\s.*|)$/mUsi', $line) > 0;
104+
}
94105
}

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/TitleRule.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
use function mb_strlen;
2626
use function min;
27-
use function preg_match;
2827
use function trim;
2928

3029
/**
@@ -45,7 +44,7 @@ public function applies(BlockContext $blockContext): bool
4544
$line = $blockContext->getDocumentIterator()->current();
4645
$nextLine = $blockContext->getDocumentIterator()->getNextLine();
4746

48-
if ($this->isExplicitMarkup($line)) {
47+
if (LineChecker::isExplicitMarkup($line)) {
4948
return false;
5049
}
5150

@@ -113,16 +112,4 @@ private function nextLineIsAnUnderline(string $line, string|null $nextLine): str
113112

114113
return $letter ?? '';
115114
}
116-
117-
/**
118-
* RST explicit markup blocks (anchors, comments, directives, ...) start with two
119-
* dots followed by whitespace, or are a lonely `..`. Such a line must never be
120-
* treated as a section title, even if the next line happens to look like an underline.
121-
*
122-
* @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#explicit-markup-blocks
123-
*/
124-
private function isExplicitMarkup(string $line): bool
125-
{
126-
return preg_match('/^\.\.(?:\s|$)/', $line) === 1;
127-
}
128115
}

packages/guides-restructured-text/tests/unit/Parser/Productions/TitleRuleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public static function provideExplicitMarkupLines(): Generator
5959
yield 'anchor with single space' => [".. _foo:\n========\n"];
6060
yield 'anchor with double space' => [".. _foo:\n========\n"];
6161
yield 'anchor with tab' => [".. \t_foo:\n========\n"];
62+
yield 'anchor above level-2 underline' => [".. _foo:\n--------\n"];
63+
yield 'phrase reference anchor' => [".. _`Foo Bar`:\n==============\n"];
6264
yield 'directive' => [".. note::\n=========\n"];
6365
yield 'comment' => [".. some comment\n===============\n"];
6466
yield 'lonely double dot' => ["..\n==\n"];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!-- content start -->
2+
<div class="section" id="release-notes-1">
3+
<a id="changelog"></a>
4+
<a id="release-notes"></a>
5+
<h1>Release Notes</h1>
6+
7+
<p>Some content</p>
8+
9+
10+
<p>This points to release notes: <a href="/index.html#release-notes">Release Notes</a></p>
11+
12+
13+
<p>This points to changelog: <a href="/index.html#changelog">Release Notes</a></p>
14+
15+
</div>
16+
<!-- content end -->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. _release-notes:
2+
.. _changelog:
3+
Release Notes
4+
=============
5+
6+
Some content
7+
8+
This points to release notes: :ref:`release-notes`
9+
10+
This points to changelog: :ref:`changelog`

0 commit comments

Comments
 (0)