Skip to content

Commit 14300dc

Browse files
authored
Merge pull request #69 from javiereguiluz/tweak_codeblocks
Tweak the code blocks
2 parents c7d27dc + 28b3b55 commit 14300dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+997
-1255
lines changed

src/Renderers/CodeNodeRenderer.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,40 @@ 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+
$lines = implode("\n", range(1, $numOfLines - 1));
79+
8280
return $this->templateRenderer->render(
8381
'code.html.twig',
8482
[
85-
'language' => $language,
86-
'languageMapping' => self::LANGUAGES_MAPPING[$language] ?? $language,
87-
'code' => $code,
88-
'lineNumbers' => rtrim($lineNumbers),
83+
'languages' => $languages,
84+
'lines' => $lines,
85+
'code' => $highlightedCode,
8986
// this is the number of digits of the codeblock lines-of-code
9087
// e.g. LOC = 5, digits = 1; LOC = 18, digits = 2
9188
// this is useful to tweak the code listings according to their length
92-
'numLocDigits' => strlen((string) \count($lines)),
89+
'numLocDigits' => strlen((string) $numOfLines),
9390
]
9491
);
9592
}
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 }}</pre>
4+
<pre class="codeblock-code"><code>{{ code|raw }}</code></pre>
175
</div>
186
</div>
Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<meta charset="utf-8" />
5-
</head>
6-
<body>
7-
<div translate="no" class="notranslate literal-block loc-1">
8-
<div class="highlight-bash">
9-
<table class="highlighttable">
10-
<tr>
11-
<td class="linenos">
12-
<div class="linenodiv">
13-
<pre> 1</pre>
14-
</div>
15-
</td>
16-
<td class="code">
17-
<div class="highlight">
18-
<pre class="hljs bash">git <span class="hljs-built_in">clone</span> git@github.com:symfony/symfony.git</pre>
19-
</div>
20-
</td>
21-
</tr>
22-
</table>
3+
<head>
4+
<meta charset="utf-8" />
5+
6+
7+
</head>
8+
9+
<body>
10+
<div translate="no" class="notranslate codeblock codeblock-loc-1 codeblock-bash">
11+
<div class="codeblock-scroll">
12+
<pre class="codeblock-lines">1</pre>
13+
<pre class="codeblock-code"><code>git <span class="hljs-built_in">clone</span> git@github.com:symfony/symfony.git
14+
</code></pre>
2315
</div>
2416
</div>
25-
</body>
26-
</html>
17+
18+
</body>
19+
</html>
Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<meta charset="utf-8" />
5-
</head>
6-
<body>
7-
<div translate="no" class="notranslate literal-block loc-2">
8-
<div class="highlight-html+php">
9-
<table class="highlighttable">
10-
<tr>
11-
<td class="linenos">
12-
<div class="linenodiv">
13-
<pre> 1 2 3 4 5 6 7 8 9
3+
<head>
4+
<meta charset="utf-8" />
5+
6+
7+
</head>
8+
9+
<body>
10+
<div translate="no" class="notranslate codeblock codeblock-loc-2 codeblock-html+php codeblock-html">
11+
<div class="codeblock-scroll">
12+
<pre class="codeblock-lines">1
13+
2
14+
3
15+
4
16+
5
17+
6
18+
7
19+
8
20+
9
1421
10
1522
11
1623
12</pre>
17-
</div>
18-
</td>
19-
<td class="code">
20-
<div class="highlight">
21-
<pre class="hljs html">
22-
<span class="hljs-comment">&lt;!-- views/layout.php --&gt;</span>
24+
<pre class="codeblock-code"><code><span class="hljs-comment">&lt;!-- views/layout.php --&gt;</span>
2325
<span class="hljs-meta">&lt;!doctype <span class="hljs-meta-keyword">html</span>&gt;</span>
2426
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
25-
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
26-
<span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>
27-
<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>
28-
<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
29-
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
30-
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
31-
<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>
32-
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
33-
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
34-
</pre>
35-
</div>
36-
</td>
37-
</tr>
38-
</table>
27+
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
28+
<span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>
29+
<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>
30+
<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
31+
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
32+
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
33+
<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>
34+
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
35+
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
36+
</code></pre>
3937
</div>
4038
</div>
41-
</body>
42-
</html>
39+
40+
</body>
41+
</html>
Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<meta charset="utf-8" />
5-
</head>
6-
<body>
7-
<div translate="no" class="notranslate literal-block loc-1">
8-
<div class="highlight-html+twig">
9-
<table class="highlighttable">
10-
<tr>
11-
<td class="linenos">
12-
<div class="linenodiv">
13-
<pre> 1 2</pre>
14-
</div>
15-
</td>
16-
<td class="code">
17-
<div class="highlight">
18-
<pre class="hljs twig">
19-
<span class="hljs-comment">{# some code #}</span>
20-
<span class="xml">
21-
<span class="hljs-comment">&lt;!-- some code --&gt;</span></span>
22-
</pre>
23-
</div>
24-
</td>
25-
</tr>
26-
</table>
3+
<head>
4+
<meta charset="utf-8" />
5+
6+
7+
</head>
8+
9+
<body>
10+
<div translate="no" class="notranslate codeblock codeblock-loc-1 codeblock-html+twig codeblock-twig">
11+
<div class="codeblock-scroll">
12+
<pre class="codeblock-lines">1
13+
2</pre>
14+
<pre class="codeblock-code"><code><span class="hljs-comment">{# some code #}</span><span class="xml">
15+
<span class="hljs-comment">&lt;!-- some code --&gt;</span>
16+
</span></code></pre>
2717
</div>
2818
</div>
29-
</body>
30-
</html>
19+
20+
</body>
21+
</html>
Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<meta charset="utf-8" />
5-
</head>
6-
<body>
7-
<div translate="no" class="notranslate literal-block loc-1">
8-
<div class="highlight-html">
9-
<table class="highlighttable">
10-
<tr>
11-
<td class="linenos">
12-
<div class="linenodiv">
13-
<pre> 1</pre>
14-
</div>
15-
</td>
16-
<td class="code">
17-
<div class="highlight">
18-
<pre class="hljs html"><span class="hljs-comment">&lt;!-- some code --&gt;</span></pre>
19-
</div>
20-
</td>
21-
</tr>
22-
</table>
3+
<head>
4+
<meta charset="utf-8" />
5+
6+
7+
</head>
8+
9+
<body>
10+
<div translate="no" class="notranslate codeblock codeblock-loc-1 codeblock-html">
11+
<div class="codeblock-scroll">
12+
<pre class="codeblock-lines">1</pre>
13+
<pre class="codeblock-code"><code><span class="hljs-comment">&lt;!-- some code --&gt;</span>
14+
</code></pre>
2315
</div>
2416
</div>
25-
</body>
26-
</html>
17+
18+
</body>
19+
</html>
Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<meta charset="utf-8" />
5-
</head>
6-
<body>
7-
<div translate="no" class="notranslate literal-block loc-1">
8-
<div class="highlight-ini">
9-
<table class="highlighttable">
10-
<tr>
11-
<td class="linenos">
12-
<div class="linenodiv">
13-
<pre> 1</pre>
14-
</div>
15-
</td>
16-
<td class="code">
17-
<div class="highlight">
18-
<pre class="hljs ini"><span class="hljs-attr">fetch</span> = +refs/notes/*:refs/notes/*</pre>
19-
</div>
20-
</td>
21-
</tr>
22-
</table>
3+
<head>
4+
<meta charset="utf-8" />
5+
6+
7+
</head>
8+
9+
<body>
10+
<div translate="no" class="notranslate codeblock codeblock-loc-1 codeblock-ini">
11+
<div class="codeblock-scroll">
12+
<pre class="codeblock-lines">1</pre>
13+
<pre class="codeblock-code"><code><span class="hljs-attr">fetch</span> = +refs/notes/*:refs/notes/*
14+
</code></pre>
2315
</div>
2416
</div>
25-
</body>
26-
</html>
17+
18+
</body>
19+
</html>

0 commit comments

Comments
 (0)