Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/guides-code/src/Code/Twig/CodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ public function getFilters(): array
];
}

/** @param array<string, mixed> $context */
public function highlight(array $context, string $code, string $language = 'text'): string
/**
* `CodeNode::getLanguage()` is nullable, and templates pass it straight into this filter, so null has to be
* accepted here and mean the same as an omitted language.
*
* @param array<string, mixed> $context
*/
public function highlight(array $context, string $code, string|null $language = null): string
{
$debugInformation = $context['debugInformation'] ?? [];
if (!is_array($debugInformation)) {
$debugInformation = [];
}

return ($this->highlighter)($language, $code, $debugInformation)->code;
return ($this->highlighter)($language ?? 'text', $code, $debugInformation)->code;
}
}
57 changes: 57 additions & 0 deletions packages/guides-code/tests/unit/Twig/CodeExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Guides\Code\Twig;

use Highlight\Highlighter as HighlightPHP;
use phpDocumentor\Guides\Code\Highlighter\HighlightPhpHighlighter;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;

final class CodeExtensionTest extends TestCase
{
private CodeExtension $extension;

protected function setUp(): void
{
$this->extension = new CodeExtension(new HighlightPhpHighlighter(new HighlightPHP(), new NullLogger()));
}

public function testItHighlightsCodeWithoutALanguageAsPlainText(): void
{
// A highlighter treats this differently per language: as "text" the quoted part stays plain, in a
// programming language it becomes a string token. Plain input would be returned unchanged by every
// language and could therefore not tell the fallback apart from any other one.
$code = '<a> & "b"';

self::assertSame(
$this->extension->highlight([], $code, 'text'),
$this->extension->highlight([], $code, null),
'A CodeNode without a language must be rendered like an explicit "text" language',
);

self::assertNotSame(
$this->extension->highlight([], $code, 'php'),
$this->extension->highlight([], $code, null),
'The fixture must be able to tell the "text" fallback apart from another language',
);
}

public function testItHighlightsCodeWithALanguage(): void
{
self::assertStringContainsString(
'hljs-keyword',
$this->extension->highlight([], '<?php return 1;', 'php'),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<span class="caption-text">{{ renderNode(node.caption) }}</span>
</div>
{%- endif -%}
<pre{% if node.classes %} class="{{ node.classesString }}"{% endif %}><code class="language-{{ node.language }}{{ node.startingLineNumber ? ' line-numbers' }}"
<pre{% if node.classes %} class="{{ node.classesString }}"{% endif %}><code class="language-{{ node.language ?? 'text' }}{{ node.startingLineNumber ? ' line-numbers' }}"
{%- if node.startingLineNumber %} data-start="{{ node.startingLineNumber }}"{% endif -%}
{%- if node.emphasizeLines %} data-emphasize-lines="{{ node.emphasizeLines }}"{% endif -%}>
{%- include "body/code/highlighted-code.html.twig" -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- content start -->
<div class="section" id="index">
<h1>index</h1>
<pre><code class="language-text">&lt;?php

declare(strict_types=1);

class Example
{
public function test(): string
{
return 'this is a test';
}
}
</code></pre>

</div>
<!-- content end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

class Example
{
public function test(): string
{
return 'this is a test';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd">
<!-- The highlighting filter of this extension is where a CodeNode without a language used to abort the run -->
<extension class="phpDocumentor\Guides\Code" />
</guides>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
index
=====

.. literalinclude:: _code/_Example.php
4 changes: 2 additions & 2 deletions tests/Integration/tests/markdown/code-md/expected/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

<div class="section" id="markdown-with-code-blocks">
<h1>Markdown with Code Blocks</h1>
<pre><code class="language-">&lt;html&gt;
<pre><code class="language-text">&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;/html&gt;
</code></pre>

<div class="section" id="fenced-code-blocks">
<h2>Fenced Code Blocks</h2>
<pre><code class="language-">{
<pre><code class="language-text">{
&quot;firstName&quot;: &quot;John&quot;,
&quot;lastName&quot;: &quot;Smith&quot;,
&quot;age&quot;: 25
Expand Down
Loading