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