Skip to content

Commit 027833c

Browse files
committed
feature #176 Add more modern PHP syntax highlighting (wouterj)
This PR was squashed before being merged into the main branch. Discussion ---------- Add more modern PHP syntax highlighting Because I had fun playing around with this, I've added more changes to the PHP syntax file, bringing it more or less up to date with Highlight.js' syntax file. Noteworthy changes: * Support arrow functions, enums, match statement, named arguments and more constants * Updated the full list of supported PHP types * Identify function class/class instantiations (this improves highlighting function arguments) `@javiereguiluz` please test this out locally. This patch will generally add more color to the code blocks. You might want to tweak the CSS a bit depending on your favor (we seem to have relatively little coloring in our code examples). E.g. function calls now get `hljs-title` as well (you can use `.invoke__` to differentiate them from other titles), and named arguments will be labelled as `hljs-attr` (which is currently colored similar to `hljs-meta` used for PHP attributes). | Before | After | --- | --- | ![image](https://github.com/symfony-tools/docs-builder/assets/749025/f38b5b93-f7e7-4bde-9be7-4b8f07adf961) | ![image](https://github.com/symfony-tools/docs-builder/assets/749025/cc2b36d7-73d0-4bc9-a151-6b1aaf699d7c) | ![image](https://github.com/symfony-tools/docs-builder/assets/749025/5d0054ed-f3cf-459f-b079-1ace730dadea) | ![image](https://github.com/symfony-tools/docs-builder/assets/749025/c57facf0-101c-414d-92cd-c2f3c514d2f8) | ![image](https://github.com/symfony-tools/docs-builder/assets/749025/89fc8e5f-a513-4c37-8ae1-8015a2960e55) | ![image](https://github.com/symfony-tools/docs-builder/assets/749025/5c772837-027c-4ae8-929e-9aaab12fc05f) Commits ------- 0642507 Add more modern PHP syntax highlighting
2 parents 533dd4d + 0642507 commit 027833c

File tree

9 files changed

+277
-51
lines changed

9 files changed

+277
-51
lines changed

src/Renderers/CodeNodeRenderer.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ public function render(): string
7070

7171
$highLighter = new Highlighter();
7272
$highlightedCode = $highLighter->highlight($languageMapping, $code)->value;
73-
74-
// this allows to highlight the $ in PHP variable names
75-
$highlightedCode = str_replace('<span class="hljs-variable">$', '<span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>', $highlightedCode);
7673
}
7774

7875
if ('terminal' === $language) {

src/Templates/highlight.php/php.json

Lines changed: 130 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"php7"
99
],
1010
"case_insensitive": true,
11-
"keywords": "and include_once list abstract global private echo interface as static endswitch array null if endwhile or const for endforeach self var while isset public protected exit foreach throw elseif include __FILE__ empty require_once do xor return parent clone use __CLASS__ __LINE__ else break print eval new catch __METHOD__ case exception default die require __FUNCTION__ enddeclare final try match switch continue endfor endif declare unset true false goto instanceof insteadof __DIR__ __NAMESPACE__ yield finally",
11+
"keywords": "PHP_VERSION PHP_MAJOR_VERSION PHP_MINOR_VERSION PHP_RELEASE_VERSION PHP_VERSION_ID PHP_EXTRA_VERSION ZEND_THREAD_SAFE ZEND_DEBUG_BUILD PHP_ZTS PHP_DEBUG PHP_MAXPATHLEN PHP_OS PHP_OS_FAMILY PHP_SAPI PHP_EOL PHP_INT_MAX PHP_INT_MIN PHP_INT_SIZE PHP_FLOAT_DIG PHP_FLOAT_EPSILON PHP_FLOAT_MIN PHP_FLOAT_MAX DEFAULT_INCLUDE_PATH PEAR_INSTALL_DIR PEAR_EXTENSION_DIR PHP_EXTENSION_DIR PHP_PREFIX PHP_BINDIR PHP_BINARY PHP_MANDIR PHP_LIBDIR PHP_DATADIR PHP_SYSCONFDIR PHP_LOCALSTATEDIR PHP_CONFIG_FILE_PATH PHP_CONFIG_FILE_SCAN_DIR PHP_SHLIB_SUFFIX PHP_FD_SETSIZE E_ERROR E_WARNING E_PARSE E_NOTICE E_CORE_ERROR E_CORE_WARNING E_COMPILE_ERROR E_COMPILE_WARNING E_USER_ERROR E_USER_WARNING E_USER_NOTICE E_RECOVERABLE_ERROR E_DEPRECATED E_USER_DEPRECATED E_ALL E_STRICT __COMPILER_HALT_OFFSET__ PHP_WINDOWS_EVENT_CTRL_C PHP_WINDOWS_EVENT_CTRL_BREAK PHP_CLI_PROCESS_TITLE STDERR STDIN STDOUT __CLASS__ __DIR__ __FILE__ __FUNCTION__ __LINE__ __METHOD__ __NAMESPACE__ __TRAIT__ die echo exit include include_once print require require_once abstract and as binary break case catch class clone const continue declare default do double else elseif empty enddeclare endfor endforeach endif endswitch endwhile enum eval extends final finally for foreach from global goto if implements instanceof insteadof interface isset list match|0 new or parent private protected public readonly return switch throw trait try unset use var void while xor yield array bool boolean callable float int integer iterable mixed never numeric object real string resource self static false FALSE null NULL true TRUE",
1212
"contains": [
1313
{
1414
"className": "meta",
@@ -26,9 +26,10 @@
2626
{
2727
"begin": "\\(",
2828
"end": "\\)",
29-
"keywords": "true false null new array",
29+
"keywords": "array bool boolean float int integer new real string false FALSE null NULL true TRUE PHP_VERSION PHP_MAJOR_VERSION PHP_MINOR_VERSION PHP_RELEASE_VERSION PHP_VERSION_ID PHP_EXTRA_VERSION ZEND_THREAD_SAFE ZEND_DEBUG_BUILD PHP_ZTS PHP_DEBUG PHP_MAXPATHLEN PHP_OS PHP_OS_FAMILY PHP_SAPI PHP_EOL PHP_INT_MAX PHP_INT_MIN PHP_INT_SIZE PHP_FLOAT_DIG PHP_FLOAT_EPSILON PHP_FLOAT_MIN PHP_FLOAT_MAX DEFAULT_INCLUDE_PATH PEAR_INSTALL_DIR PEAR_EXTENSION_DIR PHP_EXTENSION_DIR PHP_PREFIX PHP_BINDIR PHP_BINARY PHP_MANDIR PHP_LIBDIR PHP_DATADIR PHP_SYSCONFDIR PHP_LOCALSTATEDIR PHP_CONFIG_FILE_PATH PHP_CONFIG_FILE_SCAN_DIR PHP_SHLIB_SUFFIX PHP_FD_SETSIZE E_ERROR E_WARNING E_PARSE E_NOTICE E_CORE_ERROR E_CORE_WARNING E_COMPILE_ERROR E_COMPILE_WARNING E_USER_ERROR E_USER_WARNING E_USER_NOTICE E_RECOVERABLE_ERROR E_DEPRECATED E_USER_DEPRECATED E_ALL E_STRICT __COMPILER_HALT_OFFSET__ PHP_WINDOWS_EVENT_CTRL_C PHP_WINDOWS_EVENT_CTRL_BREAK PHP_CLI_PROCESS_TITLE STDERR STDIN STDOUT __CLASS__ __DIR__ __FILE__ __FUNCTION__ __LINE__ __METHOD__ __NAMESPACE__ __TRAIT__",
3030
"contains": {
31-
"$ref": "#contains.11.contains.1.contains"
31+
"$ref": "#contains.10.contains.3.contains",
32+
"_": "params"
3233
}
3334
},
3435
{
@@ -137,39 +138,124 @@
137138
},
138139
{
139140
"className": "variable",
140-
"begin": "\\$this\\b"
141-
},
142-
{
143-
"className": "variable",
144-
"begin": "\\$+[a-zA-Z_-ÿ][a-zA-Z0-9_-ÿ]*"
141+
"begin": "\\$+[a-zA-Z_-ÿ][a-zA-Z0-9_-ÿ]*",
142+
"returnBegin": true,
143+
"contains": [
144+
{
145+
"className": "variable-other-marker",
146+
"begin": "\\$"
147+
},
148+
{
149+
"begin": "\\$*[a-zA-Z_-ÿ][a-zA-Z0-9_-ÿ]*"
150+
}
151+
]
145152
},
146153
{
147-
"className": "operator",
148-
"begin": "(::|->)",
149-
"end": "[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*",
150-
"excludeEnd": true
154+
"begin": "\\b(?!fn\\b|function\\b|__CLASS__\\b|__DIR__\\b|__FILE__\\b|__FUNCTION__\\b|__COMPILER_HALT_OFFSET__\\b|__LINE__\\b|__METHOD__\\b|__NAMESPACE__\\b|__TRAIT__\\b|die\\b|echo\\b|exit\\b|include\\b|include_once\\b|print\\b|require\\b|require_once\\b|array\\b|abstract\\b|and\\b|as\\b|binary\\b|bool\\b|boolean\\b|break\\b|callable\\b|case\\b|catch\\b|class\\b|clone\\b|const\\b|continue\\b|declare\\b|default\\b|do\\b|double\\b|else\\b|elseif\\b|empty\\b|enddeclare\\b|endfor\\b|endforeach\\b|endif\\b|endswitch\\b|endwhile\\b|enum\\b|eval\\b|extends\\b|final\\b|finally\\b|float\\b|for\\b|foreach\\b|from\\b|global\\b|goto\\b|if\\b|implements\\b|instanceof\\b|insteadof\\b|int\\b|integer\\b|interface\\b|isset\\b|iterable\\b|list\\b|match\\b|mixed\\b|new\\b|never\\b|object\\b|or\\b|private\\b|protected\\b|public\\b|readonly\\b|real\\b|return\\b|string\\b|switch\\b|throw\\b|trait\\b|try\\b|unset\\b|use\\b|var\\b|void\\b|while\\b|xor\\b|yield|Countable\\b|OuterIterator\\b|RecursiveIterator\\b|SeekableIterator\\b|ArrayAccess\\b|BackedEnum\\b|Generator\\b|Iterator\\b|IteratorAggregate\\b|Serializable\\b|Stringable\\b|Throwable\\b|Traversable\\b|UnitEnum\\b|__PHP_Incomplete_Class\\b|parent\\b|php_user_filter\\b|self\\b|static\\b)[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*(?![A-Za-z0-9])(?![$])[ \\t\\n]*(?=(?=\\())",
155+
"end": "\\)",
156+
"returnBegin": true,
157+
"contains": [
158+
{
159+
"className": "title invoke__",
160+
"begin": "[a-zA-Z0-9_\\x7f-\\xff]\\w*",
161+
"relevance": 0
162+
},
163+
{
164+
"begin": "\\(",
165+
"endsWithParent": true,
166+
"keywords": "array bool boolean float int integer new real string false FALSE null NULL true TRUE PHP_VERSION PHP_MAJOR_VERSION PHP_MINOR_VERSION PHP_RELEASE_VERSION PHP_VERSION_ID PHP_EXTRA_VERSION ZEND_THREAD_SAFE ZEND_DEBUG_BUILD PHP_ZTS PHP_DEBUG PHP_MAXPATHLEN PHP_OS PHP_OS_FAMILY PHP_SAPI PHP_EOL PHP_INT_MAX PHP_INT_MIN PHP_INT_SIZE PHP_FLOAT_DIG PHP_FLOAT_EPSILON PHP_FLOAT_MIN PHP_FLOAT_MAX DEFAULT_INCLUDE_PATH PEAR_INSTALL_DIR PEAR_EXTENSION_DIR PHP_EXTENSION_DIR PHP_PREFIX PHP_BINDIR PHP_BINARY PHP_MANDIR PHP_LIBDIR PHP_DATADIR PHP_SYSCONFDIR PHP_LOCALSTATEDIR PHP_CONFIG_FILE_PATH PHP_CONFIG_FILE_SCAN_DIR PHP_SHLIB_SUFFIX PHP_FD_SETSIZE E_ERROR E_WARNING E_PARSE E_NOTICE E_CORE_ERROR E_CORE_WARNING E_COMPILE_ERROR E_COMPILE_WARNING E_USER_ERROR E_USER_WARNING E_USER_NOTICE E_RECOVERABLE_ERROR E_DEPRECATED E_USER_DEPRECATED E_ALL E_STRICT __COMPILER_HALT_OFFSET__ PHP_WINDOWS_EVENT_CTRL_C PHP_WINDOWS_EVENT_CTRL_BREAK PHP_CLI_PROCESS_TITLE STDERR STDIN STDOUT __CLASS__ __DIR__ __FILE__ __FUNCTION__ __LINE__ __METHOD__ __NAMESPACE__ __TRAIT__",
167+
"contains": [
168+
{
169+
"className": "attr",
170+
"begin": "[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*(?![A-Za-z0-9])(?![$])(?=:)(?=(?!::))"
171+
},
172+
{
173+
"variants": [
174+
{
175+
"begin": "::(?=(?!class\\b))[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*(?![A-Za-z0-9])(?![$])\\b(?!\\()",
176+
"returnBegin": true,
177+
"contains": [
178+
{
179+
"begin": "::"
180+
},
181+
{
182+
"className": "variable constant_",
183+
"begin": "[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*(?![A-Za-z0-9])(?![$])\\b(?!\\()"
184+
}
185+
]
186+
},
187+
{
188+
"begin": "::class",
189+
"returnBegin": true,
190+
"contains": [
191+
{
192+
"begin": "::"
193+
},
194+
{
195+
"className": "variable language_",
196+
"begin": "class"
197+
}
198+
]
199+
}
200+
]
201+
},
202+
{
203+
"$ref": "#contains.8",
204+
"_": "variable"
205+
},
206+
{
207+
"$ref": "#contains.10.contains.3.contains.2",
208+
"_": "comment"
209+
},
210+
{
211+
"$ref": "#contains.10.contains.3.contains.3",
212+
"_": "string"
213+
},
214+
{
215+
"$ref": "#contains.10.contains.3.contains.4",
216+
"_": "number"
217+
},
218+
{
219+
"$ref": "#contains.10",
220+
"_": "closure"
221+
},
222+
{
223+
"$ref": "#contains.9",
224+
"_": "invoke"
225+
}
226+
]
227+
}
228+
]
151229
},
152230
{
153231
"className": "function",
154-
"beginKeywords": "function",
232+
"beginKeywords": "fn function",
155233
"end": "[;{]",
156234
"excludeEnd": true,
157235
"illegal": "\\$|\\[|%",
158236
"contains": [
237+
{
238+
"beginKeywords": "use"
239+
},
159240
{
160241
"className": "title",
161242
"begin": "[a-zA-Z_]\\w*",
162243
"relevance": 0
163244
},
245+
{
246+
"begin": "=>",
247+
"endsParent": true
248+
},
164249
{
165250
"className": "params",
166251
"begin": "\\(",
167252
"end": "\\)",
168-
"keywords": "true false null new array",
253+
"keywords": "array bool boolean callable float int integer iterable mixed never numeric object real string resource self static false FALSE null NULL true TRUE",
169254
"contains": [
170255
"self",
171256
{
172-
"$ref": "#contains.9"
257+
"$ref": "#contains.8",
258+
"_": "variable"
173259
},
174260
{
175261
"className": "comment",
@@ -254,27 +340,42 @@
254340
]
255341
},
256342
{
257-
"$ref": "#contains.0"
343+
"$ref": "#contains.0",
344+
"_": "simple-attribute"
258345
},
259346
{
260-
"$ref": "#contains.1"
347+
"$ref": "#contains.1",
348+
"_": "attribute"
261349
}
262350
]
263351
}
264352
]
265353
},
354+
{
355+
"$ref": "#contains.9.contains.1.contains.1",
356+
"_": "constant"
357+
},
266358
{
267359
"className": "class",
268-
"beginKeywords": "class interface trait enum",
269-
"end": "{",
360+
"variants": [
361+
{
362+
"beginKeywords": "enum",
363+
"illegal": "[($\"]"
364+
},
365+
{
366+
"beginKeywords": "class interface trait",
367+
"illegal": "[:($\"]"
368+
}
369+
],
370+
"end": "\\{",
270371
"excludeEnd": true,
271-
"illegal": "[:\\(\\$\"]",
272372
"contains": [
273373
{
274374
"beginKeywords": "extends implements"
275375
},
276376
{
277-
"$ref": "#contains.11.contains.0"
377+
"$ref": "#contains.10.contains.1",
378+
"_": "title"
278379
}
279380
]
280381
},
@@ -284,7 +385,8 @@
284385
"illegal": "[\\.']",
285386
"contains": [
286387
{
287-
"$ref": "#contains.11.contains.0"
388+
"$ref": "#contains.10.contains.1",
389+
"_": "title"
288390
}
289391
]
290392
},
@@ -293,18 +395,21 @@
293395
"end": ";",
294396
"contains": [
295397
{
296-
"$ref": "#contains.11.contains.0"
398+
"$ref": "#contains.10.contains.1",
399+
"_": "title"
297400
}
298401
]
299402
},
300403
{
301404
"begin": "=>"
302405
},
303406
{
304-
"$ref": "#contains.11.contains.1.contains.3"
407+
"$ref": "#contains.10.contains.3.contains.3",
408+
"_": "string"
305409
},
306410
{
307-
"$ref": "#contains.11.contains.1.contains.4"
411+
"$ref": "#contains.10.contains.3.contains.4",
412+
"_": "number"
308413
}
309414
]
310415
}

tests/IntegrationTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ public function testParseUnitBlock(string $blockName)
115115
$expected = preg_replace('/<\!\-\- REMOVE(.)+\-\->/', '', $expected);
116116

117117
$this->assertSame(
118-
$indenter->indent($expected),
119-
$indenter->indent(trim($actualCrawler->filter('body')->html()))
118+
$this->normalize($indenter->indent($expected)),
119+
$this->normalize($indenter->indent(trim($actualCrawler->filter('body')->html())))
120120
);
121121
}
122122

@@ -351,6 +351,11 @@ public function testParseString()
351351
$this->assertSame($htmlString, (new DocBuilder())->buildString($rstString)->getStringResult());
352352
}
353353

354+
private function normalize(string $str): string
355+
{
356+
return preg_replace('/\s+$/m', '', $str);
357+
}
358+
354359
private function createIndenter(): Indenter
355360
{
356361
$indenter = new Indenter();

tests/Templates/fixtures/php.output.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
*
1212
* <span class="hljs-doctag">@Route</span>("/blog/{slug}", name="blog_list")
1313
*/</span>
14-
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">list</span><span class="hljs-params">(UrlGeneratorInterface <span class="hljs-variable">$urlGenerator</span>, string <span class="hljs-variable">$slug</span>)</span>
14+
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">list</span><span class="hljs-params">(UrlGeneratorInterface <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>urlGenerator</span>, <span class="hljs-keyword">string</span> <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>slug</span>)</span>
1515
</span>{
16-
<span class="hljs-keyword">return</span> <span class="hljs-variable">$this</span><span class="hljs-operator">-&gt;</span>render(<span class="hljs-string">'foo/bar.html.twig'</span>, [
17-
<span class="hljs-string">'key'</span> =&gt; <span class="hljs-variable">$value</span>
16+
<span class="hljs-keyword">return</span> <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>this</span>-&gt;<span class="hljs-title invoke__">render</span>(<span class="hljs-string">'foo/bar.html.twig'</span>, [
17+
<span class="hljs-string">'key'</span> =&gt; <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>value</span>
1818
]
1919
}
2020
}

tests/fixtures/expected/blocks/code-blocks/php-attributes.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
;
149149
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">(
150150
<span class="hljs-meta">#[TaggedIterator</span>(<span class="hljs-string">'app.handlers'</span>)<span class="hljs-meta">]</span>
151-
iterable <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>handlers</span>,
151+
<span class="hljs-keyword">iterable</span> <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>handlers</span>,
152152
)</span></span>{
153153
}
154154

0 commit comments

Comments
 (0)