source: Dev/branches/rest-dojo-ui/client/util/less/index.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

  • Property svn:executable set to *
File size: 3.8 KB
Line 
1var path = require('path'),
2    sys = require('sys'),
3    fs = require('fs');
4
5require.paths.unshift(path.join(__dirname, '..'));
6
7var 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
43        options = options || {};
44
45        if (options.silent) { return }
46
47        if (!ctx.index) {
48            return sys.error(ctx.stack || ctx.message);
49        }
50
51        if (typeof(extract[0]) === 'string') {
52            error.push(stylize((ctx.line - 1) + ' ' + extract[0], 'grey'));
53        }
54
55        error.push(ctx.line + ' ' + extract[1].slice(0, ctx.column)
56                            + stylize(stylize(extract[1][ctx.column], 'bold')
57                            + extract[1].slice(ctx.column + 1), 'yellow'));
58
59        if (typeof(extract[2]) === 'string') {
60            error.push(stylize((ctx.line + 1) + ' ' + extract[2], 'grey'));
61        }
62        error = error.join('\n') + '\033[0m\n';
63
64        message += stylize(ctx.message, 'red');
65        ctx.filename && (message += stylize(' in ', 'red') + ctx.filename);
66
67        sys.error(message, error);
68
69        if (ctx.callLine) {
70            sys.error(stylize('from ', 'red')       + (ctx.filename || ''));
71            sys.error(stylize(ctx.callLine, 'grey') + ' ' + ctx.callExtract);
72        }
73        if (ctx.stack) { sys.error(stylize(ctx.stack, 'red')) }
74    }
75};
76
77['color',    'directive', 'operation',  'dimension',
78 'keyword',  'variable',  'ruleset',    'element',
79 'selector', 'quoted',    'expression', 'rule',
80 'call',     'url',       'alpha',      'import',
81 'mixin',    'comment',   'anonymous',  'value', 'javascript'
82].forEach(function (n) {
83    require(path.join('less', 'tree', n));
84});
85
86less.Parser.importer = function (file, paths, callback) {
87    var pathname;
88
89    paths.unshift('.');
90
91    for (var i = 0; i < paths.length; i++) {
92        try {
93            pathname = path.join(paths[i], file);
94            fs.statSync(pathname);
95            break;
96        } catch (e) {
97            pathname = null;
98        }
99    }
100
101    if (pathname) {
102        fs.readFile(pathname, 'utf-8', function(e, data) {
103          if (e) sys.error(e);
104
105          new(less.Parser)({
106              paths: [path.dirname(pathname)],
107              filename: pathname
108          }).parse(data, function (e, root) {
109              if (e) less.writeError(e);
110              callback(root);
111          });
112        });
113    } else {
114        sys.error("file '" + file + "' wasn't found.\n");
115        process.exit(1);
116    }
117}
118
119require('less/functions');
120
121for (var k in less) { exports[k] = less[k] }
122
123// Stylize a string
124function stylize(str, style) {
125    var styles = {
126        'bold'      : [1,  22],
127        'inverse'   : [7,  27],
128        'underline' : [4,  24],
129        'yellow'    : [33, 39],
130        'green'     : [32, 39],
131        'red'       : [31, 39],
132        'grey'      : [90, 39]
133    };
134    return '\033[' + styles[style][0] + 'm' + str +
135           '\033[' + styles[style][1] + 'm';
136}
137
Note: See TracBrowser for help on using the repository browser.