Skip to content

Commit 9011e3d

Browse files
committed
fixing php custom highlighting and adding twig
1 parent ab71c81 commit 9011e3d

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

src/Renderers/CodeNodeRenderer.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
class CodeNodeRenderer implements NodeRenderer
1313
{
14+
private static $isHighlighterConfigured = false;
15+
1416
private const LANGUAGES_MAPPING = [
1517
'html+jinja' => 'twig',
1618
'html+twig' => 'twig',
@@ -41,6 +43,8 @@ public function __construct(CodeNode $codeNode, TemplateRenderer $templateRender
4143

4244
public function render(): string
4345
{
46+
$this->configureHighlighter();
47+
4448
$value = $this->codeNode->getValue();
4549

4650
if ($this->codeNode->isRaw()) {
@@ -59,7 +63,6 @@ public function render(): string
5963

6064
if ('text' !== $language) {
6165
$highLighter = new Highlighter();
62-
Highlighter::registerLanguage('php', __DIR__.'/../Templates/highlight.php/pjp.json', true);
6366
$code = $highLighter->highlight(self::LANGUAGES_MAPPING[$language] ?? $language, $code)->value;
6467
}
6568

@@ -103,4 +106,14 @@ private function getLines(string $code): array
103106

104107
return array_reverse($reversedLines);
105108
}
109+
110+
private function configureHighlighter()
111+
{
112+
if (false === self::$isHighlighterConfigured) {
113+
Highlighter::registerLanguage('php', __DIR__.'/../Templates/highlight.php/php.json', true);
114+
Highlighter::registerLanguage('twig', __DIR__.'/../Templates/highlight.php/twig.json', true);
115+
}
116+
117+
self::$isHighlighterConfigured = true;
118+
}
106119
}

src/Templates/highlight.php/twig.json

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"aliases": [
3+
"craftcms"
4+
],
5+
"case_insensitive": true,
6+
"subLanguage": "xml",
7+
"contains": [
8+
{
9+
"className": "comment",
10+
"begin": "\\{#",
11+
"end": "#}",
12+
"contains": [
13+
{
14+
"begin": "\\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\\b"
15+
},
16+
{
17+
"className": "doctag",
18+
"begin": "(?:TODO|FIXME|NOTE|BUG|XXX):",
19+
"relevance": 0
20+
}
21+
]
22+
},
23+
{
24+
"className": "template-tag",
25+
"begin": "\\{%",
26+
"end": "%}",
27+
"contains": [
28+
{
29+
"className": "name",
30+
"begin": "\\w+",
31+
"keywords": "autoescape block do embed extends filter flush for if import include macro sandbox set spaceless use verbatim endautoescape endblock enddo endembed endextends endfilter endflush endfor endif endimport endinclude endmacro endsandbox endset endspaceless enduse endverbatim",
32+
"starts": {
33+
"endsWithParent": true,
34+
"contains": [
35+
{
36+
"begin": "\\|[A-Za-z_]+:?",
37+
"keywords": "abs batch capitalize convert_encoding date date_modify default escape first format join json_encode keys last length lower merge nl2br number_format raw replace reverse round slice sort split striptags title trim upper url_encode",
38+
"contains": [
39+
{
40+
"beginKeywords": "attribute block constant cycle date dump include max min parent random range source template_from_string",
41+
"keywords": {
42+
"name": "attribute block constant cycle date dump include max min parent random range source template_from_string"
43+
},
44+
"relevance": 0,
45+
"contains": [
46+
{
47+
"className": "params",
48+
"begin": "\\(",
49+
"end": "\\)"
50+
}
51+
]
52+
}
53+
]
54+
},
55+
{
56+
"$ref": "#contains.1.contains.0.starts.contains.0.contains.0"
57+
}
58+
],
59+
"relevance": 0
60+
}
61+
}
62+
]
63+
},
64+
{
65+
"className": "template-variable",
66+
"begin": "\\{\\{",
67+
"end": "}}",
68+
"contains": [
69+
"self",
70+
{
71+
"$ref": "#contains.1.contains.0.starts.contains.0"
72+
},
73+
{
74+
"$ref": "#contains.1.contains.0.starts.contains.0.contains.0"
75+
}
76+
]
77+
}
78+
]
79+
}

0 commit comments

Comments
 (0)