Skip to content

Commit 857166e

Browse files
committed
minor #1235 [Site] Remove highlight.php (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Site] Remove highlight.php | Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes | Issues | Fix [#683](#683) | License | MIT Remove highlight.php and use highlight.js directly. (highlight.php is in fact abandonned) Commits ------- 572d719 [Site] Remove highlight.php
2 parents 23100d0 + 572d719 commit 857166e

File tree

9 files changed

+87
-111
lines changed

9 files changed

+87
-111
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Controller } from '@hotwired/stimulus';
2+
import hljs from 'highlight.js/lib/core';
3+
import hljs_javascript from 'highlight.js/lib/languages/javascript';
4+
import hljs_php from 'highlight.js/lib/languages/php';
5+
import hljs_xml from 'highlight.js/lib/languages/xml';
6+
import hljs_twig from 'highlight.js/lib/languages/twig';
7+
8+
hljs.registerLanguage('javascript', hljs_javascript);
9+
hljs.registerLanguage('php', hljs_php);
10+
hljs.registerLanguage('twig', hljs_twig);
11+
// xml is the language used for HTML
12+
hljs.registerLanguage('xml', hljs_xml);
13+
14+
export default class extends Controller {
15+
static targets = ['codeBlock'];
16+
17+
codeBlockTargetConnected() {
18+
this.codeBlockTargets.forEach(this.#highlightCodeBlock)
19+
}
20+
21+
#highlightCodeBlock(codeBlock) {
22+
if (codeBlock.dataset.highlighted) {
23+
return;
24+
}
25+
hljs.highlightElement(codeBlock);
26+
}
27+
}

ux.symfony.com/assets/styles/components/_Terminal.scss

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,16 @@
140140
overflow-inline: auto;
141141
}
142142
.Terminal_body pre {
143-
padding: 1rem;
143+
padding: 0;
144144
margin: 0;
145145
}
146-
147146
.Terminal_content {
148147
font-size: .9rem;
148+
overflow-x: auto;
149+
overflow-y: auto;
150+
padding: 1rem;
151+
152+
pre {
153+
overflow: visible;
154+
}
149155
}

ux.symfony.com/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"kornrunner/blurhash": "^1.2.2",
1717
"league/commonmark": "^2.4",
1818
"pagerfanta/twig": "^3.8",
19-
"scrivo/highlight.php": "^9.18.1.10",
2019
"symfony/asset": "6.4.*",
2120
"symfony/asset-mapper": "6.4.*",
2221
"symfony/console": "6.4.*",

ux.symfony.com/composer.lock

Lines changed: 1 addition & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ux.symfony.com/importmap.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@
115115
'highlight.js/lib/languages/javascript' => [
116116
'version' => '11.7.0',
117117
],
118+
'highlight.js/lib/languages/php' => [
119+
'version' => '11.7.0',
120+
],
121+
'highlight.js/lib/languages/twig' => [
122+
'version' => '11.7.0',
123+
],
124+
'highlight.js/lib/languages/xml' => [
125+
'version' => '11.7.0',
126+
],
118127
'intl-messageformat' => [
119128
'version' => '10.3.5',
120129
],

ux.symfony.com/src/Twig/CodeBlock.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use App\Util\FilenameHelper;
1515
use App\Util\SourceCleaner;
16-
use Highlight\Highlighter;
1716
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1817
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
1918

@@ -40,30 +39,28 @@ class CodeBlock
4039
public bool $stripExcessHtml = false;
4140

4241
public function __construct(
43-
private Highlighter $highlighter,
4442
#[Autowire('%kernel.project_dir%')] private string $rootDir,
4543
) {
4644
}
4745

48-
public function highlightSource(): string
46+
/**
47+
* Returns a list of source code pieces, extracted from the filename
48+
* argument, ready to be renderer in the template.
49+
*
50+
* Every piece is composed as an array {content, highlight} with
51+
* content: the raw source code (after cleaning)
52+
* highlight: whether they must be syntax-highlighted or not
53+
*
54+
* @return list<array{content: string, highlight: ?bool}>
55+
*/
56+
public function prepareSource(): array
4957
{
5058
$content = $this->getRawSource();
5159
if ('php' === $this->getLanguage()) {
5260
$content = SourceCleaner::cleanupPhpFile($content);
5361
}
5462

55-
$pieces = $this->splitAndProcessSource($content);
56-
57-
$highlighted = [];
58-
foreach ($pieces as $piece) {
59-
if ($piece['highlight']) {
60-
$highlighted[] = $this->highlighter->highlight($this->getLanguage(), $piece['content'])->value;
61-
} else {
62-
$highlighted[] = $piece['content'];
63-
}
64-
}
65-
66-
return implode('', $highlighted);
63+
return $this->splitAndProcessSource($content);
6764
}
6865

6966
public function getRawSource(): string
@@ -96,15 +93,17 @@ public function getGithubLink(): string
9693
return sprintf('https://github.com/symfony/ux/blob/2.x/ux.symfony.com/%s', $this->filename);
9794
}
9895

99-
private function getLanguage(): string
96+
public function getLanguage(): string
10097
{
10198
if (null !== $this->language) {
10299
return $this->language;
103100
}
104101

105-
$parts = explode('.', $this->filename);
102+
if ($ext = strrchr($this->filename, '.')) {
103+
return $this->language = substr($ext, 1);
104+
}
106105

107-
return array_pop($parts);
106+
throw new \RuntimeException('Unable to detect the code language');
108107
}
109108

110109
public function getElementId(): ?string
@@ -151,7 +150,8 @@ private function splitAndProcessSource(string $content): array
151150

152151
// the use statements + surrounding span
153152
$parts[] = [
154-
'content' => '<span class="hljs-comment" role="button" title="Expand use statements" data-action="click->code-expander#expandUseStatements">// ... use statements hidden - click to show </span>',
153+
'content' => '<span class="hljs-comment" role="button" title="Expand use statements" data-action="click->code-expander#expandUseStatements">
154+
<pre><code class="nohighlight">// ... use statements hidden - click to show</code></pre></span>',
155155
'highlight' => false,
156156
];
157157
$parts[] = [

ux.symfony.com/src/Util/SourceCleaner.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,25 @@ class SourceCleaner
1717
{
1818
public static function cleanupPhpFile(string $contents, bool $removeClass = false): string
1919
{
20+
// Remove <?php
2021
$contents = u($contents)->replace("<?php\n", '');
2122

2223
// Remove LICENCE header
2324
if ($contents->indexOf('* This file is part of the Symfony package')) {
24-
$contents = $contents->after(' */');
25+
$contents = $contents->before('/*')->trim()->append($contents->after(' */')->trim());
2526
}
2627

28+
// Remove namespace(s)
2729
$contents = $contents->replaceMatches('/namespace[^\n]*/', '');
2830

31+
// Remove class declaration
2932
if ($removeClass) {
3033
$contents = $contents->replaceMatches('/class[^\n]*\n{/', '')
3134
->trim('{}')
32-
// remove use statements
35+
// Remove use statements
3336
->replaceMatches('/^use [^\n]*$/m', '');
3437

35-
// unindent all lines by 4 spaces
38+
// Unindent all lines by 4 spaces
3639
$lines = explode("\n", $contents);
3740
$lines = array_map(function (string $line) {
3841
return substr($line, 4);

ux.symfony.com/symfony.lock

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,6 @@
181181
"ralouphie/getallheaders": {
182182
"version": "3.0.3"
183183
},
184-
"scrivo/highlight.php": {
185-
"version": "v9.18.1.9"
186-
},
187184
"sebastian/cli-parser": {
188185
"version": "1.0.1"
189186
},

ux.symfony.com/templates/components/CodeBlock.html.twig

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
<div {{ attributes.defaults({
22
class: 'Terminal ' ~ this.classString,
33
}) }}>
4+
45
{% if showFilename %}
56
<div class="Terminal_header py-2 ps-4 pe-2 mb-0 d-flex justify-content-between align-items-center">
67
<a id="{{ this.elementId }}" href="#{{ this.elementId }}" class="Terminal_title"><code>{{ filename }}</code></a>
78
<div class="Terminal_actions">
8-
<twig:CodeBlockButtons source="{{ this.rawSource }}" link="{{ this.githubLink }}" />
9+
<twig:CodeBlockButtons source="{{ this.rawSource }}" link="{{ this.githubLink }}"/>
910
</div>
1011
</div>
1112
{% endif %}
13+
1214
<div class="Terminal_body" {{ stimulus_controller('code-expander') }}>
1315
{% if not showFilename %}
1416
<div class="Terminal_actions">
1517
<twig:CodeBlockButtons source="{{ this.rawSource }}" link="{{ this.githubLink }}"/>
1618
</div>
1719
{% endif %}
18-
<pre style="height: {{ height }};" {{ stimulus_target('code-expander', 'codeContent') }}><code>
19-
{{- this.highlightSource()|raw -}}
20-
</code></pre>
20+
<div class="Terminal_content" style="height: {{ height }};"
21+
{{ stimulus_target('code-expander', 'codeContent') }}
22+
{{ stimulus_controller('code-highlighter') }}
23+
>
24+
{% for piece in this.prepareSource %}
25+
{% if piece.highlight ?? true %}
26+
<pre><code class="language-{{ this.language }}" {{ stimulus_target('code-highlighter', 'codeBlock') }}>
27+
{{- piece.content -}}
28+
</code></pre>
29+
{% else %}
30+
{{- piece.content|raw -}}
31+
{% endif %}
32+
{% endfor %}
33+
</div>
2134

2235
{% block code_content_bottom %}
2336
<button

0 commit comments

Comments
 (0)