diff --git a/packages/guides-restructured-text/src/RestructuredText/Directives/CodeBlockDirective.php b/packages/guides-restructured-text/src/RestructuredText/Directives/CodeBlockDirective.php index f2b58a7c6..37d8e2bdc 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Directives/CodeBlockDirective.php +++ b/packages/guides-restructured-text/src/RestructuredText/Directives/CodeBlockDirective.php @@ -62,6 +62,12 @@ public function process( BlockContext $blockContext, Directive $directive, ): Node|null { + if ($blockContext->getDocumentIterator()->isEmpty()) { + $this->logger->warning('The code-block has no content. Did you properly indent the code? ', $blockContext->getLoggerInformation()); + + return null; + } + $node = new CodeNode( $blockContext->getDocumentIterator()->toArray(), ); diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/LinesIterator.php b/packages/guides-restructured-text/src/RestructuredText/Parser/LinesIterator.php index a8e390095..f6c6388a2 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Parser/LinesIterator.php +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/LinesIterator.php @@ -17,6 +17,7 @@ use OutOfBoundsException; use function chr; +use function count; use function explode; use function max; use function mb_strpos; @@ -133,6 +134,11 @@ public function toArray(): array return $this->lines; } + public function isEmpty(): bool + { + return count($this->lines) === 0 || (count($this->lines) === 1 && trim($this->lines[0]) === ''); + } + /** @psalm-assert-if-false non-empty-string $line */ public static function isEmptyLine(string|null $line): bool { diff --git a/tests/Integration/tests/code/code-block-empty/expected/index.html b/tests/Integration/tests/code/code-block-empty/expected/index.html new file mode 100644 index 000000000..7113026f5 --- /dev/null +++ b/tests/Integration/tests/code/code-block-empty/expected/index.html @@ -0,0 +1,8 @@ + +
+

Title

+ +

Some paragraph.

+
+ + diff --git a/tests/Integration/tests/code/code-block-empty/expected/logs/warning.log b/tests/Integration/tests/code/code-block-empty/expected/logs/warning.log new file mode 100644 index 000000000..f6f55ef38 --- /dev/null +++ b/tests/Integration/tests/code/code-block-empty/expected/logs/warning.log @@ -0,0 +1 @@ +app.WARNING: The code-block has no content. Did you properly indent the code? diff --git a/tests/Integration/tests/code/code-block-empty/input/index.rst b/tests/Integration/tests/code/code-block-empty/input/index.rst new file mode 100644 index 000000000..b762b8cb9 --- /dev/null +++ b/tests/Integration/tests/code/code-block-empty/input/index.rst @@ -0,0 +1,6 @@ +Title +===== + +.. code-block:: xml + +Some paragraph. diff --git a/tests/Integration/tests/directives/directive-configuration-block/expected/index.html b/tests/Integration/tests/directives/directive-configuration-block/expected/index.html index 9bb882a48..58036f796 100644 --- a/tests/Integration/tests/directives/directive-configuration-block/expected/index.html +++ b/tests/Integration/tests/directives/directive-configuration-block/expected/index.html @@ -1,30 +1,29 @@
-

Directive tests

+

Directive tests

-
-
- - +
+
+ +
-
# app/config/services.yml
-
+
# app/config/services.yml
+
+
// config/routes.php
-
+