-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
231 lines (206 loc) · 5.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Module dependencies.
*/
var consolidate = require('consolidate');
['jade', 'dust', 'swig', 'liquor', 'ejs',
'eco', 'jazz', 'jqtpl', 'haml', 'whiskers',
'haml-coffee', 'hogan', 'handlebars',
'underscore', 'qejs', 'walrus', 'mustache',
'dot', 'just'].forEach(function (engine) {
if (consolidate[engine] &&
typeof consolidate[engine].outExtension === 'undefined' &&
typeof consolidate[engine].inExtension === 'undefined') {
consolidate[engine].outExtension = 'html';
consolidate[engine].inExtension = engine;
}
});
exports = module.exports = consolidate;
var fs = require('fs');
var pa = require('path');
var readCache = {};
/**
* Require cache.
*/
var cacheStore = {};
/**
* Require cache.
*/
var requires = {};
/**
* Clear the cache.
*
* @api public
*/
(function () {
var oldClearCache = consolidate.clearCache;
exports.clearCache = function(){
cacheStore = {};
oldClearCache();
};
}());
function cache(options, compiled) {
if (compiled && options.filename && options.cache) {
delete readCache[options.filename];//don't need to cache in both locations
cacheStore[options.filename] = compiled;
} else if (options.filename && options.cache) {
return cacheStore[options.filename];
}
return compiled;
}
/**
* Read `path` with `options` with
* callback `(err, str)`. When `options.cache`
* is true the template string will be cached.
*
* @param {String} options
* @param {Function} fn
* @api private
*/
function read(path, options, fn) {
var str = readCache[path];
// cached (only if cached is a string and not a compiled template function)
if (options.cache && str && typeof str === 'string') return fn(null, str);
// read
fs.readFile(path, 'utf8', function(err, str){
if (err) return fn(err);
str = str.replace(/\r\n/g, '\n');
if (options.cache) readCache[path] = str;
fn(null, str);
});
}
/**
* fromStringRenderer
*/
function fromStringRenderer(name) {
return function(path, options, fn){
options.filename = path;
if (cache(options)) {
exports[name].render('', options, fn);//string doesn't matter if it's in the cache.
} else {
read(path, options, function(err, str){
if (err) return fn(err);
exports[name].render(str, options, fn);
});
}
};
}
function clone(obj) {
if (typeof obj === 'object') {
var res = {};
Object.keys(obj).forEach(function (key) {
res[key] = clone(obj[key]);
});
return res;
} else {
return obj;
}
}
/**
* options:
* - paths: specifies search paths for @import directives
* - filename: is required for @import directives and useful for error reporting
* - compress: defaults to false, minifies the output if true.
*/
exports.less = fromStringRenderer('less');
exports.less.render = function (str, options, fn) {
var engine = requires.less || (requires.less = require('less'));
options = options || {};
if (options.filename) {
options.paths = options.paths || [pa.dirname(options.filename)];
}
var parser = new(engine.Parser)(options);
parser.parse(str, function (e, tree) {
if (e) return fn(e);
var res;
try {
res = tree.toCSS(options);
} catch (ex) {
return fn(ex);
}
fn(null, res);
});
};
exports.less.outExtension = 'css';
exports.less.inExtension = 'less';
/**
* options:
* - paths: specifies search paths for @import directives
* - filename: is required for @import directives and useful for error reporting
* - compress: defaults to false, minifies the output if true.
*/
exports.styl = exports.stylus = fromStringRenderer('stylus');
exports.stylus.render = function (str, options, fn) {
var engine = require.stylus || (require.stylus = require('stylus'));
engine.render(str, options || {}, fn);
};
exports.stylus.outExtension = 'css';
exports.stylus.inExtension = 'styl';
exports.sass = fromStringRenderer('sass');
exports.sass.render = function (str, options, fn) {
var engine = require.sass || (require.sass = require('sass'));
var res;
try {
res = engine.render(str);
} catch (ex) {
return fn(ex);
}
fn(null, res);
};
exports.sass.outExtension = 'css';
exports.sass.inExtension = 'sass';
exports.markdown = exports.md = fromStringRenderer('md');
exports.md.render = function (str, options, fn) {
var engine = require.markdown;
function load(name, engine) {
try {
var md = require(name);
} catch (ex) {
return undefined;
}
return function (str, options) {
return engine.call(md, str, options);
};
}
engine = engine || load('supermarked', function (str, options) {
return this(str, options || {});
});
engine = engine || load('marked', function (str, options) {
return this.parse(str, options || {});
});
engine = engine || load('discount', function (str, options) {
return this.parse(str);
});
engine = engine || load('markdown-js', function (str, options) {
return this.parse(str);
});
engine = engine || load('markdown', function (str, options) {
return this.parse(str);
});
if (!engine) {
throw new Error('Cannot find markdown library, install markdown, discount, or marked.');
}
var res;
try {
res = engine(str, options);
} catch (ex) {
return fn(ex);
}
fn(null, res);
};
exports.markdown.outExtension = 'html';
exports.markdown.inExtension = 'md';
exports.coffee = exports['coffee-script'] = exports.coffeescript = fromStringRenderer('coffee');
exports.coffee.render = function (str, options, fn) {
options = clone(options);
options.bare = options.bare || false;
var engine = require.coffeescript || (require.coffeescript = require('coffee-script'));
var res;
try {
res = engine.compile(str, options);
} catch (ex) {
return fn(ex);
}
fn(null, res);
};
exports.coffee.outExtension = 'js';
exports.coffee.inExtension = 'coffee';