/
highlighter.php
113 lines (95 loc) · 3.37 KB
/
highlighter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<html>
<head>
<title>PHP Source Highlighter</title>
<style type="text/css">
body { color: #eee; background-color: #0a0a0a; }
pre { font: 400 14px 'Consolas', 'Bitstream Vera Sans Mono', 'Druid Mono', monospace; color: #fff; }
span.string { color: #6df; }
span.variable { color: #fd3; }
span.constant { color: #f50; }
span.modifier { color: #fa7; font-style: italic; }
span.storage { color: #fa7; font-weight: 800; }
span.comment { color: #aaa; }
</style>
</head>
<body>
<?php
/**
* Formats source code with HTML markup for syntax highlighting with CSS.
*
* @author Adrian Unger <unger.adrian@gmail.com>
* @link http://staydecent.ca
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @version 0.0.2
*/
class Highlighter {
private $defined_functions;
function __construct() {
$this->defined_functions = get_defined_functions();
}
function format( $file, $lang = 'php' ) {
// Read from file or string
if( is_file($file) ) {
$source = strip_tags(file_get_contents($file));
}
else {
$source = strip_tags($file);
}
// Convert line-breaks
$source = preg_replace('/\r\n|\r/', "\r\n", $source);
// Read line by line
$lines = explode("\n", $source);
$n_lines = count($lines);
for( $i = 0; $i < $n_lines; ++$i ) {
$first_char = substr(trim($lines[$i]), 0, 1);
if( $first_char == '/' || $first_char == '*' || $first_char == '#' ) {
$lines[$i] = '<span class="comment">' . $lines[$i] . '</span>';
}
else {
// Strings(quoted)
$lines[$i] = preg_replace('/(".*?"|\'.*?\')/', '<span class="string">$1</span>', $lines[$i]);
// Variables
$lines[$i] = preg_replace('/(\$[a-zA-z0-9]*|->[a-zA-z0-9]*)\b/', '<span class="variable">$1</span>', $lines[$i]);
// Constant
$lines[$i] = preg_replace('/(__[a-zA-Z0-9]*_+|\$_+[A-Z]*)/', '<span class="constant">$1</span>', $lines[$i]);
// Storage
$lines[$i] = preg_replace('/(class|function|new|public|private|protected|var|interface) /', '<span class="storage">$1</span> ', $lines[$i]);
// Storage Modifier
$lines[$i] = preg_replace('/(abstract|extends|implements) /', '<span class="modifier">$1</span> ', $lines[$i]);
}
}
$output = '';
foreach( $lines as $line ) {
if( !empty($line) ) {
$output .= $line . "\r\n";
}
}
return "<pre class=\"$lang\">\n" . $output . "</pre>\n";
}
}
$hl = new Highlighter;
echo $hl->format('
/**
* Class Name
* Description
* $var
* @PHPDoc tags
*/
class ClassName extends AnotherClass
{
function __construct($param)
{
# code...
$classclassclassfunction
//functionclass
$this->route = (empty($_GET[\'route\'])) ? $this->c_default : trim($_GET[\'route\'], \'/\\\');
}
public function FunctionName($value)
{
echo __FILE__, "String";
}
}
');
?>
</body>
</html>