Skip to content

Commit 374ba05

Browse files
committed
Highlight focus comment
1 parent f3a3090 commit 374ba05

File tree

3 files changed

+83
-23
lines changed

3 files changed

+83
-23
lines changed

include/layout.inc

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,86 @@ ini_set('highlight.string', 'string');
1515
ini_set('highlight.html', 'html');
1616

1717
// Highlight PHP code
18-
function highlight_php($code, $return = false)
18+
function highlight_php(string $code, bool $return = false): ?string
1919
{
20-
$highlighted = highlight_string($code, true);
20+
$hasFocusTag = preg_match('!//.*\[focus]!', $code);
21+
22+
$highlighted = highlight_php_process_lines($code, $hasFocusTag);
23+
24+
if ($return) {
25+
return $highlighted;
26+
}
27+
28+
echo $highlighted;
29+
return null;
30+
}
31+
32+
function highlight_php_process_lines(string $code, bool $hasFocusTag): string
33+
{
34+
$lines = explode(PHP_EOL, $code);
35+
$processedLines = [];
36+
37+
foreach ($lines as $line) {
38+
if ($line === '') {
39+
$processedLines[] = '<br />';
40+
continue;
41+
}
42+
43+
$processedLines[] = highlight_php_process_single_line($line);
44+
}
45+
46+
$highlighted = implode('', $processedLines);
2147

22-
// Use this ugly hack for now to avoid code snippets with bad syntax screwing up the highlighter
2348
if (strstr($highlighted, "include/layout.inc</b>")) {
24-
$highlighted = '<span class="html">' . nl2br(htmlentities($code, ENT_HTML5), false) . "</span>";
49+
return format_as_plain_code($code);
2550
}
2651

27-
// Fix output to use CSS classes and wrap well
28-
$highlighted = '<div class="phpcode">' . strtr(
29-
$highlighted,
30-
[
31-
'&nbsp;' => ' ',
32-
"\n" => '',
52+
return format_highlighted_output($highlighted, $hasFocusTag);
53+
}
3354

34-
'<span style="color: ' => '<span class="',
35-
],
36-
) . '</div>';
55+
function highlight_php_process_single_line(string $line): string
56+
{
57+
$highlighted = highlight_string('<?php ' . $line, true);
58+
$highlighted = str_replace('&lt;?php&nbsp;', '', $highlighted);
3759

38-
if ($return) { return $highlighted; }
39-
echo $highlighted;
40-
return null;
60+
$isFocus = preg_match('!//.*\[focus]!', $line);
61+
62+
$highlighted = preg_replace('!//[^<]*\s*\[focus]!', '', $highlighted);
63+
64+
return '<span class="' . ($isFocus ? 'focus' : 'fade') . '">' . $highlighted . '</span>';
65+
}
66+
67+
function format_as_plain_code(string $code): string
68+
{
69+
return '<div class="phpcode"><span class="html">' .
70+
nl2br(htmlentities($code, ENT_HTML5), false) .
71+
'</span></div>';
4172
}
4273

43-
// Same as highlight_php() but does not require '<?php' in $code
44-
function highlight_php_trimmed($code, $return = false)
74+
function format_highlighted_output(string $highlighted, bool $hasFocus): string
75+
{
76+
return '<div class="phpcode' . ($hasFocus ? ' phpcode--focused' : '') . '">' .
77+
strtr(
78+
$highlighted,
79+
[
80+
'&nbsp;' => ' ',
81+
"\n" => '',
82+
'<span style="color: ' => '<span class="',
83+
],
84+
) .
85+
'</div>';
86+
}
87+
88+
function highlight_php_trimmed(string $code, bool $return = false): ?string
4589
{
4690
$code = "<?php\n" . $code;
4791
$highlighted_code = highlight_php($code, true);
4892
$highlighted_code = preg_replace("!&lt;\?php(<br />)+!", '', $highlighted_code, 1);
4993

50-
if ($return) { return $highlighted_code; }
94+
if ($return) {
95+
return $highlighted_code;
96+
}
97+
5198
echo $highlighted_code;
5299
return null;
53100
}

releases/8.5/release.inc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,11 @@ final class Stopwatch
267267
public function stopLap()
268268
{
269269
$fromStart = hrtime(true) - $this->start;
270-
$lastLap = $this->laps !== []
271-
? $this->laps[array_key_last($this->laps)]
272-
: 0;
270+
271+
$lastLap = $this->laps !== [] // [focus]
272+
? $this->laps[array_key_last($this->laps)] // [focus]
273+
: 0; // [focus]
274+
273275
$lapDuration = $fromStart - $lastLap;
274276
275277
$this->laps[] = $fromStart;
@@ -306,7 +308,9 @@ final class Stopwatch
306308
public function stopLap()
307309
{
308310
$fromStart = hrtime(true) - $this->start;
309-
$lastLap = array_last($this->laps) ?? 0;
311+
312+
$lastLap = array_last($this->laps) ?? 0; // [focus]
313+
310314
$lapDuration = $fromStart - $lastLap;
311315
312316
$this->laps[] = $fromStart;

styles/theme-base.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,15 @@ dl dd {
501501
overflow-x: auto;
502502
}
503503

504+
.phpcode--focused .fade {
505+
opacity: .5;
506+
transition: .2s all;
507+
}
508+
509+
.phpcode--focused:hover .fade {
510+
opacity: 1;
511+
}
512+
504513
.phpcode, div.classsynopsis {
505514
text-align: left;
506515
}

0 commit comments

Comments
 (0)