Last active 7 months ago

portalzine's Avatar portalzine revised this gist 7 months ago. Go to revision

2 files changed, 91 insertions

Syntax Highlighting Class.css (file created)

@@ -0,0 +1,14 @@
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 (file created)

@@ -0,0 +1,77 @@
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 + (?<!\\\)".*?(?<!\\\)"|
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 );
Newer Older