/
generate.js
428 lines (373 loc) · 13 KB
/
generate.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
const marked = require('marked')
const matter = require('gray-matter')
const moment = require('moment')
const escape_html = require('escape-html')
const tmp = require('tmp-promise')
const fsp = require('fs').promises
const mkdirp = require('mkdirp-promise')
const watcher = require('@atom/nsfw')
const path = require('path')
// --------------------------------------------------------------------------------
function Post (date, filename, permalink, meta, content) {
this.date = date
this.filename = filename
this.permalink = permalink
this.meta = meta
this.content = content
}
// --------------------------------------------------------------------------------
const url = 'http://norswap.com'
const posts_per_page = 15
const atom_posts = 10
// --------------------------------------------------------------------------------
// Forces the last file written by ribosome to be flushed.
process.on('exit', () => {
. /!stdout()
});
// --------------------------------------------------------------------------------
// Post Tracking
// Tracks the lowest post that changed, so that we only regenerate the feed and
// the index pages if we need to.
let lowest_post_change = Infinity
// Posts are sorted in newest-first order: so reverse filename order, because
// filenames start with their date followed by a daily counter.
let posts
// When building incrementally, fill with boolean to indicate whether there is
// still a source file for the post at the same index (= true).
let post_exists
// Returns the index of the post with the given filename within `posts` or the location at which it
// should be inserted.
function find_post (filename)
{
let low = 0
let high = posts.length
while (low != high)
{
const mid = Math.floor((high + low) / 2)
if (posts[mid].filename > filename)
low = mid + 1
else
high = mid
}
return low
}
// Insert `post` into `posts`, keeping it sorted and avoiding duplicates by
// filename.
function insert_post (post)
{
const i = find_post(post.filename)
lowest_post_change = Math.min(i, lowest_post_change)
// Skip content for posts for which it is not required elsewhere.
if (i >= atom_posts) {
if (post.meta.layout != 'brief')
post.content = ''
// The content of the last atom post might no longer be required.
} else if (posts.length > atom_posts) {
const last = posts[atom_posts]
if (last.meta.layout != 'brief') last.content = ''
}
if (i < posts.length && posts[i].filename == post.filename) {
posts[i] = post
post_exists[i] = true
} else {
posts.splice(i, 0, post)
post_exists.splice(i, 0, true)
}
}
// Removes the post with the given filename from `posts`, if it exists.
function remove_post (filename)
{
const i = find_post(filename)
lowest_post_change = Math.min(i, lowest_post_change)
if (i < posts.length && posts[i].filename == filename)
{
posts.splice(i, 1)
// Regenerate the content for one post that might now be needed to round
// out the atom feed.
if (i < atom_posts && posts.length >= atom_posts) {
const last = posts[atom_posts - 1]
if (last.meta.layout != 'brief')
generate_page('post', last.filename, false)
}
}
}
async function parse_post_cache()
{
posts = JSON.parse(await fsp.readFile('posts.json'),
(k,v) => k == 'date' ? moment(v) : v)
post_exists = new Array(posts.length).fill(false)
}
async function write_post_cache()
{
if (lowest_post_change < Infinity)
await fsp.writeFile('posts.json', JSON.stringify(posts, null, '\t'))
}
// --------------------------------------------------------------------------------
// Page Generation
function permalink (type, filename)
{
switch (type) {
case 'post':
return filename.slice(13, -3) // strip date, counter and extension
case 'page':
return filename.slice(0, -3); // strip extension
case 'draft':
return 'draft-' + filename.slice(0, -3) // strip extension
}
throw "unknown page type"
}
async function generate_page (type, filename, incremental) // relative filename
{
const src_loc = type + 's/' + filename
const plink = permalink(type, filename)
const dst_loc = `../${plink}/index.html`
const src_time = (await fsp.stat(src_loc)).mtimeMs
if (incremental) {
const dst_time = await fsp.stat(dst_loc).then(x => x.mtimeMs, _ => 0)
if (src_time < dst_time) {
if (type == 'post')
post_exists[find_post(filename)] = true
return
}
}
const date = moment(filename.slice(0, 10), 'YYYY-MM-DD')
const layout = type == 'draft' ? 'post' : type
const obj = matter(await fsp.readFile(src_loc, {encoding: 'utf-8'}))
const meta = obj.data
if (meta.title) meta.title = escape_html(meta.title)
const content = marked(obj.content)
await mkdirp('../' + plink)
. /!output(dst_loc)
. /!include('page.dna')
console.log(`Created ${dst_loc}`)
if (type == 'post')
insert_post(new Post(date, filename, plink, meta, content))
}
async function generate_pages (type, incremental)
{
const promises = []
for (filename of await fsp.readdir(type + 's')) {
if (filename[0] == '.') continue
promises.push(generate_page(type, filename, incremental))
}
return Promise.all(promises)
}
// Note: incremental builds will only call this for posts.
async function delete_page (type, filename)
{
const dir = '../' + permalink(type, filename)
// catch in case already deleted
await fsp.unlink(dir + '/index.html').catch(e => {})
await fsp.rmdir(dir).catch(e => {})
console.log(`Deleted ${dir}`)
if (type == 'post')
remove_post(filename)
}
// --------------------------------------------------------------------------------
// Generate Index Pages & Atom Feed
async function generate_index_page (posts, page, num_pages)
{
const layout = 'home';
const tmp_file = await tmp.tmpName();
. /!output(tmp_file)
. /!include('home.dna')
. /!stdout() // forces the temp file to be flushed
const content = await fsp.readFile(tmp_file, {encoding: 'utf-8'});
let output = ''
if (page == 1) {
output = '../index.html'
} else {
await mkdirp('../' + page);
output = `../${page}/index.html`
}
const meta = {} // unused, but avoid compile-time error
. /!output(output)
. /!include('page.dna')
console.log(`Created ${output}`)
}
async function generate_index_pages_and_atom ()
{
const num_pages = Math.floor((posts.length - 1) / posts_per_page) + 1
const lowest_page = Math.floor(lowest_post_change / posts_per_page) + 1
for (let i = lowest_page; i <= num_pages; ++i)
generate_index_page(posts, i, num_pages)
if (lowest_post_change < atom_posts) {
. /!output('../atom.xml')
. /!include('atom.dna')
console.log('Created ../atom.xml')
}
}
// --------------------------------------------------------------------------------
async function build (incremental)
{
generate_pages('page', incremental)
generate_pages('draft', incremental)
await parse_post_cache()
await generate_pages('post', incremental)
if (incremental)
// Delete posts whose source have been deleted.
await Promise.all(
Array.from(post_exists.entries())
.filter(([i, val]) => !val)
.map(([i, val]) => delete_page('post', posts[i].filename)))
generate_index_pages_and_atom()
await write_post_cache()
}
// --------------------------------------------------------------------------------
async function create (dir, file)
{
switch (dir) {
case 'posts':
case 'pages':
case 'drafts':
const kind = dir.slice(0, -1)
await generate_page(kind, file)
. /!stdout()
break
case 'top':
await fsp.copyFile(`${dir}/${file}`, `../${file}`)
.then (_ => console.log(`Created ../${file}`))
// On Windows (at least), renames will cause a creation event on
// the old file, causing this to fail. Don't emit a message.
// https://github.com/Axosoft/nsfw/issues/26
.catch (e => console.log(e.message))
break
}
}
async function rename (dir, old, cur)
{
switch (dir) {
case 'posts':
case 'pages':
case 'drafts':
const type = dir.slice(0, -1)
const old_loc = '../' + permalink(type, old)
const cur_loc = '../' + permalink(type, cur)
await fsp.rename(old_loc, cur_loc)
.then (_ => console.log(`Moved ${old_loc} to ${cur_loc}`))
.catch (e => console.log(e.message))
break
case 'top':
await fsp.rename(`../${old}`, `../${cur}`)
.then (_ => console.log(`Moved ../${old} to ../${cur}`))
.catch (e => console.log(e.message))
break
}
}
async function unlink (dir, filename)
{
switch (dir) {
case 'posts':
case 'pages':
case 'drafts':
const type = dir.slice(0, -1)
await delete_page(type, filename)
break
case 'top':
await fsp.unlink(`../${filename}`)
.then (_ => console.log(`Deleted ../${filename}`))
.catch (e => console.log(e.message))
break
}
}
function debounce (events)
{
const map = {}
// eliminate duplicate events
events.forEach(it => map[JSON.stringify(it)] = it)
for (event of Object.values(map))
{
// On Windows (at least), renames will cause a creation event on the old
// file. Here we delete it. (https://github.com/Axosoft/nsfw/issues/26)
// It also generates a modify event on the the new file, which we also
// delete.
if (event.action == watcher.actions.RENAMED) {
const create_old = {
action: watcher.actions.CREATED,
directory: event.directory,
file: event.oldFile
}
const modify_new = {
action: watcher.actions.MODIFIED,
directory: event.directory,
file: event.newFile
}
delete map[JSON.stringify(create_old)]
delete map[JSON.stringify(modify_new)]
}
// On Windows (at least), creations also cause a corresponding modification.
else if (event.action == watcher.actions.CREATED) {
const modify = {
action: watcher.actions.MODIFIED,
directory: event.directory,
file: event.file
}
delete map[JSON.stringify(modify)]
}
}
return Object.values(map)
}
async function watch()
{
await parse_post_cache()
let lock = Promise.resolve()
const cwd = path.resolve('.')
const watching = await watcher(path.resolve(cwd), async (events) => {
let resolve
const old_lock = lock
lock = new Promise((r, _) => resolve = r)
await old_lock
let promises = []
for (event of debounce(events)) {
const path = event.directory.replace(/\\/g, '/') // normalize
const dir = path.split('/').last()
if (dir == '.factory') continue
switch (event.action) {
case watcher.actions.CREATED:
case watcher.actions.MODIFIED:
promises.push(create(dir, event.file))
break
case watcher.actions.DELETED:
promises.push(unlink(dir, event.file))
break
case watcher.actions.RENAMED:
promises.push(rename(dir, event.oldFile, event.newFile))
break
}
}
if (promises.length > 0) {
await Promise.all(promises)
await Promise.all([
generate_index_pages_and_atom(),
write_post_cache()])
lowest_post_change = Infinity
console.log('') // skip a line before next set of events
}
resolve()
},
{debounceMS: 1000}) // bogus events tend to be fired, even so
watching.start()
}
// --------------------------------------------------------------------------------
switch (process.argv[2]) // assumes node <script> <arg>
{
case 'build':
build(true)
break
case 'rebuild':
build(false)
break
case 'watch':
watch()
break
}
// --------------------------------------------------------------------------------
// NOTES
//
// # Potential Improvements:
//
// - Make incremental build also track the deletion of pages and assets (not
// only posts).
//
// - Watching does not recursively track the changes in the top/ directories
// (only direct changes).