Skip to content

Tweak the code blocks #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 17, 2021
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
39 changes: 18 additions & 21 deletions src/Renderers/CodeNodeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,40 @@ public function __construct(CodeNode $codeNode, TemplateRenderer $templateRender

public function render(): string
{
$this->configureHighlighter();

$value = $this->codeNode->getValue();

$code = $this->codeNode->getValue();
if ($this->codeNode->isRaw()) {
return $value;
}

$lines = $this->getLines($value);
$code = implode("\n", $lines);

$lineNumbers = '';
for ($i = 1; $i <= \count($lines); ++$i) {
$lineNumbers .= u((string) $i)->padStart(2, ' ')."\n";
return $code;
}

$language = $this->codeNode->getLanguage() ?? 'php';
$languageMapping = self::LANGUAGES_MAPPING[$language] ?? $language;
$languages = array_unique([$language, $languageMapping]);

if ('text' === $language) {
$highlightedCode = $code;
} else {
$this->configureHighlighter();

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

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

$numOfLines = \count(preg_split('/\r\n|\r|\n/', $highlightedCode));
$lines = implode("\n", range(1, $numOfLines - 1));

return $this->templateRenderer->render(
'code.html.twig',
[
'language' => $language,
'languageMapping' => self::LANGUAGES_MAPPING[$language] ?? $language,
'code' => $code,
'lineNumbers' => rtrim($lineNumbers),
'languages' => $languages,
'lines' => $lines,
'code' => $highlightedCode,
// this is the number of digits of the codeblock lines-of-code
// e.g. LOC = 5, digits = 1; LOC = 18, digits = 2
// this is useful to tweak the code listings according to their length
'numLocDigits' => strlen((string) \count($lines)),
'numLocDigits' => strlen((string) $numOfLines),
]
);
}
Expand Down
20 changes: 4 additions & 16 deletions src/Templates/default/html/code.html.twig
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
<div translate="no" class="notranslate literal-block loc-{{ numLocDigits }}">
<div class="highlight-{{ language }}">
<table class="highlighttable">
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>{{ lineNumbers }}</pre>
</div>
</td>
<td class="code">
<div class="highlight">
<pre class="hljs {{ languageMapping }}">{{ code|raw }}</pre>
</div>
</td>
</tr>
</table>
<div translate="no" class="notranslate codeblock codeblock-loc-{{ numLocDigits }} {{ languages|map(language => "codeblock-#{language}")|join(' ') }}">
<div class="codeblock-scroll">
<pre class="codeblock-lines">{{ lines }}</pre>
<pre class="codeblock-code"><code>{{ code|raw }}</code></pre>
</div>
</div>
37 changes: 15 additions & 22 deletions tests/fixtures/expected/blocks/code-blocks/bash.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div translate="no" class="notranslate literal-block loc-1">
<div class="highlight-bash">
<table class="highlighttable">
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 1</pre>
</div>
</td>
<td class="code">
<div class="highlight">
<pre class="hljs bash">git <span class="hljs-built_in">clone</span> git@github.com:symfony/symfony.git</pre>
</div>
</td>
</tr>
</table>
<head>
<meta charset="utf-8" />


</head>

<body>
<div translate="no" class="notranslate codeblock codeblock-loc-1 codeblock-bash">
<div class="codeblock-scroll">
<pre class="codeblock-lines">1</pre>
<pre class="codeblock-code"><code>git <span class="hljs-built_in">clone</span> git@github.com:symfony/symfony.git
</code></pre>
</div>
</div>
</body>
</html>

</body>
</html>
65 changes: 32 additions & 33 deletions tests/fixtures/expected/blocks/code-blocks/html-php.html
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div translate="no" class="notranslate literal-block loc-2">
<div class="highlight-html+php">
<table class="highlighttable">
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 1 2 3 4 5 6 7 8 9
<head>
<meta charset="utf-8" />


</head>

<body>
<div translate="no" class="notranslate codeblock codeblock-loc-2 codeblock-html+php codeblock-html">
<div class="codeblock-scroll">
<pre class="codeblock-lines">1
2
3
4
5
6
7
8
9
10
11
12</pre>
</div>
</td>
<td class="code">
<div class="highlight">
<pre class="hljs html">
<span class="hljs-comment">&lt;!-- views/layout.php --&gt;</span>
<pre class="codeblock-code"><code><span class="hljs-comment">&lt;!-- views/layout.php --&gt;</span>
<span class="hljs-meta">&lt;!doctype <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>
<span class="php"><span class="hljs-meta">&lt;?php</span> <span class="hljs-keyword">echo</span> <span class="hljs-string">'title'</span>; <span class="hljs-meta">?&gt;</span></span>
<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
<span class="php"><span class="hljs-meta">&lt;?php</span> <span class="hljs-keyword">echo</span> <span class="hljs-string">'body'</span>; <span class="hljs-meta">?&gt;</span></span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</pre>
</div>
</td>
</tr>
</table>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>
<span class="php"><span class="hljs-meta">&lt;?php</span> <span class="hljs-keyword">echo</span> <span class="hljs-string">'title'</span>; <span class="hljs-meta">?&gt;</span></span>
<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
<span class="php"><span class="hljs-meta">&lt;?php</span> <span class="hljs-keyword">echo</span> <span class="hljs-string">'body'</span>; <span class="hljs-meta">?&gt;</span></span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
</div>
</div>
</body>
</html>

</body>
</html>
43 changes: 17 additions & 26 deletions tests/fixtures/expected/blocks/code-blocks/html-twig.html
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div translate="no" class="notranslate literal-block loc-1">
<div class="highlight-html+twig">
<table class="highlighttable">
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 1 2</pre>
</div>
</td>
<td class="code">
<div class="highlight">
<pre class="hljs twig">
<span class="hljs-comment">{# some code #}</span>
<span class="xml">
<span class="hljs-comment">&lt;!-- some code --&gt;</span></span>
</pre>
</div>
</td>
</tr>
</table>
<head>
<meta charset="utf-8" />


</head>

<body>
<div translate="no" class="notranslate codeblock codeblock-loc-1 codeblock-html+twig codeblock-twig">
<div class="codeblock-scroll">
<pre class="codeblock-lines">1
2</pre>
<pre class="codeblock-code"><code><span class="hljs-comment">{# some code #}</span><span class="xml">
<span class="hljs-comment">&lt;!-- some code --&gt;</span>
</span></code></pre>
</div>
</div>
</body>
</html>

</body>
</html>
37 changes: 15 additions & 22 deletions tests/fixtures/expected/blocks/code-blocks/html.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div translate="no" class="notranslate literal-block loc-1">
<div class="highlight-html">
<table class="highlighttable">
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 1</pre>
</div>
</td>
<td class="code">
<div class="highlight">
<pre class="hljs html"><span class="hljs-comment">&lt;!-- some code --&gt;</span></pre>
</div>
</td>
</tr>
</table>
<head>
<meta charset="utf-8" />


</head>

<body>
<div translate="no" class="notranslate codeblock codeblock-loc-1 codeblock-html">
<div class="codeblock-scroll">
<pre class="codeblock-lines">1</pre>
<pre class="codeblock-code"><code><span class="hljs-comment">&lt;!-- some code --&gt;</span>
</code></pre>
</div>
</div>
</body>
</html>

</body>
</html>
37 changes: 15 additions & 22 deletions tests/fixtures/expected/blocks/code-blocks/ini.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div translate="no" class="notranslate literal-block loc-1">
<div class="highlight-ini">
<table class="highlighttable">
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 1</pre>
</div>
</td>
<td class="code">
<div class="highlight">
<pre class="hljs ini"><span class="hljs-attr">fetch</span> = +refs/notes/*:refs/notes/*</pre>
</div>
</td>
</tr>
</table>
<head>
<meta charset="utf-8" />


</head>

<body>
<div translate="no" class="notranslate codeblock codeblock-loc-1 codeblock-ini">
<div class="codeblock-scroll">
<pre class="codeblock-lines">1</pre>
<pre class="codeblock-code"><code><span class="hljs-attr">fetch</span> = +refs/notes/*:refs/notes/*
</code></pre>
</div>
</div>
</body>
</html>

</body>
</html>
Loading