2010-03-22 07:18:47 +08:00
|
|
|
var path = require('path'),
|
2010-06-05 00:34:41 +08:00
|
|
|
sys = require('sys'),
|
2010-03-22 07:18:47 +08:00
|
|
|
fs = require('fs');
|
2010-03-13 16:34:48 +08:00
|
|
|
|
2010-05-05 12:23:45 +08:00
|
|
|
require.paths.unshift(path.join(__dirname, '..'));
|
2010-03-13 16:34:48 +08:00
|
|
|
|
|
|
|
var less = {
|
2010-06-20 13:33:42 +08:00
|
|
|
version: [1, 0, 20],
|
2010-03-22 07:18:47 +08:00
|
|
|
Parser: require('less/parser').Parser,
|
|
|
|
importer: require('less/parser').importer,
|
2010-03-23 09:34:05 +08:00
|
|
|
tree: require('less/tree'),
|
|
|
|
render: function (input, options, callback) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
if (typeof(options) === 'function') {
|
|
|
|
callback = options, options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
var parser = new(this.Parser)(options),
|
|
|
|
ee;
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
parser.parse(input, function (e, root) {
|
|
|
|
callback(e, root.toCSS());
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ee = new(require('events').EventEmitter);
|
|
|
|
|
|
|
|
process.nextTick(function () {
|
|
|
|
parser.parse(input, function (e, root) {
|
|
|
|
if (e) { ee.emit('error', e) }
|
|
|
|
else { ee.emit('success', root.toCSS()) }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return ee;
|
|
|
|
}
|
2010-04-26 07:39:21 +08:00
|
|
|
},
|
2010-06-19 07:51:29 +08:00
|
|
|
writeError: function (ctx, options) {
|
2010-06-19 07:44:48 +08:00
|
|
|
var message = "";
|
2010-04-26 07:39:21 +08:00
|
|
|
var extract = ctx.extract;
|
2010-06-02 09:28:42 +08:00
|
|
|
var error = [];
|
|
|
|
|
2010-06-19 07:51:29 +08:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
if (options.silent) { return }
|
|
|
|
|
2010-06-19 07:44:48 +08:00
|
|
|
if (!ctx.index && ctx.stack) {
|
|
|
|
return sys.error(ctx.stack);
|
2010-06-12 09:46:24 +08:00
|
|
|
}
|
|
|
|
|
2010-06-02 09:34:56 +08:00
|
|
|
if (typeof(extract[0]) === 'string') {
|
|
|
|
error.push(stylize((ctx.line - 1) + ' ' + extract[0], 'grey'));
|
|
|
|
}
|
|
|
|
|
2010-06-19 08:12:03 +08:00
|
|
|
error.push(ctx.line + ' ' + extract[1].slice(0, ctx.column)
|
2010-06-19 07:46:38 +08:00
|
|
|
+ stylize(stylize(extract[1][ctx.column], 'bold')
|
2010-06-02 09:28:42 +08:00
|
|
|
+ extract[1].slice(ctx.column + 1), 'yellow'));
|
2010-06-02 09:34:56 +08:00
|
|
|
|
|
|
|
if (typeof(extract[2]) === 'string') {
|
|
|
|
error.push(stylize((ctx.line + 1) + ' ' + extract[2], 'grey'));
|
|
|
|
}
|
2010-06-02 09:28:42 +08:00
|
|
|
error = error.join('\n') + '\033[0m\n';
|
2010-04-26 07:39:21 +08:00
|
|
|
|
2010-06-19 07:44:48 +08:00
|
|
|
message += stylize(ctx.message, 'red');
|
|
|
|
ctx.filename && (message += stylize(' in ', 'red') + ctx.filename);
|
|
|
|
|
|
|
|
sys.error(message, error);
|
|
|
|
|
2010-06-19 08:12:03 +08:00
|
|
|
if (ctx.callLine) {
|
|
|
|
sys.error(stylize('from ', 'red') + (ctx.filename || ''));
|
|
|
|
sys.error(stylize(ctx.callLine, 'grey') + ' ' + ctx.callExtract);
|
|
|
|
}
|
|
|
|
if (ctx.stack) { sys.error(stylize(ctx.stack, 'red')) }
|
2010-03-23 09:34:05 +08:00
|
|
|
}
|
2010-03-13 16:34:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
['color', 'directive', 'operation', 'dimension',
|
|
|
|
'keyword', 'variable', 'ruleset', 'element',
|
|
|
|
'selector', 'quoted', 'expression', 'rule',
|
2010-03-20 05:50:48 +08:00
|
|
|
'call', 'url', 'alpha', 'import',
|
2010-06-16 06:46:09 +08:00
|
|
|
'mixin', 'comment', 'anonymous', 'value'
|
2010-03-13 16:34:48 +08:00
|
|
|
].forEach(function (n) {
|
|
|
|
require(path.join('less', 'tree', n));
|
|
|
|
});
|
|
|
|
|
2010-03-23 11:15:25 +08:00
|
|
|
less.Parser.importer = function (file, paths, callback) {
|
2010-03-22 07:18:47 +08:00
|
|
|
var pathname;
|
|
|
|
|
|
|
|
paths.unshift('.');
|
|
|
|
|
|
|
|
for (var i = 0; i < paths.length; i++) {
|
|
|
|
try {
|
|
|
|
pathname = path.join(paths[i], file);
|
|
|
|
fs.statSync(pathname);
|
|
|
|
break;
|
|
|
|
} catch (e) {
|
|
|
|
pathname = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pathname) {
|
|
|
|
fs.stat(pathname, function (e, stats) {
|
2010-06-19 07:23:56 +08:00
|
|
|
if (e) sys.error(e);
|
2010-03-22 07:18:47 +08:00
|
|
|
|
|
|
|
fs.open(pathname, process.O_RDONLY, stats.mode, function (e, fd) {
|
2010-06-19 07:23:56 +08:00
|
|
|
if (e) sys.error(e);
|
2010-03-22 07:18:47 +08:00
|
|
|
|
|
|
|
fs.read(fd, stats.size, 0, "utf8", function (e, data) {
|
2010-06-19 07:23:56 +08:00
|
|
|
if (e) sys.error(e);
|
2010-03-22 07:18:47 +08:00
|
|
|
|
|
|
|
new(less.Parser)({
|
2010-06-05 01:28:38 +08:00
|
|
|
paths: [path.dirname(pathname)],
|
|
|
|
filename: pathname
|
2010-03-22 07:18:47 +08:00
|
|
|
}).parse(data, function (e, root) {
|
2010-06-05 01:28:38 +08:00
|
|
|
if (e) less.writeError(e);
|
2010-03-23 11:15:25 +08:00
|
|
|
callback(root);
|
2010-03-22 07:18:47 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
2010-06-19 07:23:56 +08:00
|
|
|
sys.error("file '" + file + "' wasn't found.\n");
|
2010-03-22 07:18:47 +08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-13 16:34:48 +08:00
|
|
|
require('less/functions');
|
|
|
|
|
2010-03-19 04:42:25 +08:00
|
|
|
for (var k in less) { exports[k] = less[k] }
|
2010-03-13 16:34:48 +08:00
|
|
|
|
2010-04-26 07:39:21 +08:00
|
|
|
// Stylize a string
|
|
|
|
function stylize(str, style) {
|
|
|
|
var styles = {
|
|
|
|
'bold' : [1, 22],
|
|
|
|
'inverse' : [7, 27],
|
|
|
|
'underline' : [4, 24],
|
|
|
|
'yellow' : [33, 39],
|
|
|
|
'green' : [32, 39],
|
|
|
|
'red' : [31, 39],
|
|
|
|
'grey' : [90, 39]
|
|
|
|
};
|
|
|
|
return '\033[' + styles[style][0] + 'm' + str +
|
|
|
|
'\033[' + styles[style][1] + 'm';
|
|
|
|
}
|
|
|
|
|