Skip to content

Commit f5f8013

Browse files
committed
Render feature comparisons with standard function
This refactor greatly simplifies maintaining consistent styling and markup for the feature comparisons. No content has been changed, except for fixing unnecessary blank lines at the start & end of the "Command line linter supports multiple files" code blocks on the PHP 8.3 release page. I also have a patch ready to apply the refactor to the PHP 8.5 release page after this is merged.
1 parent cf80cca commit f5f8013

File tree

7 files changed

+1444
-2105
lines changed

7 files changed

+1444
-2105
lines changed

include/layout.inc

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,148 @@ function highlight_php_trimmed($code, $return = false)
5252
return null;
5353
}
5454

55+
function get_rfc_link(string $namedLink): string
56+
{
57+
// Use 'title|link' string since multiple links may have the same title
58+
preg_match('!^((.+)\|)?(.+)$!', $namedLink, $matches);
59+
$link = $matches[3];
60+
$title = $matches[2] ?: 'Link';
61+
62+
return '<a class="php8-rfc" href="' . $link . '">' . $title . '</a>';
63+
}
64+
65+
function feature_header(string $id, string $title, array $links): string
66+
{
67+
$links = array_map(get_rfc_link(...), $links);
68+
$linksHtml = implode("\n", $links);
69+
70+
return <<<HTML
71+
<h2 class="php8-h2" id="$id">
72+
$title
73+
$linksHtml
74+
</h2>
75+
HTML;
76+
}
77+
78+
function get_code_block(
79+
string $beforeCode,
80+
string $afterCode,
81+
string $version,
82+
string $previousVersion,
83+
bool $highlightCode,
84+
): string {
85+
$alignClass = '';
86+
$noBeforeCode = $beforeCode === '';
87+
88+
if (substr_count($beforeCode, "\n") - substr_count($afterCode, "\n") > 12) {
89+
// if new code is substantially shorter, align to top of code block
90+
$alignClass = 'align-start';
91+
}
92+
93+
if ($highlightCode) {
94+
$beforeCode = highlight_php_trimmed($beforeCode, true);
95+
$afterCode = highlight_php_trimmed($afterCode, true);
96+
} else {
97+
$beforeCode = "<code>$beforeCode</code>";
98+
$afterCode = "<code>$afterCode</code>";
99+
}
100+
101+
$version = htmlentities($version);
102+
$previousVersion = htmlentities($previousVersion);
103+
104+
if ($noBeforeCode) {
105+
$codeMarkup = <<<HTML
106+
<div class="example-contents example-contents-full">
107+
<div class="php8-compare__label php8-compare__label_new">$version</div>
108+
<div class="php8-code phpcode">$afterCode</div>
109+
</div>
110+
HTML;
111+
} else {
112+
$codeMarkup = <<<HTML
113+
<div class="php8-compare__block example-contents">
114+
<div class="php8-compare__label">$previousVersion</div>
115+
<div class="php8-code phpcode">$beforeCode</div>
116+
</div>
117+
<div class="php8-compare__arrow"></div>
118+
<div class="php8-compare__block example-contents">
119+
<div class="php8-compare__label php8-compare__label_new">$version</div>
120+
<div class="php8-code $alignClass phpcode">
121+
$afterCode
122+
</div>
123+
</div>
124+
HTML;
125+
}
126+
127+
return <<<HTML
128+
<div class="php8-compare__main">
129+
$codeMarkup
130+
</div>
131+
HTML;
132+
}
133+
134+
function feature_comparison(
135+
string $id,
136+
string $title,
137+
string $description,
138+
string $version,
139+
string $previousVersion,
140+
string $beforeCode,
141+
string $afterCode,
142+
bool $highlightCode = true,
143+
array $links = [],
144+
string $beforeCode2 = '',
145+
string $afterCode2 = '',
146+
): string {
147+
$header = feature_header($id, $title, $links);
148+
$contentHtml = '';
149+
$codeBlock = get_code_block($beforeCode, $afterCode, $version, $previousVersion, $highlightCode);
150+
$code2Block = '';
151+
$contentClass = 'php8-compare__content';
152+
153+
if ($afterCode2 !== '') {
154+
$code2Block = get_code_block($beforeCode2, $afterCode2, $version, $previousVersion, $highlightCode);
155+
$contentClass .= ' php8-compare__content--spaced';
156+
}
157+
158+
if ($description !== '') {
159+
$contentHtml = <<<HTML
160+
<div class="$contentClass">$description</div>
161+
HTML;
162+
}
163+
164+
return <<<HTML
165+
<div class="php8-compare">
166+
$header
167+
$codeBlock
168+
$contentHtml
169+
$code2Block
170+
</div>
171+
HTML;
172+
}
173+
174+
function feature_comparisons(array $comparisons, string $version, string $previousVersion): string
175+
{
176+
$html = '<section class="php8-section center">' . "\n";
177+
178+
foreach ($comparisons as $feature) {
179+
$html .= feature_comparison(
180+
id: $feature['id'],
181+
title: $feature['title'],
182+
description: $feature['description'] ?? '',
183+
version: $version,
184+
previousVersion: $previousVersion,
185+
beforeCode: $feature['before'] ?? '',
186+
afterCode: $feature['after'],
187+
highlightCode: $feature['highlightCode'] ?? true,
188+
links: $feature['links'] ?? [],
189+
beforeCode2: $feature['before2'] ?? '',
190+
afterCode2: $feature['after2'] ?? '',
191+
) . "\n";
192+
}
193+
194+
return $html . "\n" . '</section>';
195+
}
196+
55197
// Resize the image using the output of make_image()
56198
function resize_image($img, $width = 1, $height = 1)
57199
{
@@ -138,7 +280,7 @@ function make_submit($file, $alt = false, $align = false, $extras = false,
138280
return '<input type="image"' . substr($img, 4);
139281
}
140282

141-
// Return a hiperlink to something within the site
283+
// Return a hyperlink to something within the site
142284
function make_link(string $url, string $linktext = ''): string
143285
{
144286
return sprintf("<a href=\"%s\">%s</a>", $url, $linktext ?: $url);

0 commit comments

Comments
 (0)