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
|
var Q = require("q");
var _ = require("lodash");
var path = require("path");
var fs = require("./utils/fs");
var Configuration = require("./configuration");
var TemplateEngine = require("./template");
var parser = require("./parser");
var Book = function(root, options, parent) {
// Root folder of the book
this.root = root;
// Parent book
this.parent = parent;
// Configuration
this.config = new Configuration(this, options);
Object.defineProperty(this, "options", {
get: function () {
return this.config.options;
}
});
// Template
this.template = new TemplateEngine(this);
// Summary
this.summary = {};
// Glossary
this.glossary = [];
// Langs
this.langs = [];
// Sub-books
this.books = [];
};
// Initialize and parse the book: config, summary, glossary
Book.prototype.init = function() {
var that = this;
var multilingal = false;
return this.config.load()
.then(function() {
return that.parseLangs()
.then(function() {
multilingal = that.langs.length > 0;
// Sub-books that inherit from the current book configuration
that.books = _.map(that.langs, function(lang) {
return new Book(
path.join(that.root, lang.path),
_.extend({}, that.options, {
'output': path.join(that.options.output, lang.path),
'lang': lang.lang
}),
that
)
});
});
})
.then(function() {
if (multilingal) return;
return that.parseReadme();
})
.then(function() {
if (multilingal) return;
return that.parseSummary();
})
.then(function() {
if (multilingal) return;
return that.parseGlossary();
})
.then(function() {
// Init sub-books
return _.reduce(that.books, function(prev, book) {
return prev.then(function() {
return book.init();
});
}, Q());
})
.thenResolve(this);
};
// Generate the output
Book.prototype.generate = function() {
var that = this;
return this.init()
.then(function() {
});
};
// Parse readme to extract defaults title and description
Book.prototype.parseReadme = function() {
var that = this;
return that.findFile(that.config.getStructure("readme"))
.then(function(readme) {
if (!readme) throw "No README file";
return that.template.renderFile(readme.path)
.then(function(content) {
return readme.parser.readme(content);
});
})
.then(function(readme) {
that.options.title = that.options.title || readme.title;
that.options.description = that.options.description || readme.description;
});
};
// Parse langs to extract list of sub-books
Book.prototype.parseLangs = function() {
var that = this;
return that.findFile(that.config.getStructure("langs"))
.then(function(langs) {
if (!langs) return [];
return that.template.renderFile(langs.path)
.then(function(content) {
return langs.parser.langs(content);
});
})
.then(function(langs) {
that.langs = langs;
});
};
// Parse summary to extract list of chapters
Book.prototype.parseSummary = function() {
var that = this;
return Q.all([
that.findFile(that.config.getStructure("summary")),
that.findFile(that.config.getStructure("readme"))
])
.spread(function(summary, readme) {
if (!summary) throw "No SUMMARY file";
return that.template.renderFile(summary.path)
.then(function(content) {
return summary.parser.summary(content, readme.path);
});
})
.then(function(summary) {
that.summary = summary;
});
};
// Parse glossary to extract terms
Book.prototype.parseGlossary = function() {
var that = this;
return that.findFile(that.config.getStructure("glossary"))
.then(function(glossary) {
if (!glossary) return {};
return that.template.renderFile(glossary.path)
.then(function(content) {
return glossary.parser.glossary(content);
});
})
.then(function(glossary) {
that.glossary = glossary;
});
};
// Find file that can be parsed with a specific filename
Book.prototype.findFile = function(filename) {
var that = this;
return _.reduce(parser.extensions, function(prev, ext) {
return prev.then(function(output) {
// Stop if already find a parser
if (output) return output;
var filepath = filename+ext;
return that.fileExists(filepath)
.then(function(exists) {
if (!exists) return null;
return {
parser: parser.get(ext).parser,
path: filepath
};
})
});
}, Q(null));
};
// Check if a file exists in the book
Book.prototype.fileExists = function(filename) {
return fs.exists(
path.join(this.root, filename)
);
};
// Read a file
Book.prototype.readFile = function(filename) {
return fs.readFile(
path.join(this.root, filename),
{ encoding: "utf8" }
);
};
// Return stat for a file
Book.prototype.statFile = function(filename) {
return fs.stat(path.join(this.root, filename));
};
module.exports= Book;
|