-
Notifications
You must be signed in to change notification settings - Fork 5
/
fruit.js
90 lines (77 loc) · 2.04 KB
/
fruit.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
/** Fruit.js
*
* Documentation generator from Markdown files
*
* Dependencies:
* - markdown-js
* - RSVP
* - LESS
*
**/
var Document = require('./src/Document.js'),
$ = require('./src/Utils.js'),
path = require('path'),
fs = require('fs');
function FruitJS (options, relativePath) {
this.options = options;
this.path = relativePath;
this.document = new Document(options, relativePath);
var prevDir = process.cwd(),
doc = this.document,
addTreeFn = function (arr, fn) {
for(var a in arr) {
addItemFn(arr[a], fn);
}
},
addItemFn = function (itm, fn) {
if(!fs.existsSync(itm)) {
console.error('File not found.', itm);
throw new Error('Unable to find file '+item);
} else if(fs.statSync(itm).isDirectory()) {
var sub = fs.readdirSync(itm);
for(var s in sub)
sub[s] = itm + path.sep + sub[s];
addTreeFn(sub,fn);
} else {
fn(itm);
}
};
process.chdir(path.dirname(relativePath));
addTreeFn(options.css, doc.addCSS.bind(doc));
addTreeFn(options.less, doc.addLESS.bind(doc));
addTreeFn(options.js, doc.addJS.bind(doc));
addTreeFn(options.images, doc.addImage.bind(doc));
addTreeFn(options.assets, doc.addAsset.bind(doc));
if(options.imageTitle) {
doc.addImage(options.imageTitle);
doc.setImageTitle(path.basename(options.imageTitle));
}
if(options.noExtraction) {
doc.disableExtraction();
}
if(options.preMenu) {
if(options.preMenu instanceof Array) {
for(var p in options.preMenu) {
doc.addBeforeMenu(options.preMenu[p]);
}
} else {
doc.addBeforeMenu(options.preMenu);
}
}
if(options.postMenu) {
if(options.postMenu instanceof Array) {
for(var p in options.postMenu) {
doc.addAfterMenu(options.postMenu[p]);
}
} else {
doc.addAfterMenu(options.postMenu);
}
}
for(var p in options.pages)
doc.addPage($.GetRelativePath(this.path, options.pages[p]));
process.chdir(prevDir);
}
FruitJS.prototype.render = function(folder) {
return this.document.render(folder, this.options.singlePage);
}
module.exports = FruitJS;