This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
write.js
250 lines (217 loc) · 7.31 KB
/
write.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
var fs = require('fs');
var path = require('path');
var append = require('append');
var clone = require('clone');
var dive = require('dive');
var async = require('async');
var ejs = require('ejs');
var esc = require('esc');
module.exports = function write(reg, conf, cb) {
console.log('Beginning to write index and tag files.');
async.parallel({
indexes: function (callback) {
indexes(reg, conf, function (err) {
console.log('Index files written.');
callback(err);
});
},
tags: function (callback) {
tags(reg, conf, function (err, rest) {
console.log('Tag files written.');
callback(err);
});
},
autoindex: function (callback) {
autoindex(reg, conf, function (err, rest) {
console.log('Autoindex files written.');
callback(err);
});
}
}, cb);
};
function indexes(reg, conf, cb) {
var dir = conf.directories;
var output = path.resolve(conf.root, dir.output);
// total number of indexes
var todo = conf.indexes.length;
// for each index, lookup documents and then write them to disk
conf.indexes.forEach(function (index) {
// read template
fs.readFile(path.resolve(dir.templates, index.template), 'utf8',
function (err, tpl) {
// extend index's properties
var p = (typeof conf.properties == 'object') ?
append(clone(conf.properties), index.properties) :
clone(index.properties);
// add esc to p
p.esc = esc;
// set title
p.title = index.title;
// if we've got a single page index (e.g. feeds)
if (typeof index.path == 'string')
// get documents
reg.get({ _id: new RegExp(index.pattern) }, {}, index.sort, index.limit,
function (err, page) {
page.toArray(function (err, documents) {
if (err)
return cb(err);
p.__docs = documents;
var data = ejs.render(tpl, { locals: p });
// get filename and write the file to disk
var file = path.resolve(output, index.path);
fs.writeFile(file, data, function (err) {
if (err)
return cb(err);
console.log(' * '+file+' written.');
if (!--todo)
cb();
});
});
});
// if we've got a multi page index (e.g. blog archives)
else
// get documents as pages
reg.getPages({ _id: new RegExp(index.pattern) }, {}, index.sort,
index.limit, function (err, pages) {
if (err)
return cb(err);
// if no files are in the index, decrease todo
if (pages.length == 0 && !--todo)
return cb();
// for each page, own scope
for (var i = 0, files = 0; i < pages.length; i++) {(function (i) {
var page = pages[i];
page.toArray(function (err, documents) {
if (err)
return cb(err);
p.__docs = documents;
// get filename for page
if (i == 0)
var file = path.resolve(output, index.path.first);
else
var file = path.resolve(output,
index.path.pattern.replace(/{{page}}/g, i + 1));
var fileContents = ejs.render(tpl, { locals: p });
// write the page to disk
fs.writeFile(file, fileContents, function (err) {
if (err)
return cb(err);
// callback at last
console.log(' * '+file+' written.');
if (++files == pages.length && !--todo)
cb();
});
});
})(i);}
});
});
});
}
function tags(reg, conf, cb) {
var dir = conf.directories;
var tags = conf.tags;
if (reg.tags) {
var tagDir = path.resolve(conf.root, dir.output, tags.directory);
// Load templates concurrently
async.parallel({
tag: function (callback) {
fs.readFile(path.resolve(dir.templates, tags.template), 'utf8',
function (err, tpl) {
var todo = reg.tags.length;
// for each tag
reg.tags.toArray().forEach(function (tag) {
var p = clone(conf.properties);
p.esc = esc;
p.title = tag;
var file = path.resolve(tagDir, tag+'.html');
var fileContents = ejs.render(tpl, { locals: p });
// write tag file
fs.writeFile(file, fileContents, function (err) {
if (err)
return callback(err);
console.log(' * '+file+' written.');
if (!--todo)
return callback();
});
});
});
},
index: function (callback) {
fs.readFile(path.resolve(dir.templates, tags.index.template), 'utf8',
function (err, tpl) {
var p = clone(conf.properties);
p.__tags = reg.tags.toArray();
var file = path.resolve(tagDir, tags.index.path);
var fileContents = ejs.render(tpl, { locals: p });
fs.writeFile(file, fileContents, function (err) {
if (err)
return callback(err);
console.log(' * '+file+' written.');
callback();
});
});
}
}, function (err) {
if (err)
return cb(err);
cb();
});
} else
return cb();
}
function autoindex(reg, conf, cb) {
var pubDir = path.resolve(conf.root, conf.directories.output);
var tplDir = conf.directories.templates;
// dive down the directory tree
dive(pubDir, { directories: true, files: false, recursive: true },
function (err, dir) {
if (err)
return cb(err);
// normalize dir and remove pubDir
var dirname = dir.replace(path.normalize(pubDir + '/'), '');
// normalize slashes
dirname = dirname.replace(new RegExp('\\\\', 'g'), '/');
for (var i in conf.autoindex) {(function (index) {
if (dirname.match(new RegExp(index.pattern))) {
async.parallel({
tpl: function (callback) {
// read template
fs.readFile(path.resolve(tplDir, index.template), 'utf8',
function (err, tpl) {
if (err)
return callback(err);
callback(null, tpl);
});
},
files: function (callback) {
// read directory
fs.readdir(dir, function (err, files) {
if (err)
return callback(err);
// filter unwanted files
var filtered = files.filter(function (elem) {
return !(new RegExp(index.filter)).test(elem);
});
callback(null, filtered);
});
}
}, function (err, data) {
if (err)
return cb(err);
var p = clone(conf.properties);
p.__dir = dirname;
p.__files = data.files;
p.title = dirname.split('/').pop();
var file = path.resolve(dir, index.path);
var fileContents = ejs.render(data.tpl, { locals: p });
// write generated index
fs.writeFile(file, fileContents, function (err) {
if (err)
return cb(err);
console.log(' * '+file+' written.');
});
});
}
})(conf.autoindex[i])}
}, cb);
}