Skip to content

Commit 3a54583

Browse files
authored
Merge pull request #823 from phpDocumentor/task/code-block-warning
[TASK] Warn when code-block has no content
2 parents 8b945c9 + b290204 commit 3a54583

File tree

6 files changed

+44
-18
lines changed

6 files changed

+44
-18
lines changed

packages/guides-restructured-text/src/RestructuredText/Directives/CodeBlockDirective.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public function process(
6262
BlockContext $blockContext,
6363
Directive $directive,
6464
): Node|null {
65+
if ($blockContext->getDocumentIterator()->isEmpty()) {
66+
$this->logger->warning('The code-block has no content. Did you properly indent the code? ', $blockContext->getLoggerInformation());
67+
68+
return null;
69+
}
70+
6571
$node = new CodeNode(
6672
$blockContext->getDocumentIterator()->toArray(),
6773
);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OutOfBoundsException;
1818

1919
use function chr;
20+
use function count;
2021
use function explode;
2122
use function max;
2223
use function mb_strpos;
@@ -133,6 +134,11 @@ public function toArray(): array
133134
return $this->lines;
134135
}
135136

137+
public function isEmpty(): bool
138+
{
139+
return count($this->lines) === 0 || (count($this->lines) === 1 && trim($this->lines[0]) === '');
140+
}
141+
136142
/** @psalm-assert-if-false non-empty-string $line */
137143
public static function isEmptyLine(string|null $line): bool
138144
{
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- content start -->
2+
<div class="section" id="title">
3+
<h1>Title</h1>
4+
5+
<p>Some paragraph.</p>
6+
</div>
7+
8+
<!-- content end -->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app.WARNING: The code-block has no content. Did you properly indent the code?
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Title
2+
=====
3+
4+
.. code-block:: xml
5+
6+
Some paragraph.
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
<!-- content start -->
22
<div class="section" id="directive-tests">
3-
<h1>Directive tests</h1>
3+
<h1>Directive tests</h1>
44

5-
<div class="configuration-block">
6-
<div role="tablist" aria-label="Configuration formats" class="configuration-tabs configuration-tabs-length-2">
7-
<button role="tab" type="button" data-language="yaml"
8-
aria-controls="configuration-block-tabpanel-523eafc9524d67db5f21ecae2362d532" aria-selected="true"
9-
data-active="true" >
10-
<span>Yaml</span>
11-
</button>
12-
<button role="tab" type="button" data-language="custom"
13-
aria-controls="configuration-block-tabpanel-a51ba27b3c76153f629592baf23834dc" aria-selected="false"
14-
tabindex="-1">
15-
<span>CUSTOM</span>
16-
</button>
5+
<div class="configuration-block">
6+
<div role="tablist" aria-label="Configuration formats" class="configuration-tabs configuration-tabs-length-2">
7+
<button role="tab" type="button" data-language="yaml"
8+
aria-controls="configuration-block-tabpanel-523eafc9524d67db5f21ecae2362d532" aria-selected="true"
9+
data-active="true" >
10+
<span>Yaml</span>
11+
</button>
12+
<button role="tab" type="button" data-language="custom"
13+
aria-controls="configuration-block-tabpanel-a51ba27b3c76153f629592baf23834dc" aria-selected="false"
14+
tabindex="-1">
15+
<span>CUSTOM</span>
16+
</button>
1717
</div>
1818

1919
<div role="tabpanel" id="configuration-block-tabpanel-523eafc9524d67db5f21ecae2362d532" aria-label="Yaml" class="configuration-codeblock" data-language="yaml" style="">
20-
<pre><code class="language-yaml"># app/config/services.yml</code></pre>
21-
</div>
20+
<pre><code class="language-yaml"># app/config/services.yml</code></pre>
21+
</div>
2222
<div role="tabpanel" id="configuration-block-tabpanel-a51ba27b3c76153f629592baf23834dc" aria-label="CUSTOM" class="configuration-codeblock" data-language="custom" style="display: none">
23-
<pre><code class="language-php">// config/routes.php</code></pre>
24-
</div>
23+
<pre><code class="language-php">// config/routes.php</code></pre>
2524
</div>
26-
2725
</div>
2826

27+
</div>
2928

3029
<!-- content end -->

0 commit comments

Comments
 (0)