Skip to content

Commit e22c862

Browse files
committed
Tweak the code blocks
1 parent c7d27dc commit e22c862

File tree

2 files changed

+21
-37
lines changed

2 files changed

+21
-37
lines changed

src/Renderers/CodeNodeRenderer.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,39 @@ public function __construct(CodeNode $codeNode, TemplateRenderer $templateRender
5353

5454
public function render(): string
5555
{
56-
$this->configureHighlighter();
57-
58-
$value = $this->codeNode->getValue();
59-
56+
$code = $this->codeNode->getValue();
6057
if ($this->codeNode->isRaw()) {
61-
return $value;
62-
}
63-
64-
$lines = $this->getLines($value);
65-
$code = implode("\n", $lines);
66-
67-
$lineNumbers = '';
68-
for ($i = 1; $i <= \count($lines); ++$i) {
69-
$lineNumbers .= u((string) $i)->padStart(2, ' ')."\n";
58+
return $code;
7059
}
7160

7261
$language = $this->codeNode->getLanguage() ?? 'php';
62+
$languageMapping = self::LANGUAGES_MAPPING[$language] ?? $language;
63+
$languages = array_unique([$language, $languageMapping]);
64+
65+
if ('text' === $language) {
66+
$highlightedCode = $code;
67+
} else {
68+
$this->configureHighlighter();
7369

74-
if ('text' !== $language) {
7570
$highLighter = new Highlighter();
76-
$code = $highLighter->highlight(self::LANGUAGES_MAPPING[$language] ?? $language, $code)->value;
71+
$highlightedCode = $highLighter->highlight($languageMapping, $code)->value;
7772

7873
// this allows to highlight the $ in PHP variable names
79-
$code = str_replace('<span class="hljs-variable">$', '<span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>', $code);
74+
$highlightedCode = str_replace('<span class="hljs-variable">$', '<span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>', $highlightedCode);
8075
}
8176

77+
$numOfLines = \count(preg_split('/\r\n|\r|\n/', $highlightedCode));
78+
8279
return $this->templateRenderer->render(
8380
'code.html.twig',
8481
[
85-
'language' => $language,
86-
'languageMapping' => self::LANGUAGES_MAPPING[$language] ?? $language,
87-
'code' => $code,
88-
'lineNumbers' => rtrim($lineNumbers),
82+
'languages' => $languages,
83+
'lines' => range(1, $numOfLines - 1),
84+
'code' => $highlightedCode,
8985
// this is the number of digits of the codeblock lines-of-code
9086
// e.g. LOC = 5, digits = 1; LOC = 18, digits = 2
9187
// this is useful to tweak the code listings according to their length
92-
'numLocDigits' => strlen((string) \count($lines)),
88+
'numLocDigits' => strlen((string) $numOfLines),
9389
]
9490
);
9591
}
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
<div translate="no" class="notranslate literal-block loc-{{ numLocDigits }}">
2-
<div class="highlight-{{ language }}">
3-
<table class="highlighttable">
4-
<tr>
5-
<td class="linenos">
6-
<div class="linenodiv">
7-
<pre>{{ lineNumbers }}</pre>
8-
</div>
9-
</td>
10-
<td class="code">
11-
<div class="highlight">
12-
<pre class="hljs {{ languageMapping }}">{{ code|raw }}</pre>
13-
</div>
14-
</td>
15-
</tr>
16-
</table>
1+
<div translate="no" class="notranslate codeblock codeblock-loc-{{ numLocDigits }} {{ languages|map(language => "codeblock-#{language}")|join(' ') }}">
2+
<div class="codeblock-scroll">
3+
<pre class="codeblock-lines">{{ lines|join("\n") }}</pre>
4+
<pre class="codeblock-code"><code>{{ code|raw }}</code></pre>
175
</div>
186
</div>

0 commit comments

Comments
 (0)