Skip to content

Commit

Permalink
Merge pull request #823 from phpDocumentor/task/code-block-warning
Browse files Browse the repository at this point in the history
[TASK] Warn when code-block has no content
  • Loading branch information
jaapio authored Jan 21, 2024
2 parents 8b945c9 + b290204 commit 3a54583
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OutOfBoundsException;

use function chr;
use function count;
use function explode;
use function max;
use function mb_strpos;
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- content start -->
<div class="section" id="title">
<h1>Title</h1>

<p>Some paragraph.</p>
</div>

<!-- content end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app.WARNING: The code-block has no content. Did you properly indent the code?
6 changes: 6 additions & 0 deletions tests/Integration/tests/code/code-block-empty/input/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Title
=====

.. code-block:: xml
Some paragraph.
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
<!-- content start -->
<div class="section" id="directive-tests">
<h1>Directive tests</h1>
<h1>Directive tests</h1>

<div class="configuration-block">
<div role="tablist" aria-label="Configuration formats" class="configuration-tabs configuration-tabs-length-2">
<button role="tab" type="button" data-language="yaml"
aria-controls="configuration-block-tabpanel-523eafc9524d67db5f21ecae2362d532" aria-selected="true"
data-active="true" >
<span>Yaml</span>
</button>
<button role="tab" type="button" data-language="custom"
aria-controls="configuration-block-tabpanel-a51ba27b3c76153f629592baf23834dc" aria-selected="false"
tabindex="-1">
<span>CUSTOM</span>
</button>
<div class="configuration-block">
<div role="tablist" aria-label="Configuration formats" class="configuration-tabs configuration-tabs-length-2">
<button role="tab" type="button" data-language="yaml"
aria-controls="configuration-block-tabpanel-523eafc9524d67db5f21ecae2362d532" aria-selected="true"
data-active="true" >
<span>Yaml</span>
</button>
<button role="tab" type="button" data-language="custom"
aria-controls="configuration-block-tabpanel-a51ba27b3c76153f629592baf23834dc" aria-selected="false"
tabindex="-1">
<span>CUSTOM</span>
</button>
</div>

<div role="tabpanel" id="configuration-block-tabpanel-523eafc9524d67db5f21ecae2362d532" aria-label="Yaml" class="configuration-codeblock" data-language="yaml" style="">
<pre><code class="language-yaml"># app/config/services.yml</code></pre>
</div>
<pre><code class="language-yaml"># app/config/services.yml</code></pre>
</div>
<div role="tabpanel" id="configuration-block-tabpanel-a51ba27b3c76153f629592baf23834dc" aria-label="CUSTOM" class="configuration-codeblock" data-language="custom" style="display: none">
<pre><code class="language-php">// config/routes.php</code></pre>
</div>
<pre><code class="language-php">// config/routes.php</code></pre>
</div>

</div>

</div>

<!-- content end -->

0 comments on commit 3a54583

Please sign in to comment.