-
Notifications
You must be signed in to change notification settings - Fork 0
/
gruntfile.js
251 lines (206 loc) · 7.98 KB
/
gruntfile.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var fs = require('fs-extra');
var path = require('path');
var assert = require('assert');
var handlebars = require('handlebars');
var args = process.argv;
//grab second arg options
var template = args[3];
var global_handlebars_data = {
bower : "{{assets}}/components",
default_javascript : [
'https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js',
'{{assets}}/js/app.js'
],
default_css : [
'https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.min.css',
'{{assets}}/css/bare.css',
'{{assets}}/css/main.css'
],
default_fonts : [
'https://fonts.googleapis.com/css?family=Dosis',
]
}
module.exports = function(grunt) {
var temply = grunt.option("temply");
var temp_dir = temply ? temply.path : null;
if (!temp_dir) {
var res = template.split(/^--/);
temp_dir = path.resolve(__dirname + '/templates/' + res[1] + '/');
} else {
temp_dir = path.resolve(temp_dir);
}
if (!fs.existsSync(temp_dir)){
throw new Error(res[1] + ' does not seem to be a valid template name');
}
//template specific file loader
function get_file (file){
return path.resolve(temp_dir + '/' + file);
}
var less_files = {};
var init = require(get_file('init.js'));
var dist = init.outLocation || "dist";
//each template can define a seperate layout
//it must be named layout.hbs and under template root directory
//if not then global layout will be used
var layout = get_file("layout.hbs");
try {
layout = fs.lstatSync(layout).isFile() ? layout : 'layout.hbs';
} catch (e){
layout = 'layout.hbs';
}
grunt.registerTask('Compile', 'Compile Handlebars', function() {
try {
fs.emptyDirSync(get_file(dist));
} catch(e){}
//go through each page.hbs, and compile
var baseLocation = get_file("content");
var baseDestination = get_file(dist);
if (init.handlebars && typeof init.handlebars === 'function'){
init.handlebars(handlebars);
}
(function LoopFiles (location){
var rel = path.relative(location, baseLocation);
var assets = rel.replace('\\', '/');
if (!assets){
assets = './assets';
} else { assets += '/assets'; }
global_handlebars_data.assets = assets;
var currentDest = location.split(baseLocation)[1] || '/';
currentDest += '/';
var hbs = fs.readdirSync(location);
var layoutContent = fs.readFileSync(layout);
var page_data;
var data = (function(){
for (keys in global_handlebars_data){
if (!init.data[keys]){
init.data[keys] = global_handlebars_data[keys];
}
}
return init.data;
})();
/* pages starting with ipage- are special pages created automatically
so remove and unlink if they are found in directory as they will be
created again */
for (var i = 0; i < hbs.length; i++){
var hb = hbs[i];
var re = /^ipage-/;
if (re.test(hb)){
hbs.splice(i, 1);
--i;
fs.unlinkSync(get_file("content" + currentDest + hb));
}
}
for (var i = 0; i < hbs.length; i++){
var hb = hbs[i];
var page = get_file("content" + currentDest + hb);
if (fs.lstatSync( page ).isDirectory()){
if ( !fs.existsSync( get_file(dist + currentDest + hb) ) ) {
fs.mkdirSync( get_file(dist + currentDest + hb) );
}
LoopFiles(page);
continue;
}
//split
var f = hb.split('.');
var filename = f[0] + (f[2] ? '.' + f[2] : '.html');
if (f[1] === 'less' || f[2] === 'less'){
if (f[0] === 'main' || f[1] === 'main'){
var location = get_file(dist + currentDest + f[0] + '.css');
less_files[location] = page;
}
continue;
}
// console.log(f);
if (f[1] !== 'hbs'){
filename = hb;
var location = get_file(dist + currentDest + filename);
if (init.fileHandler){
var action = init.fileHandler[ f[1] ];
if (typeof action === 'function'){
var out = action(page);
if (out){
fs.writeFileSync(location, out);
}
} else if (typeof action === 'string' && action === 'ignore'){
console.log('ignoring file ' + filename);
} else {
fs.copySync(page, location, {});
}
} else {
fs.copySync(page, location, {});
}
continue;
}
handlebars.registerHelper("config", function(context, options){
var text = context.fn();
page_data = eval ("(" + text + ")");
for (key in data){
if (!page_data[key]){
page_data[key] = data[key];
}
}
return "";
});
//export back some information about current
//page, so you can use it with handlebars helpers
init.current = {
location : location,
page : page,
layout : layout,
hbs : hbs,
data : page_data
};
var pagecontent = fs.readFileSync(page).toString("utf8");
var p = handlebars.compile(pagecontent);
data.assets = assets;
var page_out = p(data);
handlebars.registerPartial("content", page_out);
var all = page_data || data || {};
all.assets = assets;
var template;
if (f[2]){
template = handlebars.compile('{{> content}}');
} else {
template = handlebars.compile(layoutContent.toString("utf8"));
}
var out = template(all);
//FIXME: we compile 2 times just to parse
//{{assets}} within global options
//there must be a better way to do this
template = handlebars.compile(out);
out = template({ assets : assets });
fs.writeFileSync(get_file(dist + currentDest + filename), out);
page_data = null;
}
})(baseLocation);
});
grunt.initConfig({
less: {
development: {
options: {
paths : ["./less", __dirname + "/less"],
// compress: true,
// yuicompress: true,
// optimization: 4
},
files: less_files
}
},
watch: {
files : {
files: [get_file('content/**/*')],
tasks: ['Compile', 'less'],
options: {
nospawn: true
}
}
}
});
//load
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');
//register
grunt.registerTask('default', ['watch']);
grunt.registerTask('watcher', ['watch']);
grunt.registerTask('compile', ['Compile', 'less']);
};