Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] [BUGFIX] Improve comment handling #909

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use function in_array;
use function mb_strlen;
use function preg_match;
use function trim;

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

return $letter;
}

public static function isDirective(string $line): bool
{
return preg_match('/^\.\.\s+(\|(.+)\| |)([^\s]+)::( (.*)|)$/mUsi', $line) > 0;
}

public static function isLink(string $line): bool
{
return preg_match('/^\.\.\s+_(.+):.*$/mUsi', trim($line)) > 0;
}

public static function isAnnotation(string $line): bool
{
return preg_match('/^\.\.\s+\[([#a-zA-Z0-9]*)\]\s(.*)$$/mUsi', $line) > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use phpDocumentor\Guides\RestructuredText\Parser\AnnotationUtility;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;
use phpDocumentor\Guides\RestructuredText\Parser\LineChecker;
use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator;

use function preg_match;
Expand All @@ -42,12 +43,7 @@ public function __construct(

public function applies(BlockContext $blockContext): bool
{
return $this->isAnnotation($blockContext->getDocumentIterator()->current());
}

private function isAnnotation(string $line): bool
{
return preg_match('/^\.\.\s+\[([#a-zA-Z0-9]*)\]\s(.*)$$/mUsi', $line) > 0;
return LineChecker::isAnnotation($blockContext->getDocumentIterator()->current());
}

public function apply(BlockContext $blockContext, CompoundNode|null $on = null): Node|null
Expand All @@ -66,7 +62,7 @@ public function apply(BlockContext $blockContext, CompoundNode|null $on = null):
&& LinesIterator::isEmptyLine($documentIterator->getNextLine()) === false
) {
$documentIterator->next();
if ($this->isAnnotation($documentIterator->current())) {
if (LineChecker::isAnnotation($documentIterator->current())) {
$nodes[] = $this->createAnnotationNode($annotationKey, $buffer, $blockContext, $documentIterator, $name);
$openingLine = $documentIterator->current();
[$annotationKey, $content] = $this->analyzeOpeningLine($openingLine);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;
use phpDocumentor\Guides\RestructuredText\Parser\LineChecker;

use function preg_match;
use function str_starts_with;
use function trim;

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

/**
* Every explicit markup block which is not a valid markup construct is regarded as a comment.
*/
private function isComment(string $line): bool
{
return trim($line) === '..' || preg_match('/^\.\.\s+[^:]*$/mUsi', $line) > 0;
if (trim($line) === '..') {
return true;
}

if (!str_starts_with($line, '.. ')) {
return false;
}

if (LineChecker::isDirective($line)) {
return false;
}

if (LineChecker::isLink($line)) {
return false;
}

return !LineChecker::isAnnotation($line);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use phpDocumentor\Guides\RestructuredText\Parser\DirectiveOption;
use phpDocumentor\Guides\RestructuredText\Parser\LineChecker;
use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator;
use phpDocumentor\Guides\RestructuredText\Parser\UnindentStrategy;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -69,12 +70,7 @@ private function registerDirective(DirectiveHandler $directive): void

public function applies(BlockContext $blockContext): bool
{
return $this->isDirective($blockContext->getDocumentIterator()->current());
}

private function isDirective(string $line): bool
{
return preg_match('/^\.\.\s+(\|(.+)\| |)([^\s]+)::( (.*)|)$/mUsi', $line) > 0;
return LineChecker::isDirective($blockContext->getDocumentIterator()->current());
}

public function apply(BlockContext $blockContext, CompoundNode|null $on = null): Node|null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!-- content start -->
<div class="section" id="some-title">
<a id="nocomment"></a>
<h1>Some Title</h1>

<div class="admonition note">
<p>No comment!</p>
</div>

<div class="section" id="another-title">
<a id="alsonoomment"></a>
<h2>Another title</h2>

<div class="footnote " id="footnote-1">
<div class="footnote-label">[1]</div>
<div class="footnote-content">Text of the first footnote.</div>
</div>

<div class="footnote " id="footnote-2">
<div class="footnote-label">[2]</div>
<div class="footnote-content">Text of the second footnote.</div>
</div>


</div>

</div>

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

Some Title
==========

.. Generated by https://example.com/
.. note:: No comment!

.. this is a commend
.. _alsonoomment:

Another title
-------------

.. this is a commend
.. [#f1] Text of the first footnote.
.. [#f2] Text of the second footnote.
Loading