diff --git a/lib/Parser/DocumentParser.php b/lib/Parser/DocumentParser.php index 05eff2c5..015544bc 100644 --- a/lib/Parser/DocumentParser.php +++ b/lib/Parser/DocumentParser.php @@ -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; } @@ -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); diff --git a/lib/Parser/LineChecker.php b/lib/Parser/LineChecker.php index f825f869..62dbb184 100644 --- a/lib/Parser/LineChecker.php +++ b/lib/Parser/LineChecker.php @@ -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 diff --git a/tests/Functional/tests/render/comment-empty/comment-empty.html b/tests/Functional/tests/render/comment-empty/comment-empty.html new file mode 100644 index 00000000..9ba15270 --- /dev/null +++ b/tests/Functional/tests/render/comment-empty/comment-empty.html @@ -0,0 +1,4 @@ +
this is not a comment
++diff --git a/tests/Functional/tests/render/comment-empty/comment-empty.rst b/tests/Functional/tests/render/comment-empty/comment-empty.rst new file mode 100644 index 00000000..a712e419 --- /dev/null +++ b/tests/Functional/tests/render/comment-empty/comment-empty.rst @@ -0,0 +1,6 @@ +.. +this is not a comment + +.. + + this is a blockquote diff --git a/tests/Functional/tests/render/comments-block-with-header/comments-block-with-header.html b/tests/Functional/tests/render/comments-block-with-header/comments-block-with-header.html new file mode 100644 index 00000000..92b8ea63 --- /dev/null +++ b/tests/Functional/tests/render/comments-block-with-header/comments-block-with-header.html @@ -0,0 +1 @@ +this is a blockquote
+
this is not a comment
diff --git a/tests/Functional/tests/render/comments-block-with-header/comments-block-with-header.rst b/tests/Functional/tests/render/comments-block-with-header/comments-block-with-header.rst new file mode 100644 index 00000000..2736751f --- /dev/null +++ b/tests/Functional/tests/render/comments-block-with-header/comments-block-with-header.rst @@ -0,0 +1,3 @@ +.. header comment + this is a comment +this is not a comment diff --git a/tests/Functional/tests/render/comments-block/comments-block.html b/tests/Functional/tests/render/comments-block/comments-block.html new file mode 100644 index 00000000..92b8ea63 --- /dev/null +++ b/tests/Functional/tests/render/comments-block/comments-block.html @@ -0,0 +1 @@ +this is not a comment
diff --git a/tests/Functional/tests/render/comments-block/comments-block.rst b/tests/Functional/tests/render/comments-block/comments-block.rst new file mode 100644 index 00000000..243ffebd --- /dev/null +++ b/tests/Functional/tests/render/comments-block/comments-block.rst @@ -0,0 +1,3 @@ +.. + this is a comment +this is not a comment diff --git a/tests/Parser/LineCheckerTest.php b/tests/Parser/LineCheckerTest.php index 64160002..690fbe6d 100644 --- a/tests/Parser/LineCheckerTest.php +++ b/tests/Parser/LineCheckerTest.php @@ -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