From 2c9df56b5b32592b4e5fb3df3f21b7e1694ddc21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 8 Aug 2023 17:07:53 +0200 Subject: [PATCH 1/2] Drop `\n` when highlighting Actual newlines will be replaced by `
` during highlighting and the `\n` in the boilerplate conflicts with `white-space: pre;`. see php/doc-en#2648 --- phpdotnet/phd/Highlighter.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/phpdotnet/phd/Highlighter.php b/phpdotnet/phd/Highlighter.php index 33d190c5..c3e578c9 100644 --- a/phpdotnet/phd/Highlighter.php +++ b/phpdotnet/phd/Highlighter.php @@ -63,7 +63,11 @@ public function highlight($text, $role, $format) if ($role == 'php') { try { - return str_replace(' ', ' ', highlight_string($text, 1)); + $highlighted = highlight_string($text, 1); + $highlighted = str_replace(' ', ' ', $highlighted); + $highlighted = str_replace("\n", '', $highlighted); + + return $highlighted; } catch (\ParseException $e) { v("Parse error while highlighting PHP code: %s\nText: %s", (string) $e, $text, E_USER_WARNING); From 62c531565e30fad9465cbb1213ba0dcc0b74dceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 8 Aug 2023 17:23:59 +0200 Subject: [PATCH 2/2] Simplify character replacement for `highlight_string` result --- phpdotnet/phd/Highlighter.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/phpdotnet/phd/Highlighter.php b/phpdotnet/phd/Highlighter.php index c3e578c9..b32a3711 100644 --- a/phpdotnet/phd/Highlighter.php +++ b/phpdotnet/phd/Highlighter.php @@ -63,11 +63,10 @@ public function highlight($text, $role, $format) if ($role == 'php') { try { - $highlighted = highlight_string($text, 1); - $highlighted = str_replace(' ', ' ', $highlighted); - $highlighted = str_replace("\n", '', $highlighted); - - return $highlighted; + return strtr(highlight_string($text, 1), [ + ' ' => ' ', + "\n" => '', + ]); } catch (\ParseException $e) { v("Parse error while highlighting PHP code: %s\nText: %s", (string) $e, $text, E_USER_WARNING);