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
|
var Immutable = require('immutable');
var Output = require('../models/output');
var Config = require('../models/config');
var Promise = require('../utils/promise');
var callHook = require('./callHook');
var preparePlugins = require('./preparePlugins');
var preparePages = require('./preparePages');
var prepareAssets = require('./prepareAssets');
var generateAssets = require('./generateAssets');
var generatePages = require('./generatePages');
/**
Generate a book using a generator.
The overall process is:
1. List and load plugins for this book
2. Call hook "config"
3. Call hook "init"
4. Initialize generator
5. List all assets and pages
6. Copy all assets to output
7. Generate all pages
8. Call hook "finish:before"
9. Finish generation
10. Call hook "finish"
@param {Generator} generator
@param {Book} book
@param {Object} options
@return {Promise<Output>}
*/
function generateBook(generator, book, options) {
options = generator.Options(options);
var state = generator.State? generator.State({}) : Immutable.Map();
var start = Date.now();
return Promise(
new Output({
book: book,
options: options,
state: state,
generator: generator.name
})
)
.then(preparePlugins)
.then(preparePages)
.then(prepareAssets)
.then(
callHook.bind(null,
'config',
function(output) {
var book = output.getBook();
var config = book.getConfig();
var values = config.getValues();
return values.toJS();
},
function(output, result) {
var book = output.getBook();
var config = book.getConfig();
config = Config.updateValues(config, result);
book = book.set('config', config);
return output.set('book', book);
}
)
)
.then(
callHook.bind(null,
'init',
function(output) {
return {};
},
function(output) {
return output;
}
)
)
.then(function(output) {
if (!generator.onInit) {
return output;
}
return generator.onInit(output);
})
.then(generateAssets.bind(null, generator))
.then(generatePages.bind(null, generator))
.then(callHook.bind(null,
'finish:before',
function(output) {
return {};
},
function(output) {
return output;
}
)
)
.then(function(output) {
if (!generator.onFinish) {
return output;
}
return generator.onFinish(output);
})
.then(callHook.bind(null,
'finish',
function(output) {
return {};
},
function(output) {
return output;
}
)
)
.then(function(output) {
var logger = output.getLogger();
var end = Date.now();
var duration = (end - start)/1000;
logger.info.ok('generation finished with success in ' + duration.toFixed(1) + 's !');
});
}
module.exports = generateBook;
|