-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
view.js
209 lines (177 loc) · 4.14 KB
/
view.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
var async = require('async'),
pathFn = require('path'),
_ = require('lodash');
/**
* The view object of the Theme class.
*
* @class View
* @param {String} source The full source of the view file.
* @param {String} path The relative path of the view file.
* @param {Theme} theme
* @constructor
* @namespace Theme
* @module hexo
*/
var View = module.exports = function View(source, path, theme){
/**
* The full path of the view file.
*
* @property source
* @type {String}
*/
this.source = source;
/**
* The relative path of the view file.
*
* @property path
* @type {String}
*/
this.path = path;
/**
* The extension name of the view file. (With a prefixed dot)
*
* @property extname
* @type {String}
*/
this.extname = pathFn.extname(path);
/**
* The theme object.
*
* @property theme
* @type {Theme}
*/
this.theme = theme;
/**
* View data.
*
* @property data
* @type {Object}
*/
this.data = null;
/**
* View cache.
*
* @property cache
* @type {Object}
* @private
*/
this.cache = null;
};
/**
* Renders the view.
*
* @method render
* @param {Object} [options]
* @param {Boolean} [options.cache=true]
* @param {Function} callback
*/
View.prototype.render = function(options, callback){
if (!callback){
if (typeof options === 'function'){
callback = options;
options = {};
} else {
callback = function(){};
}
}
options = _.extend({
cache: true
}, options);
var data = this.data;
if (!data) return callback();
var layout = data.hasOwnProperty('layout') ? data.layout : options.layout,
locals = _.extend(this._buildLocals(options), _.omit(data, 'layout', '_content'), {filename: this.source}),
self = this;
hexo.render.render({
path: this.source,
text: data._content
}, locals, function(err, result){
if (err) return callback(err);
if (!layout) return callback(null, result);
var layoutView = self._resolveLayout(layout);
if (layoutView){
var layoutLocals = _.extend({}, locals, {body: result, layout: false});
layoutView.render(layoutLocals, callback);
} else {
callback(null, result);
}
});
};
/**
* Resolves the layout path.
*
* @method _resolveLayout
* @param {String} name
* @return {View}
* @private
*/
View.prototype._resolveLayout = function(name){
// Relative path
var layoutView = this.theme.getView(pathFn.join(pathFn.dirname(this.path), name));
if (layoutView && layoutView.source === this.source) layoutView = null;
// Absolute path
if (!layoutView){
layoutView = this.theme.getView(name);
if (layoutView && layoutView.source === this.source) layoutView = null;
}
return layoutView;
};
/**
* Clones the object and binds the helper functions.
*
* @method _buildLocals
* @param {Object} locals
* @return {Object}
* @private
*/
View.prototype._buildLocals = function(locals){
var helpers = hexo.extend.helper.list(),
obj = {};
_.each(helpers, function(helper, name){
obj[name] = helper.bind(obj);
});
_.each(locals, function(fn, name){
if (!helpers.hasOwnProperty(name) || helpers[name].toString() === fn.toString()){
obj[name] = fn;
}
});
return obj;
};
/**
* Renders the view synchronizedly.
*
* @method renderSync
* @param {Object} [options]
* @param {Boolean} [options.cache=true]
* @return {String}
*/
View.prototype.renderSync = function(options){
options = _.extend({
cache: true
}, options);
var data = this.data;
if (!data) return;
var layout = data.hasOwnProperty('layout') ? data.layout : options.layout,
locals = _.extend(this._buildLocals(options), _.omit(data, 'layout', '_content'), {filename: this.source});
var result = hexo.render.renderSync({
path: this.source,
text: data._content
}, locals);
if (!result) return;
if (!layout) return result;
var layoutView = this._resolveLayout(layout);
if (layoutView){
var layoutLocals = _.extend({}, locals, {body: result, layout: false});
return layoutView.renderSync(layoutLocals);
} else {
return result;
}
};
/**
* Invalidates the cache.
*
* @method invalidate
*/
View.prototype.invalidate = function(){
this.cache = null;
};