-
Notifications
You must be signed in to change notification settings - Fork 0
/
synParser.php
228 lines (201 loc) · 5.2 KB
/
synParser.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* SynHigh - Syntax Highlight Module
*
* Main Class.
*/
// Root Directory
if (!defined('SYNHIGH_ROOT'))
define('SYNHIGH_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
// Language File Directory
if (!defined('SYNHIGH_LANGDIR'))
define('SYNHIGH_LANGDIR', SYNHIGH_ROOT . 'lang' . DIRECTORY_SEPARATOR);
class synhigh
{
// Source
private $source = '';
// Language
private $lang = '';
// Line Numbers
private $lineNumbers = false;
// Line Begin
private $lineBegin = 0;
// Line End
private $lineEnd = 0;
// Language File Directory
private $langPath = SYNHIGH_LANGDIR;
// Inline Output
private $withinLine = false;
/**
* Constructor
*
* @param string Source
* @param string Language
* @param bool Optional: Line Numbers
* @param integer Optional: Line Where The Source Should Begin
* @param integer Optional: Line Where The Source Should End
*/
function __construct($source = '', $lang = '', $lineNumbers = false, $lineBegin = 0, $lineEnd = 0)
{
$this->setSource($source);
$this->setLang($lang);
$this->setLineNumbers($lineNumbers);
$this->setLineBegin($lineBegin);
$this->setLineEnd($lineEnd);
}
/*
* SETTER
*/
/**
* Sets The Source
*
* @param string Source
* @return void
*/
public function setSource($source)
{
if (is_string($source) && ($source !== '')) {
$this->source = $this->normalizeNewlines($source);
}
}
/**
* Sets The Language
*
* @param string Language
*/
public function setLang($lang)
{
if (is_string($lang) && ($lang !== '')) {
// Präpariert den Sprachnamen
$lang = preg_replace('/[^\w\-]/', '', $lang);
$lang = strtolower($lang);
// Setzt den Sprachnamen
$fileName = $this->langPath . $lang . '.php';
$lang = ucfirst($lang);
$this->lang = $lang . 'Syn';
if (!is_readable($fileName)) {
throw new \Exception('Unable to load the language file!');
}
require_once($fileName);
}
}
/**
* Activates Line Numbers
*
* @param bool
* @return void
*/
public function setLineNumbers($lineNumbers)
{
if (is_bool($lineNumbers)) {
$this->lineNumbers = $lineNumbers;
}
}
/**
* Set Line Begin
*
* @param integer
* @return void
*/
public function setLineBegin($lineBegin)
{
if (is_int($lineBegin)) {
$this->lineBegin = $lineBegin;
}
}
/**
* Set End Line
*
* @param integer
* @return void
*/
public function setLineEnd($lineEnd)
{
if (is_int($lineEnd)) {
$this->lineEnd = $lineEnd;
}
}
/**
* Set Within Line
*
* @param bool
* @return void
*/
public function setWithinLine($withinLine)
{
if (is_bool($withinLine)) {
$this->withinLine = $withinLine;
}
}
/*
* FUNCTIONS
*/
/**
* Converts All Line Breaks To UNIX Format
*
* @param string Source
* @return string Source
*/
function normalizeNewlines($source)
{
$source = str_replace("\r\n", "\n", $source);
$source = str_replace("\r", "\n", $source);
return $source;
}
/**
* Limits The Source
*
* @param integer Line Where The Source Begins
* @param integer Line Where The Source Ends
* @return void
*/
function limitSource()
{
// Split
$sourcetemp = explode("\n", $this->source);
if ($this->lineBegin <= 0 || $this->lineBegin > count($sourcetemp)) {
$this->lineBegin = 0;
} else {
$this->lineBegin -= 1; // Arrays starts with 0
}
// Limit
if ($this->lineBegin > $this->lineEnd || $this->lineEnd > count($sourcetemp)) {
$sourcetemp = array_slice($sourcetemp, $this->lineBegin);
} else {
$sourcetemp = array_slice($sourcetemp, $this->lineBegin, ($this->lineEnd - 1) - (count($sourcetemp) - 1));
}
// Melting
$this->source = implode("\n", $sourcetemp);
}
/**
* Highlights The Source
*
* @return string
*/
function parseCode()
{
// Limit
if ($this->lineEnd || $this->lineBegin) {
$this->limitSource();
}
// Language
$classSyn = new $this->lang();
// Create Output
$output = $classSyn->parse($this->source);
// Within Line
if ($this->withinLine) {
return '<code class="wl">' . $output . '</code>';
}
// Linenumbers
if (!$this->lineNumbers) {
return '<pre><code>' . $output . '</code></pre>';
} else {
$linenb = '';
for ($i = substr_count($output, "\n") + 1; $i > 0; $i--) {
$linenb .= "<span></span>";
}
return '<pre class="ln"><code>' . $output . '<span class="ln">' . $linenb . '</span></code></pre>';
}
}
}
?>