Due to Google AMP (Accelerated Mobile Pages) , I have been looking for a way to effectively do Syntax Highlighting without Javascript in pure PHP.
I was about to write my own, when I found an older article from phoboslab. Thanks Dominic for saving me some time ;) Its not perfect, but close enough.
A simple Syntax Highlighting Class that does just that. The class was not working with PHP 5.4.x+, as it uses preg_replace() with the /e modifier.
It will not cover all, but its better than nothing :) I will also add a section to my my AMP tweaks article to showcase the integration of Geshi.
Here an updated version using the preg_replace_callback() function.
-
class SyntaxHighlight {
-
-
- public static function process($s) {
-
- // Workaround for escaped backslashes
-
-
- // Punctuations
- '/([\-\!\%\^\*\(\)\+\|\~\=\`\{\}\[\]\:\"\'<>\?\,\.\/]+)/'
- => '<span class="P">$1</span>',
-
- // Numbers (also look for Hex)
- '/(?<!\w)(
- (0x|\#)[\da-f]+|
- \d+|
- \d+(px|em|cm|mm|rem|s|\%)
- )(?!\w)/ix'
- => '<span class="N">$1</span>',
-
- // Make the bold assumption that an
- // all uppercase word has a special meaning
- '/(?<!\w|>|\#)(
- [A-Z_0-9]{2,}
- )(?!\w)/x'
- => '<span class="D">$1</span>',
-
- // Keywords
- '/(?<!\w|\$|\%|\@|>)(
- and|or|xor|for|do|while|foreach|as|return|die|exit|if|then|else|
- elseif|new|delete|try|throw|catch|finally|class|function|string|
- array|object|resource|var|bool|boolean|int|integer|float|double|
- real|string|array|global|const|static|public|private|protected|
- published|extends|switch|true|false|null|void|this|self|struct|
- char|signed|unsigned|short|long
- )(?!\w|=")/ix'
- => '<span class="K">$1</span>',
-
- // PHP/Perl-Style Vars: $var, %var, @var
- '/(?<!\w)(
- (\$|\%|\@)(\->|\w)+
- )(?!\w)/ix'
- => '<span class="V">$1</span>'
-
- );
-
- \/\*.*?\*\/|
- \/\/.*?\n|
- \#.[^a-fA-F0-9]+?\n|
- \<\!\-\-[\s\S]+\-\-\>|
- (?<!\\\)".*?(?<!\\\)"|
- (?<!\\\)\'(.*?)(?<!\\\)\'
-
-
- // Paste the comments and strings back in again
-
- // Delete the "Escaped Backslash Workaround Token" (TM)
- // and replace tabs with four spaces.
-
- return '<pre>'.$s.'</pre>' ;
- }
-
- // Regexp-Callback to replace every comment or string with a uniqid and save
- // the matched text in an array
- // This way, strings and comments will be stripped out and wont be processed
- // by the other expressions searching for keywords etc.
- static function replaceId($match) {
-
- // String or Comment?
- SyntaxHighlight::$tokens[$id] = '<span class="C">' . $match[1] . '</span>';
- } else {
- SyntaxHighlight::$tokens[$id] = '<span class="S">' . $match[1] . '</span>';
- }
-
- return $id;
- }
- }
- pre {
- font-family: 'Courier New', 'Bitstream Vera Sans Mono', 'monospace';
- font-size: 9pt;
- border-top: 1px solid #333;
- border-bottom: 1px solid #333;
- padding: 0.4em;
- color: #fff;
- }
- pre span.N{ color:#f2c47f; } /* Numbers */
- pre span.S{ color:#42ff00; } /* Strings */
- pre span.C{ color:#838383; } /* Comments */
- pre span.K{ color:#ff0078; } /* Keywords */
- pre span.V{ color:#70d6ff; } /* Vars */
- pre span.D{ color:#ff9a5d; } /* Defines */
-
echo SyntaxHighlight::process( $your_code );
@GitHub portalzine/UtilityBelt/SyntaxHighlight
BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More
THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More
2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More
Short look back at 2023 This has been a busy and interesting year. I am… Read More
Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More
Archaeology Travel is an online travel guide for people who enjoy exploring the world’s pasts.… Read More