tizen beta release
[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 require.paths.unshift(path.join(__dirname, '..'));
6
7 var less = {
8     version: [1, 1, 3],
9     Parser: require('less/parser').Parser,
10     importer: require('less/parser').importer,
11     tree: require('less/tree'),
12     render: function (input, options, callback) {
13         options = options || {};
14
15         if (typeof(options) === 'function') {
16             callback = options, options = {};
17         }
18
19         var parser = new(this.Parser)(options),
20             ee;
21
22         if (callback) {
23             parser.parse(input, function (e, root) {
24                 callback(e, root.toCSS(options));
25             });
26         } else {
27             ee = new(require('events').EventEmitter);
28
29             process.nextTick(function () {
30                 parser.parse(input, function (e, root) {
31                     if (e) { ee.emit('error', e) }
32                     else   { ee.emit('success', root.toCSS(options)) }
33                 });
34             });
35             return ee;
36         }
37     },
38     writeError: function (ctx, options) {
39         var message = "";
40         var extract = ctx.extract;
41         var error = [];
42         var stylize = options.color ? less.stylize : function (str) { return str };
43
44         options = options || {};
45
46         if (options.silent) { return }
47
48         if (!ctx.index) {
49             return sys.error(ctx.stack || ctx.message);
50         }
51
52         if (typeof(extract[0]) === 'string') {
53             error.push(stylize((ctx.line - 1) + ' ' + extract[0], 'grey'));
54         }
55
56         error.push(ctx.line + ' ' + extract[1].slice(0, ctx.column)
57                             + stylize(stylize(extract[1][ctx.column], 'bold')
58                             + extract[1].slice(ctx.column + 1), 'yellow'));
59
60         if (typeof(extract[2]) === 'string') {
61             error.push(stylize((ctx.line + 1) + ' ' + extract[2], 'grey'));
62         }
63         error = error.join('\n') + '\033[0m\n';
64
65         message += stylize(ctx.message, 'red');
66         ctx.filename && (message += stylize(' in ', 'red') + ctx.filename);
67
68         sys.error(message, error);
69
70         if (ctx.callLine) {
71             sys.error(stylize('from ', 'red')       + (ctx.filename || ''));
72             sys.error(stylize(ctx.callLine, 'grey') + ' ' + ctx.callExtract);
73         }
74         if (ctx.stack) { sys.error(stylize(ctx.stack, 'red')) }
75     }
76 };
77
78 ['color',    'directive', 'operation',  'dimension',
79  'keyword',  'variable',  'ruleset',    'element',
80  'selector', 'quoted',    'expression', 'rule',
81  'call',     'url',       'alpha',      'import',
82  'mixin',    'comment',   'anonymous',  'value', 'javascript'
83 ].forEach(function (n) {
84     require(path.join('less', 'tree', n));
85 });
86
87 less.Parser.importer = function (file, paths, callback) {
88     var pathname;
89
90     paths.unshift('.');
91
92     for (var i = 0; i < paths.length; i++) {
93         try {
94             pathname = path.join(paths[i], file);
95             fs.statSync(pathname);
96             break;
97         } catch (e) {
98             pathname = null;
99         }
100     }
101
102     if (pathname) {
103         fs.readFile(pathname, 'utf-8', function(e, data) {
104           if (e) sys.error(e);
105
106           new(less.Parser)({
107               paths: [path.dirname(pathname)].concat(paths),
108               filename: pathname
109           }).parse(data, function (e, root) {
110               if (e) less.writeError(e);
111               callback(root);
112           });
113         });
114     } else {
115         sys.error("file '" + file + "' wasn't found.\n");
116         process.exit(1);
117     }
118 }
119
120 require('less/functions');
121
122 for (var k in less) { exports[k] = less[k] }
123
124 // Stylize a string
125 function stylize(str, style) {
126     var styles = {
127         'bold'      : [1,  22],
128         'inverse'   : [7,  27],
129         'underline' : [4,  24],
130         'yellow'    : [33, 39],
131         'green'     : [32, 39],
132         'red'       : [31, 39],
133         'grey'      : [90, 39]
134     };
135     return '\033[' + styles[style][0] + 'm' + str +
136            '\033[' + styles[style][1] + 'm';
137 }
138 less.stylize = stylize;
139