-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (64 loc) · 2.1 KB
/
index.js
File metadata and controls
85 lines (64 loc) · 2.1 KB
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
/* global hexo */
'use strict';
var doT = require('dot'),
fs = require('fs'),
path = require('path'),
_ = require('lodash');
// cache variables
var _cache = {},
_partialsCache = {},
_globals = {
//tmp function -- disables overwriting via setGlobals
load: _.noop
};
var globalGenerator = function (caller) {
var instance = {
load: function (file){
var template = null;
// let's try loading content from cache
template = _partialsCache[file];
// no content so let's load from file system
if (!template) {
var route = path.join(path.dirname(caller), file+".dot");
template = fs.readFileSync(route, "utf-8");
_partialsCache[file] = template;
}
return template;
}
};
return _.extend({}, _globals, instance);
};
var _settings = doT.templateSettings;
exports.setGlobals = function (globals) {
for (var f in _globals) {
if (!globals[f]) {
globals[f] = _globals[f];
} else {
throw new Error('Your global uses reserved utility: ' + f);
}
}
_globals = globals;
};
exports.setTemplateSettings = function (settings) {
for (var f in settings) {
_settings[f] = settings[f];
}
};
function __loadPartial (caller, context) {
return function (file) {
// generate loader function
var globals = globalGenerator(caller);
// get template, no caching - otherwise will pollute global env
var template = doT.template(globals.load(file), null, globals);
return template.call(globals, context);
};
}
hexo.extend.renderer.register('dot', 'html', function(data, context){
var globals = globalGenerator(data.path);
context.__loadPartial = __loadPartial(data.path, context);
// generating template if not exists
var template = _cache[data.path] ? _cache[data.path] : doT.template(data.text, null, globals);
// trying to cache
_cache[data.path] = template;
return template.call(globals, context);
}, true);