Syntax Highlighting Class.css
· 445 B · CSS
Raw
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 */
| 1 | pre { |
| 2 | font-family: 'Courier New', 'Bitstream Vera Sans Mono', 'monospace'; |
| 3 | font-size: 9pt; |
| 4 | border-top: 1px solid #333; |
| 5 | border-bottom: 1px solid #333; |
| 6 | padding: 0.4em; |
| 7 | color: #fff; |
| 8 | } |
| 9 | pre span.N{ color:#f2c47f; } /* Numbers */ |
| 10 | pre span.S{ color:#42ff00; } /* Strings */ |
| 11 | pre span.C{ color:#838383; } /* Comments */ |
| 12 | pre span.K{ color:#ff0078; } /* Keywords */ |
| 13 | pre span.V{ color:#70d6ff; } /* Vars */ |
| 14 | pre span.D{ color:#ff9a5d; } /* Defines */ |
Syntax Highlighting Class.php
· 3.4 KiB · PHP
Raw
class SyntaxHighlight {
static $tokens = array();// This array will be filled from the regexp-callback
public static function process($s) {
$s = htmlspecialchars($s);
// Workaround for escaped backslashes
$s = str_replace('\\\\','\\\\<e>', $s);
$regexp = array(
// 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>'
);
$s = preg_replace_callback( '/(
\/\*.*?\*\/|
\/\/.*?\n|
\#.[^a-fA-F0-9]+?\n|
\<\!\-\-[\s\S]+\-\-\>|
(?<!\\\)&quot;.*?(?<!\\\)&quot;|
(?<!\\\)\'(.*?)(?<!\\\)\'
)/isx' , array('SyntaxHighlight', 'replaceId'),$s);
$s = preg_replace(array_keys($regexp), array_values($regexp), $s);
// Paste the comments and strings back in again
$s = str_replace(array_keys(SyntaxHighlight::$tokens), array_values(SyntaxHighlight::$tokens), $s);
// Delete the "Escaped Backslash Workaround Token" (TM)
// and replace tabs with four spaces.
$s = str_replace(array('<e>', "\t"), array('', ' '), $s);
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) {
$id = "##r" . uniqid() . "##";
// String or Comment?
if(substr($match[1], 0, 2) == '//' || substr($match[1], 0, 2) == '/*' || substr($match[1], 0, 2) == '##' || substr($match[1], 0, 7) == '<!--') {
SyntaxHighlight::$tokens[$id] = '<span class="C">' . $match[1] . '</span>';
} else {
SyntaxHighlight::$tokens[$id] = '<span class="S">' . $match[1] . '</span>';
}
return $id;
}
}
echo SyntaxHighlight::process( $your_code );
| 1 | class SyntaxHighlight { |
| 2 | |
| 3 | static $tokens = array();// This array will be filled from the regexp-callback |
| 4 | public static function process($s) { |
| 5 | $s = htmlspecialchars($s); |
| 6 | // Workaround for escaped backslashes |
| 7 | $s = str_replace('\\\\','\\\\<e>', $s); |
| 8 | $regexp = array( |
| 9 | // Punctuations |
| 10 | '/([\-\!\%\^\*\(\)\+\|\~\=\`\{\}\[\]\:\"\'<>\?\,\.\/]+)/' |
| 11 | => '<span class="P">$1</span>', |
| 12 | // Numbers (also look for Hex) |
| 13 | '/(?<!\w)( |
| 14 | (0x|\#)[\da-f]+| |
| 15 | \d+| |
| 16 | \d+(px|em|cm|mm|rem|s|\%) |
| 17 | )(?!\w)/ix' |
| 18 | => '<span class="N">$1</span>', |
| 19 | // Make the bold assumption that an |
| 20 | // all uppercase word has a special meaning |
| 21 | '/(?<!\w|>|\#)( |
| 22 | [A-Z_0-9]{2,} |
| 23 | )(?!\w)/x' |
| 24 | => '<span class="D">$1</span>', |
| 25 | // Keywords |
| 26 | '/(?<!\w|\$|\%|\@|>)( |
| 27 | and|or|xor|for|do|while|foreach|as|return|die|exit|if|then|else| |
| 28 | elseif|new|delete|try|throw|catch|finally|class|function|string| |
| 29 | array|object|resource|var|bool|boolean|int|integer|float|double| |
| 30 | real|string|array|global|const|static|public|private|protected| |
| 31 | published|extends|switch|true|false|null|void|this|self|struct| |
| 32 | char|signed|unsigned|short|long |
| 33 | )(?!\w|=")/ix' |
| 34 | => '<span class="K">$1</span>', |
| 35 | // PHP/Perl-Style Vars: $var, %var, @var |
| 36 | '/(?<!\w)( |
| 37 | (\$|\%|\@)(\->|\w)+ |
| 38 | )(?!\w)/ix' |
| 39 | => '<span class="V">$1</span>' |
| 40 | ); |
| 41 | |
| 42 | $s = preg_replace_callback( '/( |
| 43 | \/\*.*?\*\/| |
| 44 | \/\/.*?\n| |
| 45 | \#.[^a-fA-F0-9]+?\n| |
| 46 | \<\!\-\-[\s\S]+\-\-\>| |
| 47 | (?<!\\\)&quot;.*?(?<!\\\)&quot;| |
| 48 | (?<!\\\)\'(.*?)(?<!\\\)\' |
| 49 | )/isx' , array('SyntaxHighlight', 'replaceId'),$s); |
| 50 | |
| 51 | $s = preg_replace(array_keys($regexp), array_values($regexp), $s); |
| 52 | // Paste the comments and strings back in again |
| 53 | $s = str_replace(array_keys(SyntaxHighlight::$tokens), array_values(SyntaxHighlight::$tokens), $s); |
| 54 | // Delete the "Escaped Backslash Workaround Token" (TM) |
| 55 | // and replace tabs with four spaces. |
| 56 | $s = str_replace(array('<e>', "\t"), array('', ' '), $s); |
| 57 | return '<pre>'.$s.'</pre>' ; |
| 58 | } |
| 59 | // Regexp-Callback to replace every comment or string with a uniqid and save |
| 60 | // the matched text in an array |
| 61 | // This way, strings and comments will be stripped out and wont be processed |
| 62 | // by the other expressions searching for keywords etc. |
| 63 | static function replaceId($match) { |
| 64 | $id = "##r" . uniqid() . "##"; |
| 65 | |
| 66 | // String or Comment? |
| 67 | if(substr($match[1], 0, 2) == '//' || substr($match[1], 0, 2) == '/*' || substr($match[1], 0, 2) == '##' || substr($match[1], 0, 7) == '<!--') { |
| 68 | SyntaxHighlight::$tokens[$id] = '<span class="C">' . $match[1] . '</span>'; |
| 69 | } else { |
| 70 | SyntaxHighlight::$tokens[$id] = '<span class="S">' . $match[1] . '</span>'; |
| 71 | } |
| 72 | |
| 73 | return $id; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | echo SyntaxHighlight::process( $your_code ); |