-
Notifications
You must be signed in to change notification settings - Fork 8
/
template.js
164 lines (139 loc) · 4.64 KB
/
template.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
/**
* Requires
*
* @ignore
*/
var Consolidate = require('consolidate'),
FS = require('fs'),
Path = require('path'),
_ = require('lodash');
/**
* Private
*
* @ignore
*/
function renderJade(viewFiles, viewModel, callback) {
var jadeFile = viewFiles[0];
if (viewFiles.length > 1) {
console.warn('TEMPLATES: More than one possible Jade view file found, will take main or index.');
jadeFile = _.find(viewFiles, function (file) {
return Path.basename(file, Path.extname(file)) === 'main' || Path.basename(file, Path.extname(file)) === 'index';
});
}
if (jadeFile) {
// Handle exports.
viewModel.basedir = Path.join(__dirname, '../templates/jade/');
Consolidate.jade(jadeFile, viewModel, function (err, html) {
if (!err) {
callback && callback(html);
}
else {
console.error('TEMPLATES: Error generating view from Jade file ' + jadeFile, err);
callback && callback();
}
});
}
else {
console.warn('TEMPLATES: Could not find appropriate main Jade view file, please rename your main template to main.jade or index.jade.');
callback && callback();
}
}
function renderHandlebars(viewFiles, viewModel, callback) {
var hbTemplatePath = Path.join(__dirname, '../templates/handlebars/');
// Consolidate needs partial paths relative to the main tempalte being rendered and without the extension.
var getRelativePartial = function (file) {
return Path.join(Path.dirname(Path.relative(hbTemplatePath, file)), Path.basename(file, Path.extname(file)));
};
// Handle the various partials since there are no mixins.
viewModel.partials = {
card: '_card',
header: '_header',
content: '_content',
footer: '_footer'
};
var header = _.find(viewFiles, function (file) {
return Path.basename(file, Path.extname(file)) === 'header';
});
var content = _.find(viewFiles, function (file) {
return Path.basename(file, Path.extname(file)) === 'content';
});
var footer = _.find(viewFiles, function (file) {
return Path.basename(file, Path.extname(file)) === 'footer';
});
if (header) {
viewModel.partials.header = getRelativePartial(header);
}
if (content) {
viewModel.partials.content = getRelativePartial(content);
}
if (footer) {
viewModel.partials.footer = getRelativePartial(footer);
}
Consolidate.handlebars(Path.join(hbTemplatePath, 'card.hbs'), viewModel, function (err, html) {
if (!err) {
callback && callback(html);
}
else {
console.error('TEMPLATES: Error generating view from Handlebars file card.hbs', err);
callback && callback();
}
});
}
function renderStaticHtml(viewFiles, callback) {
var htmlFile = viewFiles[0];
if (viewFiles.length > 1) {
console.warn('TEMPLATES: More than one possible HTML view file found, will take main or index.');
htmlFile = _.find(viewFiles, function (file) {
return Path.basename(file, Path.extname(file)) === 'main' || Path.basename(file, Path.extname(file)) === 'index';
});
}
if (htmlFile) {
FS.readFile(htmlFile, function (err, html) {
if (!err) {
callback && callback(html);
}
else {
console.error('TEMPLATES: Error reading HTML file ' + htmlFile, err);
callback && callback();
}
});
}
else {
console.warn('TEMPLATES: Could not find appropriate main HTML view file, please rename your main file to main.html or index.html.');
callback && callback();
}
}
module.exports = {
generate: function (viewLocation, viewModel, callback) {
// Pretty silly way of doing this, need a more elgant way to determine the view files.
FS.readdir(viewLocation, function (err, files) {
var fullPath = function (file) {
return Path.join(viewLocation, file);
};
// Look for Jade files.
var viewFiles = _.filter(files, function (file) {
return Path.extname(file) === '.jade';
});
if (viewFiles.length > 0) {
return renderJade(_.map(viewFiles, fullPath), viewModel, callback);
}
// Look for Handlebars files.
viewFiles = _.filter(files, function (file) {
return Path.extname(file) === '.hbs';
});
if (viewFiles.length > 0) {
return renderHandlebars(_.map(viewFiles, fullPath), viewModel, callback);
}
// Look for HTML files.
viewFiles = _.filter(files, function (file) {
return Path.extname(file) === '.html' || Path.extname(file) === '.htm';
});
if (viewFiles.length > 0) {
return renderStaticHtml(_.map(viewFiles, fullPath), viewModel, callback);
}
else {
callback && callback();
}
});
}
};