1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yaro\LogEnvelope; |
4
|
|
|
|
5
|
|
|
class SyntaxHighlight |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $tokens = array(); |
9
|
|
|
|
10
|
|
|
public static function process($s) |
11
|
|
|
{ |
12
|
|
|
$class = get_called_class(); |
13
|
|
|
$obj = new $class; |
14
|
|
|
|
15
|
|
|
return $obj->highlight($s); |
16
|
|
|
} // end process |
17
|
|
|
|
18
|
|
|
public function highlight($s) |
19
|
|
|
{ |
20
|
|
|
$s = htmlspecialchars($s, ENT_COMPAT); |
21
|
|
|
|
22
|
|
|
// Workaround for escaped backslashes |
23
|
|
|
$s = str_replace('\\\\', '\\\\<e>', $s); |
24
|
|
|
|
25
|
|
|
$regexp = array( |
26
|
|
|
// Numbers (also look for Hex) |
27
|
|
|
'/(?<!\w)( |
28
|
|
|
(0x|\#)[\da-f]+| |
29
|
|
|
\d+| |
30
|
|
|
\d+(px|em|cm|mm|rem|s|\%) |
31
|
|
|
)(?!\w)/ix' |
32
|
|
|
=> '<span style="color:#8CD0D3;">$1</span>', |
33
|
|
|
|
34
|
|
|
// Make the bold assumption that an |
35
|
|
|
// all uppercase word has a special meaning |
36
|
|
|
'/(?<!\w|>|\#)( |
37
|
|
|
[A-Z_0-9]{2,} |
38
|
|
|
)(?!\w)/x' |
39
|
|
|
=> '<span style="color:#FFFFFF">$1</span>', |
40
|
|
|
|
41
|
|
|
// Keywords |
42
|
|
|
'/(?<!\w|\$|\%|\@|>)( |
43
|
|
|
and|or|xor|for|do|while|foreach|as|return|die|exit|if|then|else| |
44
|
|
|
elseif|new|delete|try|throw|catch|finally|class|function|string| |
45
|
|
|
array|object|resource|var|bool|boolean|int|integer|float|double| |
46
|
|
|
real|string|array|global|const|static|public|private|protected| |
47
|
|
|
published|extends|switch|true|false|null|void|this|self|struct| |
48
|
|
|
char|signed|unsigned|short|long |
49
|
|
|
)(?!\w|=")/ix' |
50
|
|
|
=> '<span style="color:#DFC47D">$1</span>', |
51
|
|
|
|
52
|
|
|
// PHP/Perl-Style Vars: $var, %var, @var |
53
|
|
|
'/(?<!\w)( |
54
|
|
|
(\$|\%|\@)(\->|\w)+ |
55
|
|
|
)(?!\w)/ix' |
56
|
|
|
=> '<span style="color:#CEDF99">$1</span>', |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
// Comments/Strings |
60
|
|
|
$s = preg_replace_callback('/( |
61
|
|
|
\/\*.*?\*\/| |
62
|
|
|
\/\/.*?\n| |
63
|
|
|
\#.[^a-fA-F0-9]+?\n| |
64
|
|
|
\<\!\-\-[\s\S]+\-\-\>| |
65
|
|
|
(?<!\\\)".*?(?<!\\\)"| |
66
|
|
|
(?<!\\\)\'(.*?)(?<!\\\)\' |
67
|
|
|
)/isx', [$this, 'replaceId'], $s); |
68
|
|
|
|
69
|
|
|
$s = preg_replace(array_keys($regexp), array_values($regexp), $s); |
70
|
|
|
|
71
|
|
|
// Paste the comments and strings back in again |
72
|
|
|
$s = str_replace(array_keys($this->tokens), array_values($this->tokens), $s); |
73
|
|
|
|
74
|
|
|
// Delete the "Escaped Backslash Workaround Token" (TM) |
75
|
|
|
// and replace tabs with four spaces. |
76
|
|
|
$s = str_replace(array('<e>', "\t"), array('', ' '), $s); |
77
|
|
|
|
78
|
|
|
return $s; |
79
|
|
|
} // end highlight |
80
|
|
|
|
81
|
|
|
/* |
82
|
|
|
* Regexp-Callback to replace every comment or string with a uniqid and save |
83
|
|
|
* the matched text in an array |
84
|
|
|
* This way, strings and comments will be stripped out and wont be processed |
85
|
|
|
* by the other expressions searching for keywords etc. |
86
|
|
|
*/ |
87
|
|
|
private function replaceId($matches) |
88
|
|
|
{ |
89
|
|
|
$match = $matches[0]; |
90
|
|
|
$id = "##r" . uniqid() . "##"; |
91
|
|
|
|
92
|
|
|
// String or Comment? |
93
|
|
|
if (substr($match, 0, 2) == '//' || substr($match, 0, 2) == '/*' || substr($match, 0, 2) == '##' || substr($match, 0, 7) == '<!--') { |
94
|
|
|
$this->tokens[$id] = '<span style="color:#7F9F7F">' . $match . '</span>'; |
95
|
|
|
} else { |
96
|
|
|
$this->tokens[$id] = '<span style="color:#CC9385">' . $match . '</span>'; |
97
|
|
|
} |
98
|
|
|
return $id; |
99
|
|
|
} // end replaceId |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|