forked from mythsman/hexo-douban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (94 loc) · 3.33 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
108
109
110
111
112
/* global hexo */
'use strict';
const fs = require('hexo-fs');
const path = require('path');
const log = require('./lib/util').log
let init_from_console = false
const supported_types = ['book', 'movie', 'game', 'song']
// Register `hexo g` and `hexo s`
supported_types.forEach(supported_type => {
hexo.extend.generator.register(`${supported_type}s`, function (locals) {
if (init_from_console) {
return
}
if (!this.config.douban || !this.config.douban[supported_type] || !this.config.douban.builtin) {
return;
}
if (!this.config.douban[supported_type].path) {
this.config.douban[supported_type].path = `${supported_type}s/index.html`
}
locals.douban_type = supported_type
return require(`./lib/generator`).call(this, locals);
});
})
const 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: '-s, --songs', desc: 'Generate douban songs only'}
]
}
// Register `hexo douban`
hexo.extend.console.register('douban', 'Generate pages from douban', options, function (args) {
init_from_console = true
if (!this.config.douban) {
log.info("No douban config specified")
return
}
if (!this.config.douban.id) {
log.info("No douban id specified")
return
}
let force_types = []
supported_types.forEach(supported_type => {
if ((args[supported_type[0]] || args[`${supported_type}s`]) && this.config.douban[supported_type]) {
force_types.push(supported_type)
}
})
let enabled_types = [];
if (force_types.length !== 0) {
enabled_types = force_types
} else {
supported_types.forEach(type => {
if (this.config.douban[type]) {
enabled_types.push(type);
}
})
}
// Config preprocess
enabled_types.forEach(type => {
if (!this.config.douban[type].path) {
this.config.douban[type].path = `${type}s/index.html`
}
})
if (enabled_types.length === 0) {
log.info("No douban type specified")
return
}
const self = this;
//Register route
enabled_types.forEach(enabled_type => {
let page_path = self.config.douban[enabled_type].path || enabled_type + "/index.html"
if (page_path.startsWith("/")) {
page_path = page_path.substr(1)
}
self.config.douban[enabled_type].path = page_path
hexo.extend.generator.register(enabled_type, function (locals) {
locals.douban_type = enabled_type
return require(`./lib/generator`).call(this, locals)
});
})
//Generate files
self.load().then(function () {
enabled_types.forEach(enabled_type => {
const publicDir = self.public_dir;
const id = self.config.douban[enabled_type].path
fs.mkdirSync(path.join(publicDir, id.replace("index.html", "")), {recursive: true})
self.route.get(id) && self.route.get(id)._data().then(function (contents) {
fs.writeFile(path.join(publicDir, id), contents);
log.info("Generated: %s", id);
});
});
});
});