Skip to content

Commit

Permalink
Merge pull request #220 from nikophil/issue/comments-block
Browse files Browse the repository at this point in the history
Allow block of comments
  • Loading branch information
greg0ire authored Dec 29, 2022
2 parents 6d199bd + a4e1f90 commit 9142491
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/Parser/DocumentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ private function parseLine(string $line): bool
return false;
}

if ($this->lineChecker->isComment($line)) {
$this->flush();
$this->setState(State::COMMENT);

return false;
}

if ($this->parseLink($line)) {
return true;
}
Expand Down Expand Up @@ -426,8 +433,6 @@ private function parseLine(string $line): bool
break;

case State::COMMENT:
$isComment = false;

if (! $this->lineChecker->isComment($line) && (trim($line) === '' || $line[0] !== ' ')) {
$this->setState(State::BEGIN);

Expand Down
4 changes: 2 additions & 2 deletions lib/Parser/LineChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ public function isListLine(string $line, ?string &$listMarker = null, ?int &$lis
*/
public function isBlockLine(string $line, int $minIndent = 1): bool
{
return trim($line) === '' || $this->isIndented($line, $minIndent);
return (trim($line) === '' || $this->isIndented($line, $minIndent)) && ! $this->isComment($line);
}

public function isComment(string $line): bool
{
return preg_match('/^\.\. (.*)$/mUsi', $line) > 0;
return preg_match('/^\.\.(?: [^_]((?:(?!::).)*))?$/mUsi', $line) > 0;
}

public function isDirective(string $line): bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<p>this is not a comment</p>
<blockquote>
<p>this is a blockquote</p>
</blockquote>
6 changes: 6 additions & 0 deletions tests/Functional/tests/render/comment-empty/comment-empty.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
..
this is not a comment

..
this is a blockquote
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>this is not a comment</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. header comment
this is a comment
this is not a comment
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>this is not a comment</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
..
this is a comment
this is not a comment
8 changes: 8 additions & 0 deletions tests/Parser/LineCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ public function testIsBlockLine(): void
public function testIsComment(): void
{
self::assertTrue($this->lineChecker->isComment('.. Test'));
self::assertTrue($this->lineChecker->isComment('..'));
self::assertTrue($this->lineChecker->isComment('.. with _ underscore'));
self::assertTrue($this->lineChecker->isComment('.. with : colon'));
self::assertTrue($this->lineChecker->isComment('.. can finish with colon:'));

self::assertFalse($this->lineChecker->isComment('Test'));
self::assertFalse($this->lineChecker->isComment('.. _should not start with underscore'));
self::assertFalse($this->lineChecker->isComment('.. should not finish with double colon::'));
self::assertFalse($this->lineChecker->isComment('.. should contain::double colon'));
}

public function testIsDirective(): void
Expand Down

0 comments on commit 9142491

Please sign in to comment.