forked from mythsman/hexo-douban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (93 loc) · 2.84 KB
/
index.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
/* global hexo */
'use strict';
var fs = require('hexo-fs');
var path = require('path');
var log = require('hexo-log')({
debug: false,
silent: false
});
hexo.extend.generator.register('books', function (locals) {
if (!this.config.douban || !this.config.douban.builtin) {
return;
}
return require('./lib/books-generator').call(this, locals);
});
hexo.extend.generator.register('movies', function (locals) {
if (!this.config.douban || !this.config.douban.builtin) {
return;
}
return require('./lib/movies-generator').call(this, locals);
});
hexo.extend.generator.register('games', function (locals) {
if (!this.config.douban || !this.config.douban.builtin) {
return;
}
return require('./lib/games-generator').call(this, locals);
});
hexo.extend.generator.register('musics', function (locals) {
if (!this.config.douban || !this.config.douban.builtin) {
return;
}
return require('./lib/musics-generator').call(this, locals);
});
var options = {
options: [
{ name: '-b, --books', desc: 'Generate douban books only' },
{ name: '-m, --movies', desc: 'Generate douban movies only' },
{ name: '-g, --games', desc: 'Generate douban games only' },
{ name: '-i, --musics', desc: 'Generate douban musics only' }
]
};
hexo.extend.console.register('douban', 'Generate pages from douban', options, function (args) {
var names = [];
if (args.b || args.books) {
names.push("books");
}
if (args.m || args.movies) {
names.push("movies");
}
if (args.g || args.games) {
names.push("games");
}
if (args.i || args.musics) {
names.push("musics");
}
if (names.length === 0) {
names.push("books", "movies", "games", "musics");
}
var doubanLoadingPath = '/assets/douban-loading.gif';
//Load static files
hexo.extend.generator.register('douban-gif', function (locals) {
return {
path: doubanLoadingPath,
data: function () {
return fs.createReadStream(path.join(__dirname, '/lib/templates/douban-loading.gif'));
}
};
});
//Register route
names.forEach(name => {
hexo.extend.generator.register(name, require('./lib/' + name + '-generator'));
})
var self = this;
var publicDir = self.public_dir;
//Generate files
self.load().then(function () {
if(!fs.existsSync(publicDir)){
fs.mkdirSync(publicDir);
}
if(!fs.existsSync(path.join(publicDir,'assets'))){
fs.mkdirSync(path.join(publicDir,'assets'));
}
self.route.get(doubanLoadingPath)._data().then(function (stream) {
stream.pipe(fs.createWriteStream(path.join(publicDir, doubanLoadingPath)));
});
names.forEach(name => {
var id = name + "/index.html";
self.route.get(id) && self.route.get(id)._data().then(function (contents) {
fs.writeFile(path.join(publicDir, id), contents);
log.info("Generated: %s", id);
});
});
});
});