Skip to content

Commit a32bc94

Browse files
authored
Support rendering release pages with PHP 8.3+ (#1613)
See php/php-src#11913
1 parent 84ce0c6 commit a32bc94

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

include/layout.inc

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,33 @@ ini_set('highlight.keyword', 'keyword');
1414
ini_set('highlight.string', 'string');
1515
ini_set('highlight.html', 'html');
1616

17+
// convert PHP 8.2 highlight_string() output to match PHP 8.3+
18+
function normalize_highlight_string(string $html): string
19+
{
20+
$search = ["\r\n", "\n", '<br />', '&nbsp;'];
21+
$replace = ["\n", '', "\n", ' '];
22+
$result = str_replace($search, $replace, $html);
23+
24+
// strip extra span tag
25+
$result = substr_replace($result, '', 5, 6);
26+
$result = substr_replace($result, '', -14, 7);
27+
28+
return '<pre>' . $result . '</pre>';
29+
}
30+
1731
// Highlight PHP code
1832
function highlight_php($code, $return = false)
1933
{
2034
$highlighted = highlight_string($code, true);
2135

22-
// Use this ugly hack for now to avoid code snippets with bad syntax screwing up the highlighter
23-
if (strstr($highlighted, "include/layout.inc</b>")) {
24-
$highlighted = '<span class="html">' . nl2br(htmlentities($code, ENT_HTML5), false) . "</span>";
36+
if (PHP_VERSION_ID < 80300) {
37+
$highlighted = normalize_highlight_string($highlighted);
2538
}
2639

27-
// Fix output to use CSS classes and wrap well
28-
$highlighted = '<div class="phpcode">' . strtr(
29-
$highlighted,
30-
[
31-
'&nbsp;' => ' ',
32-
"\n" => '',
33-
34-
'<span style="color: ' => '<span class="',
35-
],
36-
) . '</div>';
40+
// Fix output to use CSS classes
41+
$search = ['<code style="color: ', '<span style="color: '];
42+
$replace = ['<code class="', '<span class="'];
43+
$highlighted = '<div class="phpcode">' . str_replace($search, $replace, $highlighted) . '</div>';
3744

3845
if ($return) { return $highlighted; }
3946
echo $highlighted;
@@ -45,7 +52,7 @@ function highlight_php_trimmed($code, $return = false)
4552
{
4653
$code = "<?php\n" . $code;
4754
$highlighted_code = highlight_php($code, true);
48-
$highlighted_code = preg_replace("!&lt;\?php(<br />)+!", '', $highlighted_code, 1);
55+
$highlighted_code = preg_replace("!&lt;\?php(\\n)+!", '', $highlighted_code, 1);
4956

5057
// add syntax highlighting for variables
5158
$variableReplacer = function (array $matches) {

styles/theme-base.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,9 @@ div.tip p:first-child {
961961
margin-bottom: 1.5rem;
962962
}
963963

964+
.phpcode pre {
965+
margin: 0;
966+
}
964967
.phpcode code {
965968
display: block;
966969
overflow-x: auto;

0 commit comments

Comments
 (0)