-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslug.js
74 lines (66 loc) · 2.08 KB
/
slug.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
var hem = new (require('hem'));
var HamlCoffee = require('haml-coffee/src/haml-coffee');
var CoffeeScript = require('coffee-script');
var fs = require('fs');
var argv = process.argv.slice(2);
var stylus = require('stylus');
var dirname = require('path').dirname;
hem.compilers.haml = function(path) {
var compiler, content, template;
compiler = new HamlCoffee({});
content = fs.readFileSync(path, 'utf8');
compiler.parse(content);
template = compiler.precompile();
template = CoffeeScript.compile(template);
return "module.exports = (function(data){ return (function(){ return " + template + " }).call(data); })";
};
hem.compilers.jhaml = function(path) {
var compiler, content, template;
compiler = new HamlCoffee({});
content = fs.readFileSync(path, 'utf8');
compiler.parse(content);
template = compiler.precompile();
template = CoffeeScript.compile(template);
return "module.exports = (function(values){ " +
"return (function(values){ " +
"var $ = jQuery, result = $(); " +
"for(var i=0; i < this.length; i++) { " +
"var value = this[i]; " +
"var elem = (function(){ " +
"return " + template +
"}).call(value); " +
"elem = $(elem); " +
"elem.data('item', value); " +
"$.merge(result, elem); " +
"} " +
"return result; " +
"}).call(values); " +
"})";
};
require.extensions['.jhaml'] = require.extensions['.haml'];
// http://blog.divshot.com/post/31336785156/exposing-env-to-spine
hem.compilers.env = function(path) {
var content = fs.readFileSync(path, 'utf8');
var envHash = JSON.parse(content);
for (key in envHash) {
if (process.env[key]) {
envHash[key] = process.env[key];
}
}
return "module.exports = " + JSON.stringify(envHash);
};
hem.compilers.styl = function(path) {
var content = fs.readFileSync(path, 'utf8');
var result = '';
stylus(content)
.include(dirname(path))
.set('include css', true)
.render(function(err, css) {
if (err) {
throw err;
}
return result = css;
});
return result;
};
hem.exec(argv[0]);