1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var Parser = require('./parser')
|
13 | , Compiler = require('./visitor/compiler')
|
14 | , Evaluator = require('./visitor/evaluator')
|
15 | , utils = require('./utils')
|
16 | , nodes = require('./nodes');
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | var Renderer = module.exports = function Renderer(str, options) {
|
27 | options = options || {};
|
28 | options.functions = {};
|
29 | options.imports = [];
|
30 | options.filename = options.filename || 'stylus';
|
31 | this.str = str;
|
32 | this.options = options;
|
33 | this.parser = new Parser(str, options);
|
34 | };
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | Renderer.prototype.render = function(fn){
|
44 | try {
|
45 | var ast = this.parser.parse();
|
46 | this.evaluator = new Evaluator(ast, this.options);
|
47 | ast = this.evaluator.evaluate();
|
48 | new Compiler(ast, this.options).compile(fn);
|
49 | } catch (err) {
|
50 | fn(utils.formatException(
|
51 | this
|
52 | , err
|
53 | , this.options));
|
54 | }
|
55 | nodes.source = null;
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | Renderer.prototype.set = function(key, val){
|
68 | this.options[key] = val;
|
69 | return this;
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | Renderer.prototype.define = function(name, fn, raw){
|
84 | this.options.functions[name] = fn;
|
85 | fn.raw = raw;
|
86 | return this;
|
87 | };
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | Renderer.prototype.import = function(file){
|
98 | this.options.imports.push(file);
|
99 | return this;
|
100 | };
|
101 |
|
102 |
|