Skip to content

Commit f0132eb

Browse files
authored
Merge pull request #909 from phpDocumentor/backport/1.x/pr-908
[1.x] [BUGFIX] Improve comment handling
2 parents af78f35 + 341c4a1 commit f0132eb

File tree

6 files changed

+90
-15
lines changed

6 files changed

+90
-15
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
use function in_array;
1717
use function mb_strlen;
18+
use function preg_match;
19+
use function trim;
1820

1921
final class LineChecker
2022
{
@@ -74,4 +76,19 @@ public static function isSpecialLine(string $line, int $minimumLength = 2): stri
7476

7577
return $letter;
7678
}
79+
80+
public static function isDirective(string $line): bool
81+
{
82+
return preg_match('/^\.\.\s+(\|(.+)\| |)([^\s]+)::( (.*)|)$/mUsi', $line) > 0;
83+
}
84+
85+
public static function isLink(string $line): bool
86+
{
87+
return preg_match('/^\.\.\s+_(.+):.*$/mUsi', trim($line)) > 0;
88+
}
89+
90+
public static function isAnnotation(string $line): bool
91+
{
92+
return preg_match('/^\.\.\s+\[([#a-zA-Z0-9]*)\]\s(.*)$$/mUsi', $line) > 0;
93+
}
7794
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use phpDocumentor\Guides\RestructuredText\Parser\AnnotationUtility;
2323
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
2424
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;
25+
use phpDocumentor\Guides\RestructuredText\Parser\LineChecker;
2526
use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator;
2627

2728
use function preg_match;
@@ -42,12 +43,7 @@ public function __construct(
4243

4344
public function applies(BlockContext $blockContext): bool
4445
{
45-
return $this->isAnnotation($blockContext->getDocumentIterator()->current());
46-
}
47-
48-
private function isAnnotation(string $line): bool
49-
{
50-
return preg_match('/^\.\.\s+\[([#a-zA-Z0-9]*)\]\s(.*)$$/mUsi', $line) > 0;
46+
return LineChecker::isAnnotation($blockContext->getDocumentIterator()->current());
5147
}
5248

5349
public function apply(BlockContext $blockContext, CompoundNode|null $on = null): Node|null
@@ -66,7 +62,7 @@ public function apply(BlockContext $blockContext, CompoundNode|null $on = null):
6662
&& LinesIterator::isEmptyLine($documentIterator->getNextLine()) === false
6763
) {
6864
$documentIterator->next();
69-
if ($this->isAnnotation($documentIterator->current())) {
65+
if (LineChecker::isAnnotation($documentIterator->current())) {
7066
$nodes[] = $this->createAnnotationNode($annotationKey, $buffer, $blockContext, $documentIterator, $name);
7167
$openingLine = $documentIterator->current();
7268
[$annotationKey, $content] = $this->analyzeOpeningLine($openingLine);

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
use phpDocumentor\Guides\Nodes\Node;
1818
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
1919
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;
20+
use phpDocumentor\Guides\RestructuredText\Parser\LineChecker;
2021

21-
use function preg_match;
22+
use function str_starts_with;
2223
use function trim;
2324

2425
/**
@@ -59,8 +60,27 @@ private function isCommentLine(string|null $line): bool
5960
return $this->isComment($line) || trim($line) === '' || $line[0] === ' ';
6061
}
6162

63+
/**
64+
* Every explicit markup block which is not a valid markup construct is regarded as a comment.
65+
*/
6266
private function isComment(string $line): bool
6367
{
64-
return trim($line) === '..' || preg_match('/^\.\.\s+[^:]*$/mUsi', $line) > 0;
68+
if (trim($line) === '..') {
69+
return true;
70+
}
71+
72+
if (!str_starts_with($line, '.. ')) {
73+
return false;
74+
}
75+
76+
if (LineChecker::isDirective($line)) {
77+
return false;
78+
}
79+
80+
if (LineChecker::isLink($line)) {
81+
return false;
82+
}
83+
84+
return !LineChecker::isAnnotation($line);
6585
}
6686
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;
2323
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
2424
use phpDocumentor\Guides\RestructuredText\Parser\DirectiveOption;
25+
use phpDocumentor\Guides\RestructuredText\Parser\LineChecker;
2526
use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator;
2627
use phpDocumentor\Guides\RestructuredText\Parser\UnindentStrategy;
2728
use Psr\Log\LoggerInterface;
@@ -69,12 +70,7 @@ private function registerDirective(DirectiveHandler $directive): void
6970

7071
public function applies(BlockContext $blockContext): bool
7172
{
72-
return $this->isDirective($blockContext->getDocumentIterator()->current());
73-
}
74-
75-
private function isDirective(string $line): bool
76-
{
77-
return preg_match('/^\.\.\s+(\|(.+)\| |)([^\s]+)::( (.*)|)$/mUsi', $line) > 0;
73+
return LineChecker::isDirective($blockContext->getDocumentIterator()->current());
7874
}
7975

8076
public function apply(BlockContext $blockContext, CompoundNode|null $on = null): Node|null
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!-- content start -->
2+
<div class="section" id="some-title">
3+
<a id="nocomment"></a>
4+
<h1>Some Title</h1>
5+
6+
<div class="admonition note">
7+
<p>No comment!</p>
8+
</div>
9+
10+
<div class="section" id="another-title">
11+
<a id="alsonoomment"></a>
12+
<h2>Another title</h2>
13+
14+
<div class="footnote " id="footnote-1">
15+
<div class="footnote-label">[1]</div>
16+
<div class="footnote-content">Text of the first footnote.</div>
17+
</div>
18+
19+
<div class="footnote " id="footnote-2">
20+
<div class="footnote-label">[2]</div>
21+
<div class="footnote-content">Text of the second footnote.</div>
22+
</div>
23+
24+
25+
</div>
26+
27+
</div>
28+
29+
<!-- content end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. _nocomment:
2+
3+
Some Title
4+
==========
5+
6+
.. Generated by https://example.com/
7+
.. note:: No comment!
8+
9+
.. this is a commend
10+
.. _alsonoomment:
11+
12+
Another title
13+
-------------
14+
15+
.. this is a commend
16+
.. [#f1] Text of the first footnote.
17+
.. [#f2] Text of the second footnote.

0 commit comments

Comments
 (0)