3b4e928dc88f8856ed156d8ccd7d3411fb23768e
[platform/framework/web/web-ui-fw.git] / build-tools / lib / less / index.js
1 var path = require('path'),
2     sys = require('sys'),
3     fs = require('fs');
4
5 try {
6         // For old node.js versions
7         require.paths.unshift( path.join( __dirname, '..' ) );
8 } catch ( ex ) {
9 }
10
11 var less = {
12     version: [1, 1, 3],
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 || {};
18
19         if (typeof(options) === 'function') {
20             callback = options, options = {};
21         }
22
23         var parser = new(this.Parser)(options),
24             ee;
25
26         if (callback) {
27             parser.parse(input, function (e, root) {
28                 callback(e, root.toCSS(options));
29             });
30         } else {
31             ee = new(require('events').EventEmitter);
32
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)) }
37                 });
38             });
39             return ee;
40         }
41     },
42     writeError: function (ctx, options) {
43         var message = "";
44         var extract = ctx.extract;
45         var error = [];
46         var stylize = options.color ? less.stylize : function (str) { return str };
47
48         options = options || {};
49
50         if (options.silent) { return }
51
52         if (!ctx.index) {
53             return sys.error(ctx.stack || ctx.message);
54         }
55
56         if (typeof(extract[0]) === 'string') {
57             error.push(stylize((ctx.line - 1) + ' ' + extract[0], 'grey'));
58         }
59
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'));
63
64         if (typeof(extract[2]) === 'string') {
65             error.push(stylize((ctx.line + 1) + ' ' + extract[2], 'grey'));
66         }
67         error = error.join('\n') + '\033[0m\n';
68
69         message += stylize(ctx.message, 'red');
70         ctx.filename && (message += stylize(' in ', 'red') + ctx.filename);
71
72         sys.error(message, error);
73
74         if (ctx.callLine) {
75             sys.error(stylize('from ', 'red')       + (ctx.filename || ''));
76             sys.error(stylize(ctx.callLine, 'grey') + ' ' + ctx.callExtract);
77         }
78         if (ctx.stack) { sys.error(stylize(ctx.stack, 'red')) }
79     }
80 };
81
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));
89 });
90
91 less.Parser.importer = function (file, paths, callback) {
92     var pathname;
93
94     paths.unshift('.');
95
96     for (var i = 0; i < paths.length; i++) {
97         try {
98             pathname = path.join(paths[i], file);
99             fs.statSync(pathname);
100             break;
101         } catch (e) {
102             pathname = null;
103         }
104     }
105
106     if (pathname) {
107         fs.readFile(pathname, 'utf-8', function(e, data) {
108           if (e) sys.error(e);
109
110           new(less.Parser)({
111               paths: [path.dirname(pathname)].concat(paths),
112               filename: pathname
113           }).parse(data, function (e, root) {
114               if (e) less.writeError(e);
115               callback(root);
116           });
117         });
118     } else {
119         sys.error("file '" + file + "' wasn't found.\n");
120         process.exit(1);
121     }
122 }
123
124 require('less/functions');
125
126 for (var k in less) { exports[k] = less[k] }
127
128 // Stylize a string
129 function stylize(str, style) {
130     var styles = {
131         'bold'      : [1,  22],
132         'inverse'   : [7,  27],
133         'underline' : [4,  24],
134         'yellow'    : [33, 39],
135         'green'     : [32, 39],
136         'red'       : [31, 39],
137         'grey'      : [90, 39]
138     };
139     return '\033[' + styles[style][0] + 'm' + str +
140            '\033[' + styles[style][1] + 'm';
141 }
142 less.stylize = stylize;
143