1 var path = require('path'),
6 // For old node.js versions
7 require.paths.unshift( path.join( __dirname, '..' ) );
13 Parser: require('less/parser').Parser,
14 importer: require('less/parser').importer,
15 tree: require('less/tree'),
16 render: function (input, options, callback) {
17 options = options || {};
19 if (typeof(options) === 'function') {
20 callback = options, options = {};
23 var parser = new(this.Parser)(options),
27 parser.parse(input, function (e, root) {
28 callback(e, root.toCSS(options));
31 ee = new(require('events').EventEmitter);
33 process.nextTick(function () {
34 parser.parse(input, function (e, root) {
35 if (e) { ee.emit('error', e) }
36 else { ee.emit('success', root.toCSS(options)) }
42 writeError: function (ctx, options) {
44 var extract = ctx.extract;
46 var stylize = options.color ? less.stylize : function (str) { return str };
48 options = options || {};
50 if (options.silent) { return }
53 return sys.error(ctx.stack || ctx.message);
56 if (typeof(extract[0]) === 'string') {
57 error.push(stylize((ctx.line - 1) + ' ' + extract[0], 'grey'));
60 error.push(ctx.line + ' ' + extract[1].slice(0, ctx.column)
61 + stylize(stylize(extract[1][ctx.column], 'bold')
62 + extract[1].slice(ctx.column + 1), 'yellow'));
64 if (typeof(extract[2]) === 'string') {
65 error.push(stylize((ctx.line + 1) + ' ' + extract[2], 'grey'));
67 error = error.join('\n') + '\033[0m\n';
69 message += stylize(ctx.message, 'red');
70 ctx.filename && (message += stylize(' in ', 'red') + ctx.filename);
72 sys.error(message, error);
75 sys.error(stylize('from ', 'red') + (ctx.filename || ''));
76 sys.error(stylize(ctx.callLine, 'grey') + ' ' + ctx.callExtract);
78 if (ctx.stack) { sys.error(stylize(ctx.stack, 'red')) }
82 ['color', 'directive', 'operation', 'dimension',
83 'keyword', 'variable', 'ruleset', 'element',
84 'selector', 'quoted', 'expression', 'rule',
85 'call', 'url', 'alpha', 'import',
86 'mixin', 'comment', 'anonymous', 'value', 'javascript'
87 ].forEach(function (n) {
88 require(path.join('less', 'tree', n));
91 less.Parser.importer = function (file, paths, callback) {
96 for (var i = 0; i < paths.length; i++) {
98 pathname = path.join(paths[i], file);
99 fs.statSync(pathname);
107 fs.readFile(pathname, 'utf-8', function(e, data) {
111 paths: [path.dirname(pathname)].concat(paths),
113 }).parse(data, function (e, root) {
114 if (e) less.writeError(e);
119 sys.error("file '" + file + "' wasn't found.\n");
124 require('less/functions');
126 for (var k in less) { exports[k] = less[k] }
129 function stylize(str, style) {
133 'underline' : [4, 24],
139 return '\033[' + styles[style][0] + 'm' + str +
140 '\033[' + styles[style][1] + 'm';
142 less.stylize = stylize;