1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var nodes = require('./nodes')
|
13 | , basename = require('path').basename
|
14 | , relative = require('path').relative
|
15 | , join = require('path').join
|
16 | , isAbsolute = require('path').isAbsolute
|
17 | , glob = require('glob')
|
18 | , fs = require('fs');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | exports.absolute = isAbsolute || function(path){
|
29 |
|
30 |
|
31 | return path.substr(0, 2) == '\\\\' || '/' === path.charAt(0) || /^[a-z]:[\\\/]/i.test(path);
|
32 | };
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | exports.lookup = function(path, paths, ignore){
|
46 | var lookup
|
47 | , i = paths.length;
|
48 |
|
49 |
|
50 | if (exports.absolute(path)) {
|
51 | try {
|
52 | fs.statSync(path);
|
53 | return path;
|
54 | } catch (err) {
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | }
|
60 | }
|
61 |
|
62 |
|
63 | while (i--) {
|
64 | try {
|
65 | lookup = join(paths[i], path);
|
66 | if (ignore == lookup) continue;
|
67 | fs.statSync(lookup);
|
68 | return lookup;
|
69 | } catch (err) {
|
70 |
|
71 | }
|
72 | }
|
73 | };
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | exports.find = function(path, paths, ignore) {
|
85 | var lookup
|
86 | , found
|
87 | , i = paths.length;
|
88 |
|
89 |
|
90 | if (exports.absolute(path)) {
|
91 | if ((found = glob.sync(path)).length) {
|
92 | return found;
|
93 | }
|
94 | }
|
95 |
|
96 |
|
97 | while (i--) {
|
98 | lookup = join(paths[i], path);
|
99 | if (ignore == lookup) continue;
|
100 | if ((found = glob.sync(lookup)).length) {
|
101 | return found;
|
102 | }
|
103 | }
|
104 | };
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | exports.lookupIndex = function(name, paths, filename){
|
115 |
|
116 | var found = exports.find(join(name, 'index.styl'), paths, filename);
|
117 | if (!found) {
|
118 |
|
119 | found = exports.find(join(name, basename(name).replace(/\.styl/i, '') + '.styl'), paths, filename);
|
120 | }
|
121 | if (!found && !~name.indexOf('node_modules')) {
|
122 |
|
123 | found = lookupPackage(join('node_modules', name));
|
124 | }
|
125 | return found;
|
126 |
|
127 | function lookupPackage(dir) {
|
128 | var pkg = exports.lookup(join(dir, 'package.json'), paths, filename);
|
129 | if (!pkg) {
|
130 | return /\.styl$/i.test(dir) ? exports.lookupIndex(dir, paths, filename) : lookupPackage(dir + '.styl');
|
131 | }
|
132 | var main = require(relative(__dirname, pkg)).main;
|
133 | if (main) {
|
134 | found = exports.find(join(dir, main), paths, filename);
|
135 | } else {
|
136 | found = exports.lookupIndex(dir, paths, filename);
|
137 | }
|
138 | return found;
|
139 | }
|
140 | };
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | exports.formatException = function(err, options){
|
160 | var lineno = options.lineno
|
161 | , column = options.column
|
162 | , filename = options.filename
|
163 | , str = options.input
|
164 | , context = options.context || 8
|
165 | , context = context / 2
|
166 | , lines = ('\n' + str).split('\n')
|
167 | , start = Math.max(lineno - context, 1)
|
168 | , end = Math.min(lines.length, lineno + context)
|
169 | , pad = end.toString().length;
|
170 |
|
171 | var context = lines.slice(start, end).map(function(line, i){
|
172 | var curr = i + start;
|
173 | return ' '
|
174 | + Array(pad - curr.toString().length + 1).join(' ')
|
175 | + curr
|
176 | + '| '
|
177 | + line
|
178 | + (curr == lineno
|
179 | ? '\n' + Array(curr.toString().length + 5 + column).join('-') + '^'
|
180 | : '');
|
181 | }).join('\n');
|
182 |
|
183 | err.message = filename
|
184 | + ':' + lineno
|
185 | + ':' + column
|
186 | + '\n' + context
|
187 | + '\n\n' + err.message + '\n'
|
188 | + (err.stylusStack ? err.stylusStack + '\n' : '');
|
189 |
|
190 |
|
191 | if (err.fromStylus) err.stack = 'Error: ' + err.message;
|
192 |
|
193 | return err;
|
194 | };
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | exports.assertType = function(node, type, param){
|
206 | exports.assertPresent(node, param);
|
207 | if (node.nodeName == type) return;
|
208 | var actual = node.nodeName
|
209 | , msg = 'expected '
|
210 | + (param ? '"' + param + '" to be a ' : '')
|
211 | + type + ', but got '
|
212 | + actual + ':' + node;
|
213 | throw new Error('TypeError: ' + msg);
|
214 | };
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | exports.assertString = function(node, param){
|
225 | exports.assertPresent(node, param);
|
226 | switch (node.nodeName) {
|
227 | case 'string':
|
228 | case 'ident':
|
229 | case 'literal':
|
230 | return;
|
231 | default:
|
232 | var actual = node.nodeName
|
233 | , msg = 'expected string, ident or literal, but got ' + actual + ':' + node;
|
234 | throw new Error('TypeError: ' + msg);
|
235 | }
|
236 | };
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | exports.assertColor = function(node, param){
|
247 | exports.assertPresent(node, param);
|
248 | switch (node.nodeName) {
|
249 | case 'rgba':
|
250 | case 'hsla':
|
251 | return;
|
252 | default:
|
253 | var actual = node.nodeName
|
254 | , msg = 'expected rgba or hsla, but got ' + actual + ':' + node;
|
255 | throw new Error('TypeError: ' + msg);
|
256 | }
|
257 | };
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | exports.assertPresent = function(node, name){
|
268 | if (node) return;
|
269 | if (name) throw new Error('"' + name + '" argument required');
|
270 | throw new Error('argument missing');
|
271 | };
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 | exports.unwrap = function(expr){
|
285 |
|
286 | if (expr.preserve) return expr;
|
287 | if ('arguments' != expr.nodeName && 'expression' != expr.nodeName) return expr;
|
288 | if (1 != expr.nodes.length) return expr;
|
289 | if ('arguments' != expr.nodes[0].nodeName && 'expression' != expr.nodes[0].nodeName) return expr;
|
290 | return exports.unwrap(expr.nodes[0]);
|
291 | };
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | exports.coerce = function(val, raw){
|
303 | switch (typeof val) {
|
304 | case 'function':
|
305 | return val;
|
306 | case 'string':
|
307 | return new nodes.String(val);
|
308 | case 'boolean':
|
309 | return new nodes.Boolean(val);
|
310 | case 'number':
|
311 | return new nodes.Unit(val);
|
312 | default:
|
313 | if (null == val) return nodes.null;
|
314 | if (Array.isArray(val)) return exports.coerceArray(val, raw);
|
315 | if (val.nodeName) return val;
|
316 | return exports.coerceObject(val, raw);
|
317 | }
|
318 | };
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | exports.coerceArray = function(val, raw){
|
330 | var expr = new nodes.Expression;
|
331 | val.forEach(function(val){
|
332 | expr.push(exports.coerce(val, raw));
|
333 | });
|
334 | return expr;
|
335 | };
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | exports.coerceObject = function(obj, raw){
|
351 | var node = raw ? new nodes.Object : new nodes.Expression
|
352 | , val;
|
353 |
|
354 | for (var key in obj) {
|
355 | val = exports.coerce(obj[key], raw);
|
356 | key = new nodes.Ident(key);
|
357 | if (raw) {
|
358 | node.set(key, val);
|
359 | } else {
|
360 | node.push(exports.coerceArray([key, val]));
|
361 | }
|
362 | }
|
363 |
|
364 | return node;
|
365 | };
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | exports.params = function(fn){
|
376 | return fn
|
377 | .toString()
|
378 | .match(/\(([^)]*)\)/)[1].split(/ *, */);
|
379 | };
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 | exports.merge = function(a, b, deep) {
|
391 | for (var k in b) {
|
392 | if (deep && a[k]) {
|
393 | var nodeA = exports.unwrap(a[k]).first
|
394 | , nodeB = exports.unwrap(b[k]).first;
|
395 |
|
396 | if ('object' == nodeA.nodeName && 'object' == nodeB.nodeName) {
|
397 | a[k].first.vals = exports.merge(nodeA.vals, nodeB.vals, deep);
|
398 | } else {
|
399 | a[k] = b[k];
|
400 | }
|
401 | } else {
|
402 | a[k] = b[k];
|
403 | }
|
404 | }
|
405 | return a;
|
406 | };
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 | exports.uniq = function(arr){
|
417 | var obj = {}
|
418 | , ret = [];
|
419 |
|
420 | for (var i = 0, len = arr.length; i < len; ++i) {
|
421 | if (arr[i] in obj) continue;
|
422 |
|
423 | obj[arr[i]] = true;
|
424 | ret.push(arr[i]);
|
425 | }
|
426 | return ret;
|
427 | };
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 | exports.compileSelectors = function(arr, leaveHidden){
|
451 | var selectors = []
|
452 | , Parser = require('./selector-parser')
|
453 | , indent = (this.indent || '')
|
454 | , buf = [];
|
455 |
|
456 | function parse(selector, buf) {
|
457 | var parts = [selector.val]
|
458 | , str = new Parser(parts[0], parents, parts).parse().val
|
459 | , parents = [];
|
460 |
|
461 | if (buf.length) {
|
462 | for (var i = 0, len = buf.length; i < len; ++i) {
|
463 | parts.push(buf[i]);
|
464 | parents.push(str);
|
465 | var child = new Parser(buf[i], parents, parts).parse();
|
466 |
|
467 | if (child.nested) {
|
468 | str += ' ' + child.val;
|
469 | } else {
|
470 | str = child.val;
|
471 | }
|
472 | }
|
473 | }
|
474 | return str.trim();
|
475 | }
|
476 |
|
477 | function compile(arr, i) {
|
478 | if (i) {
|
479 | arr[i].forEach(function(selector){
|
480 | if (!leaveHidden && selector.isPlaceholder) return;
|
481 | if (selector.inherits) {
|
482 | buf.unshift(selector.val);
|
483 | compile(arr, i - 1);
|
484 | buf.shift();
|
485 | } else {
|
486 | selectors.push(indent + parse(selector, buf));
|
487 | }
|
488 | });
|
489 | } else {
|
490 | arr[0].forEach(function(selector){
|
491 | if (!leaveHidden && selector.isPlaceholder) return;
|
492 | var str = parse(selector, buf);
|
493 | if (str) selectors.push(indent + str);
|
494 | });
|
495 | }
|
496 | }
|
497 |
|
498 | compile(arr, arr.length - 1);
|
499 |
|
500 |
|
501 | return exports.uniq(selectors);
|
502 | };
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 | exports.parseString = function(str){
|
513 | var Parser = require('./parser')
|
514 | , parser
|
515 | , ret;
|
516 |
|
517 | try {
|
518 | parser = new Parser(str);
|
519 | ret = parser.list();
|
520 | } catch (e) {
|
521 | ret = new nodes.Literal(str);
|
522 | }
|
523 | return ret;
|
524 | };
|