@@ -15,39 +15,86 @@ ini_set('highlight.string', 'string');
1515ini_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- ' ' => ' ' ,
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 ('<?php ' , '' , $ 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+ ' ' => ' ' ,
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 ("!<\?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}
0 commit comments