This repository has been archived by the owner on Jul 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oo_css.php
executable file
·355 lines (318 loc) · 13.9 KB
/
oo_css.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
define('DEBUG', false);
define('WARN', true);
/**
* @access public
* @author Dan Beam <[email protected]>
* A simple CSS lexer / translator from OO CSS -> CSS
*/
class OO_CSS_Parser {
/**
* @access protected
* Various formatting aspects of our output
*/
protected static $format = array(
'id' => "default",
'b1' => "%s {\n ",
'r' => "\n ",
'b2' => "\n}\n",
'm' => ", ",
'd' => '/[;{},]/',
's' => ': ',
);
/**
* @method OO_CSS_Parser::__construct
* @access public
* Instantiation and configuration
*/
public function __construct($style = '1tbs') {
$format =& self::$format;
switch ($style) {
case 'minified': case 'min': case 'minned':
$format = array(
'id' => "minned",
'b1' => "%s{",
'r' => "",
'b2' => "}",
'm' => ",",
'd' => '/[:();{},]/',
's' => ':',
);
break;
case 'allman':
$format = array(
'id' => "allman",
'b1' => "%s\n{\n ",
'r' => "\n ",
'b2' => "\n}\n",
'm' => ", ",
'd' => '/[;{},]/',
's' => ': ',
);
break;
case 'oneline': case 'one-line': case 'oneliner': case 'one-liner':
$format = array(
'id' => "oneline",
'b1' => "%s { ",
'r' => " ",
'b2' => " }\n",
'm' => ", ",
'd' => '/[;{},]/',
's' => ': ',
);
break;
case '0tbs': case '1tbs': default:
// stick with default
break;
}
}
/**
* @method OO_CSS_Parser::debug
* @access public
* For generic debug messages that require higher verbosity
*/
public function debug ($msg = "") {
echo "$msg\n";
return $this;
}
/**
* @method OO_CSS_Parser::warn
* @access public
* For warnings like files not being found or readable
*/
public function warn ($msg = "") {
file_put_contents('php://stderr', "$msg\n", FILE_APPEND);
return $this;
}
/**
* @method OO_CSS_Parser::flatten
* @access protected
* @param possibly multi-dimensional array
* @return flattened array
* This is a helper method that flattens all the arguments given to the parse() method at the moment
*/
protected function flatten ($args, &$new = array()) {
// recursively iterate through args
foreach ($args as $arg) {
// recurse
if (is_array($arg)) {
self::flatten($arg, $new);
}
// base case
else {
$new[] = $arg;
}
}
// return flattened array
return $new;
}
/**
* @method OO_CSS_Parser::parse
* @access protected
* @param any kind of multi-dimensional array or any number of paramaters
* @return un-Object-Oriented CSS for use in browsers
* Returns the output of the de-OO-ing of the OO CSS
*/
public function parse () {
// flatten args to allow different ways of calling thi method
$files = self::flatten(func_get_args());
// if no files, what to do?
if (empty($files)) {
if (WARN) self::warn("No files given"); exit(1);
}
// set up global to hold rendered content
$results = array();
// set up alias for convenience
$format =& self::$format;
// this will work with only 1 file, too
foreach($files as $file) {
if (is_file($file)) {
if (is_readable($file)) {
// set up some primitives to help us out
$line_num = 0;
$token = $prev = $rules = $value = '';
$rule_stack = $block = array();
$in_comment = false;
// open a handle to our file
$handle = fopen($file, 'r');
// old school way
while (!feof($handle)) {
// read a char at a time
$token .= ($char = fread($handle, 1));
//if (DEBUG) {
// self::debug("char: $char ".($in_comment?'(comment)':''));
// self::debug("prev: $prev");
// self::debug("token: $token");
// self::debug("stack: " . print_r($rule_stack, true));
//}
// main loop
switch ($char) {
case '{':
if (!$in_comment) {
if (DEBUG) self::debug("Found start of statement list!");
if (empty($rule_stack)) {
$rule_stack[] = implode($format['m'], array_map('rtrim', array_map('trim', explode(',', substr($token, 0, -1)))));
}
else {
$computed = array();
$parents = array_map('rtrim', array_map('trim', explode(',', end($rule_stack))));
$children = array_map('rtrim', array_map('trim', explode(',', substr($token, 0, -1))));
//var_dump(array('rule_stack' => $rule_stack, 'parents' => $parents, 'children' => $children));
foreach ($parents as $parent) {
foreach ($children as $child) {
$computed[] = $parent.' '.$child;
}
}
$rule_stack[] = implode($format['m'], $computed);
}
$token = '';
}
break;
case '}':
if (!$in_comment) {
if (DEBUG) self::debug("Found end of statement list!");
// check for one last rule without a ; (valid at the end of blocks)
$rule_split = explode(':', $token);
if (isset($rule_split[1])) {
$rules = array_map('trim', array_map('rtrim', explode(',', $rule_split[0])));
$value = rtrim(trim(substr($rule_split[1], 0, -1).';'));
$recent_rule = end($rule_stack);
foreach ($rules as $rule) {
$block[$recent_rule][] = array('rule' => $rule, 'value' => $value);
}
}
array_pop($rule_stack);
$token = '';
}
break;
case ';':
if (!$in_comment) {
if (DEBUG) self::debug("Found end of rule!");
if (false === ($colon_pos = strpos($token, ':'))) {
//self::warn(print_r($rule_split, true));
if (WARN) self::warn('Syntax error around line #'.$line_num);
exit(1);
}
$rules = array_map('trim', array_map('rtrim', explode(',', substr($token, 0, $colon_pos))));
$value = rtrim(trim(substr($token, $colon_pos + 1)));
$recent_rule = end($rule_stack);
foreach ($rules as $rule) {
$block[$recent_rule][] = array('rule' => $rule, 'value' => $value);
}
$token = '';
}
break;
case '*':
if (DEBUG) self::debug("Found normal token \"$char\"!");
if ('/' === $prev) {
if (DEBUG) self::debug("Found start of comment!");
$in_comment = true;
}
break;
case '/':
if (DEBUG) self::debug("Found normal token \"$char\"!");
if ('*' === $prev) {
$in_comment = false;
if (DEBUG) self::debug("Found end of comment!");
if ('/**/' !== $token || '/*\*/' !== $token) {
$token = '';
}
}
break;
case ' ': case "\t":
if (!$in_comment) {
if (preg_match($format['d'], $prev)) {
$token = trim($token);
$char = '';
}
}
break;
case "\n": case "\r":
++$line_num;
if (DEBUG) self::debug("On line #$line_num");
if (!$in_comment) {
if (preg_match($format['d'], $prev)) {
$token = rtrim($token);
$char = '';
}
}
break;
default:
if (DEBUG) self::debug("Found normal token \"$char\"!");
break;
}
if (isset($char{0})) {
$prev = $char;
}
}
$result = array();
if (count($files) > 1) {
$result[] = "/* $file */\n\n";
}
foreach ($block as $selector => $statement) {
$rule_map = $rules = array();
// eliminate duplicate rules
foreach ($statement as $rule) {
if (!isset($rule_map[$rule['rule']])) {
$rule_map[$rule['rule']] = array();
}
$rule_map[$rule['rule']][] = $rule['value'];
}
// alphabetize rules (why we needed to de-dupe)
ksort($rule_map);
// .reconstruct-the {inside:"of a block";}
foreach ($rule_map as $attr => $vals) {
foreach ($vals as $val) {
$rules[] = $attr.$format['s'].$val;
}
}
// if minned, take off the last ; in the {block}
if ('minned' === $format['id']) {
$rules[] = substr(array_pop($rules), 0, -1);
}
// if it's .not-empty {}
if (!empty($rules)) {
// add and format the results
$results[] = str_replace(
array('%s', '%b1', '%b2'),
array($selector, $format['b1'], $format['b2']),
$format['b1'] . implode($format['r'], $rules) . $format['b2']
);
}
}
$results[] = implode('', $result);
}
else {
if (WARN) self::warn("$file not readable");
}
}
else {
if (WARN) self::warn("$file not readable");
}
}
if (!empty($results)) {
return implode('', $results);
}
}
}
// I have no idea why $argv (ini setting register_argc_argv) is different from $_SERVER['argv']
$argv_norm = isset($argv) ? array_slice($argv, 1) : array_slice($_SERVER['argv'], 2);
// if we're on the CLI, check for arguments
if ('cli' === php_sapi_name() && count($argv_norm) > 0) {
// get options from CLI args
$args = getopt('s:');
// if we found a style, remove those args
if (isset($args['s'])) {
array_splice($argv_norm, array_search('-s', $argv_norm), 2);
// create an instance
$parser = new OO_CSS_Parser($args['s']);
}
else {
// create an instance
$parser = new OO_CSS_Parser();
}
// output warning to stderr so normal > redirection doesn't work
//if (WARN) $parser->warn('Found arguments on CLI, parsing...');
// parse all arguments passed in on CLI
ob_start(); echo $parser->parse($argv_norm); ob_end_flush();
}