-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
178 lines (146 loc) · 5.36 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
const fs = require("fs");
const path = require("path");
const combyne = require("combyne");
const async = require("async");
const visitCombyne = require("visit-combyne");
// Proxy the settings from the internal combyne object
var settings = combyne.settings;
// Mimic how the actual Combyne stores.
settings._filters = {};
settings._partials = {};
// Proxy the filter and partial registration methods.
settings.registerFilter = combyne.prototype.registerFilter;
settings.registerPartial = combyne.prototype.registerPartial;
/**
* Processes a template, finding all its filters and nested partials.
*
* @param {string} fileName - The filename of the template.
* @param {Object} data - Data to render with.
* @param {Function} next - The next continuation.
*/
function processTemplate(fileName, data, next, noParse) {
var route = this;
var dirname = this.root;
var ext = path.extname(fileName);
// Read in the template name as a buffer.
fs.readFile(fileName, "utf8", function(err, buffer) {
// Pass up any errors.
if (err) {
return next(err);
}
// Send back the compiled template.
var template = combyne(String(buffer));
// Find all extend.
var extend = visitCombyne(template.tree.nodes, function(node) {
return node.type === "ExtendExpression";
}).map(function(node) { return node.value; });
// Find all partials.
var partials = visitCombyne(template.tree.nodes, function(node) {
return node.type === "PartialExpression" && noParse !== node.value;
}).map(function(node) { return node.value; });
// Find all filters.
var filters = visitCombyne(template.tree.nodes, function(node) {
return node.filters && node.filters.length;
}).map(function(node) {
return node.filters.map(function(filter) {
return filter.value;
}).join(" ");
});
// Flatten the array.
if (filters.length) {
filters = filters.join(" ").split(" ");
}
// Map all extend to functions.
extend = extend.map(function(render) {
return function(callback) {
var name = render.template;
var renderPath = path.join(dirname, name + ext);
// The last argument of this call is the noparse option that specifies
// the virtual partial should not be loaded.
processTemplate.call(route, renderPath, data, function(err, render) {
if (err) { return callback(err); }
template.registerPartial(name, render);
callback(err, template);
}, render.partial);
};
});
// Map all partials to functions.
partials = partials.map(function(name) {
return function(callback) {
// Ignore those that have already been defined globally.
if (name in settings._partials) {
return callback();
}
var partialPath = path.join(dirname, name + ext);
processTemplate.call(route, partialPath, data, function(err, partial) {
if (err) { return callback(err); }
template.registerPartial(name, partial);
settings._partials[name] = partial;
callback(err, template);
});
};
});
// Map all filters to functions.
filters = filters.map(function(name) {
// Filters cannot be so easily inferred location-wise, so assume they are
// preconfigured or exist in a filters directory.
return function(callback) {
var filtersDir = settings.filtersDir || "filters";
var filtersPath = path.join(dirname, filtersDir, name + ".js");
// Ignore those that have already been defined globally.
if (name in settings._filters) {
return callback();
}
try {
var filter = require(filtersPath);
// Register the exported function.
settings._filters[name] = filter;
}
catch (ex) {
return callback(ex);
}
callback(null, filter);
};
});
// Find all files and map the partials.
async.parallel(partials.concat(extend, filters), function(err) {
if (err) { return next(err); }
// Register all the global partials.
Object.keys(settings._partials).forEach(function(name) {
var partial = settings._partials[name];
template.registerPartial(name, partial);
});
// Register all the global filters.
Object.keys(settings._filters).forEach(function(name) {
var filter = settings._filters[name];
template.registerFilter(name, filter);
});
// Render the template.
next(err, template);
});
});
}
/**
* Express support.
*
* @param {string} fileName - The template path to load.
* @param {object} data - The data to render.
* @param {function} next - The continuation function.
*/
settings.__express = function(fileName, data, next) {
processTemplate.call(this, fileName, data, function(err, template) {
if (err) { return next(err); }
// Render the top level template with context data.
next(null, template.render(data));
});
};
// Expose configuration express settings.
module.exports = function(options) {
settings.__proto__ = options;
return settings.__express;
};
// Ensure settings is accessible.
module.exports.settings = settings;
module.exports.registerFilter = settings.registerFilter.bind(settings);
module.exports.registerPartial = settings.registerPartial.bind(settings);
module.exports.VERSION = require("./package.json").version;