Skip to content

Commit 0aa67ea

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.
1 parent a32bc94 commit 0aa67ea

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
@@ -72,6 +72,148 @@ function highlight_php_trimmed($code, $return = false)
7272
return null;
7373
}
7474

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

161-
// Return a hiperlink to something within the site
303+
// Return a hyperlink to something within the site
162304
function make_link(string $url, string $linktext = ''): string
163305
{
164306
return sprintf("<a href=\"%s\">%s</a>", $url, $linktext ?: $url);

0 commit comments

Comments
 (0)