Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
373 lines (257 loc) · 6.97 KB
/
Copy pathindex.js
File metadata and controls
373 lines (257 loc) · 6.97 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* META Doc
* Documentation generator
*
* @author META Platform <www.meta-platform.com>
* @license See LICENSE file distributed with this source code
*/
//Dependencies
var fs = require("fs");
var watch = require('node-watch');
var logger = require("meta-logger");
var EventEmitter = require('events').EventEmitter;
var TreeParser = require("./lib/treeParser.js");
var Compiler = require("./lib/compiler.js");
var Rewrite = require("./lib/rewrite.js");
var Sitemap = require("./lib/sitemap.js");
var Utils = require("./lib/utils.js");
var Shortcodes = require("./lib/shortcodes.js");
var Helpers = require("./lib/helpers.js");
/**
* Constructor
*/
var MetaDoc = function(options){
if(!options) options = {};
this.dirMedia = options.media || "./media";
this.dirPages = options.pages || "./pages";
this.dirSite = options.site || "./site";
this.dirTemplate = options.template || "./template";
this.dirAssets = options.assets || "./template/assets";
this.configFile = options.config || "./config.json";
this.cacheFile = options.cache || "./cache.json";
this.config = {};
this.compilers = [];
//Validate configuration
logger.info("Media dir: ", this.dirMedia);
logger.info("Pages dir: ", this.dirPages);
logger.info("Site output dir:", this.dirSite);
logger.info("Template: ", this.dirTemplate);
logger.info("Asssets dir: ", this.dirAssets);
logger.info("Config: ", this.configFile);
//Validate setup
if(!fs.existsSync(this.dirMedia))
throw new Error("Media directory not found.");
if(!fs.existsSync(this.dirPages))
throw new Error("Pages directory not found.");
if(!fs.existsSync(this.dirSite))
throw new Error("Site output directory not found.");
if(!fs.existsSync(this.dirTemplate + "/default.jade"))
throw new Error("Default template file not found.");
if(!fs.existsSync(this.dirAssets))
throw new Error("Assets directory not found.");
if(!fs.existsSync(this.configFile))
throw new Error("Config file not found.");
//Register default compiler
this.useCompiler(Compiler);
this.useCompiler(Rewrite);
this.useCompiler(Sitemap);
//Copy assets
this.loadConfig();
};
MetaDoc.prototype = Object.create(EventEmitter.prototype);
/**
* Loads config from JSON
*/
MetaDoc.prototype.loadConfig = function(){
var configContents = fs.readFileSync(this.configFile);
try {
this.config = JSON.parse(configContents);
} catch(e){
return this.emit("error", new Error("Configuration file is not valid JSON."));
}
};
/**
* Loads cache from file if exists
*
* @return Object|null
*/
MetaDoc.prototype.getCache = function(){
if(!fs.existsSync(this.cacheFile))
return null;
var cacheContents = fs.readFileSync(this.cacheFile);
return JSON.parse(cacheContents);
};
/**
* Save cache to file
*
* @param Object cache
*/
MetaDoc.prototype.saveCache = function(cache){
fs.writeFileSync(this.cacheFile, JSON.stringify(cache));
};
/**
* Copy modified media to target directory
*/
MetaDoc.prototype.copyMedia = function(){
logger.info("Copying media...");
try {
Utils.copyDir(this.dirMedia, this.dirSite + "/media");
} catch(e) {
this.emit("error", e);
}
return this;
};
/**
* Copy modified assets to target directory
*/
MetaDoc.prototype.copyAssets = function(){
logger.info("Copying assets...");
try {
Utils.copyDir(this.dirAssets, this.dirSite + "/assets");
} catch(e) {
this.emit("error", e);
}
return this;
};
/**
* Remove all files from site directory
*/
MetaDoc.prototype.cleanSite = function(){
logger.info("Cleaning site directory...");
Utils.cleanDir(this.dirSite);
return this;
};
/**
* Registers default compiler shortcode
*
* @param string name
* @param Function cb
*/
MetaDoc.prototype.addShortcode = function(name, cb){
Compiler.shortcodes.add(name, cb);
return this;
};
/**
* Registers default compiler helper
*
* @param Array args
*/
MetaDoc.prototype.addHelper = function(args){
Compiler.helpers.push(args);
return this;
};
/**
* Registers another compiler
*
* @param Array args
*/
MetaDoc.prototype.useCompiler = function(compiler){
this.compilers.push(compiler);
};
/**
* Registers built-in shortcodes
*/
MetaDoc.prototype.useDefaultShortcodes = function(){
//Use base shortcodes
for(var s in Shortcodes)
this.addShortcode(s, Shortcodes[s]);
};
/**
* Registers built-in helpers
*/
MetaDoc.prototype.useDefaultHelpers = function(){
//Use base helpers
for(var h in Helpers)
if(h.substr(0, 1) != "_")
this.addHelper(Helpers[h]);
};
/**
* Registers custom helpers
*/
MetaDoc.prototype.useCustomHelpers = function(){
if(this.config.custom_blocks){
for(var i in this.config.custom_blocks){
var n = this.config.custom_blocks[i];
if(n instanceof Object){
if(!n.name)
throw new Error("Custom block definition #" + i + " must specify 'name' property.");
this.addHelper( Helpers._block.call(null, n.name, n.prefix || "", n.postfix || "") );
} else {
this.addHelper( Helpers._block.call(null, this.config.custom_blocks[i], "", "") );
}
}
}
};
/**
* Compile documentation from pages to site
*
* @param boolean useCache
*/
MetaDoc.prototype.compile = function(useCache){
var self = this;
var countProperties = function(obj){
var x = 0;
for(var i in obj) x++;
return x;
};
logger.info("Compiling...");
try {
//Generate pages tree
logger.info("Parsing pages tree...");
var tree = new TreeParser(self.dirPages);
//Check cache
var changes = tree.getChanges( useCache ? self.getCache() : null );
logger.info("Pages to be compiled:", countProperties(changes.pages));
var compilerOpts = {
mediaPath: "media",
assetsPath: "assets",
pagesDir: this.dirPages,
outputDir: this.dirSite,
templateDir: this.dirTemplate,
templateIndex: "index.jade"
};
//Setup compiler
for(var c in this.compilers){
var compilerConstructor = this.compilers[c];
var compiler = new compilerConstructor(compilerOpts, tree.root, this.config);
compiler.compile(changes);
}
//Save cache
logger.debug("Saving cache...");
this.saveCache(tree.getCache());
logger.info("Done.");
} catch(e) {
return this.emit("error", e);
}
return this;
};
MetaDoc.prototype.watch = function(){
var self = this;
logger.info("Watching for changes...");
watch(this.dirPages, function(filename){
logger.info("File '%s' has changed.", filename);
self.compile(true);
});
watch(this.dirAssets, function(filename){
logger.info("File '%s' has changed, copying assets...", filename);
self.copyAssets();
});
watch(this.dirTemplate, function(filename){
logger.info("Template '%s' has changed, recompiling all...", filename);
self.compile();
});
watch(this.configFile, function(filename){
logger.info("Config has changed, recompiling all...");
self.loadConfig();
self.compile();
});
watch(this.dirMedia, function(filename){
logger.info("File '%s' has changed, copying media...", filename);
self.copyMedia();
});
return this;
};
//Export
module.exports = function(options){
return new MetaDoc(options);
};