-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
index.js
207 lines (176 loc) · 4.42 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
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
var pathFn = require('path'),
_ = require('lodash'),
async = require('async'),
colors = require('colors'),
domain = require('domain'),
Box = require('../box'),
i18n = require('../core/i18n'),
HexoError = require('../error');
/**
* This module manages all files in the theme folder.
*
* @class Theme
* @constructor
* @module hexo
* @extend Box
*/
var Theme = module.exports = function Theme(){
Box.call(this, hexo.theme_dir);
/**
* Theme configuration.
*
* @property config
* @type Object
*/
this.config = {};
/**
* The i18n instance of the theme.
*
* @property i18n
* @type i18n
*/
this.i18n = new i18n({
code: hexo.config.language
});
/**
* The view collection of the theme.
*
* @property views
* @type Object
*/
this.views = {};
this.processors = [
require('./processors/config'),
require('./processors/i18n'),
require('./processors/view'),
require('./processors/source')
];
};
Theme.prototype.__proto__ = Box.prototype;
Theme.prototype.load = Box.prototype.process;
Theme.prototype._generate = function(options, callback){
options = _.extend({
watch: false
}, options);
var model = hexo.model,
config = hexo.config,
route = hexo.route,
siteLocals = hexo.locals._generate(),
themeLocals = _.extend({}, config, this.config, config.theme_config),
env = hexo.env,
i18n = this.i18n,
layoutDir = pathFn.join(this.base, 'layout') + pathFn.sep,
self = this,
d = domain.create(),
called = false;
hexo._themeConfig = themeLocals;
var Locals = function(path, locals){
this.page = _.extend({path: path}, locals);
this.path = path;
this.url = config.url + config.root + path;
this.site = siteLocals;
this.config = config;
this.theme = themeLocals;
this._ = _;
this.__ = i18n.__();
this._p = i18n._p();
this.layout = 'layout';
this.cache = !options.watch;
this.env = hexo.env;
this.view_dir = layoutDir;
};
d.on('error', function(err){
!called && callback(err);
called = true;
});
var generators = hexo.extend.generator.list(),
keys = Object.keys(generators),
excludeGenerator = config.exclude_generator || [];
async.each(keys, function(key, next){
if (~excludeGenerator.indexOf(key)) return next();
var start = Date.now(),
generator = generators[key];
d.add(generator);
d.run(function(){
generator(siteLocals, function(path, layouts, locals){
if (!Array.isArray(layouts)) layouts = [layouts];
layouts = _.uniq(layouts);
var newLocals = new Locals(path, locals);
route.set(path, function(fn){
var view;
for (var i = 0, len = layouts.length; i < len; i++){
view = self.getView(layouts[i]);
if (view) break;
}
if (view){
hexo.log.d('Render %s: %s', layouts[i], path);
view.render(newLocals, fn);
} else {
hexo.log.w('No layout: %s', path);
fn();
}
});
}, function(err){
d.remove(generator);
hexo.log.d('Generator: %s ' + '(%dms)'.grey, key, Date.now() - start);
next(err);
});
});
}, function(err){
!called && callback(err);
called = true;
});
};
/**
* Runs generators.
*
* @method generate
* @param {Object} [options]
* @param {Boolean} [options.watch=false]
* @param {Function} [callback]
* @async
*/
Theme.prototype.generate = function(options, callback){
if (!callback){
if (typeof options === 'function'){
callback = options;
options = {};
} else {
callback = function(){};
}
}
var self = this;
async.series([
// Save database
function(next){
hexo.model.save(pathFn.join(hexo.base_dir, 'db.json'), function(err){
if (err) return next(HexoError.wrap(err, 'Database save failed'));
next();
});
},
// Run generators
function(next){
self._generate(options, callback);
}
], callback);
};
/**
* Gets a view.
*
* @method getView
* @param {String} path
* @return {Theme.View}
*/
Theme.prototype.getView = function(path){
// Replace backslashes on Windows
path = path.replace(/\\/g, '/');
var extname = pathFn.extname(path),
name = path.substring(0, path.length - extname.length),
views = this.views[name];
if (!views) return;
if (extname){
return views[extname];
} else {
return views[Object.keys(views)[0]];
}
};