-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
304 lines (277 loc) · 11.3 KB
/
helpers.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
(function() {
var Helpers, fs, gutil, hbs, highlighter, marked, path, templateData, toc, wiredep;
fs = require('fs');
path = require('path');
gutil = require('gulp-util');
marked = require('marked');
toc = require('markdown-toc');
highlighter = require('highlight.js');
wiredep = require('wiredep')();
hbs = new Object();
templateData = new Object();
Helpers = (function() {
var formatNavItem, link, navigation;
function Helpers(handlebars, tplateData) {
hbs = handlebars;
templateData = tplateData;
return {
template: this.template,
capitals: this.capitals,
bodyInsert: this.bodyInsert,
link: link,
cssInject: this.cssInject,
jsInject: this.jsInject,
markdown: this.markdown,
markdownHere: this.markdownHere,
image: this.image,
navigation: this.navigationWrapper
};
}
Helpers.prototype.capitals = function(str) {
return str.toUpperCase();
};
Helpers.prototype.bodyInsert = function(mode) {
var content;
if (mode !== 'live') {
content = '<body></body>';
} else {
content = '';
}
return new hbs.SafeString(content);
};
Helpers.prototype.jsInject = function(mode) {
var content, dir, j, k, l, len, len1, len2, ref, script, scripts;
content = new String();
if (mode === 'live') {
dir = './build-live/js';
} else {
dir = './build-dev/js';
}
scripts = fs.readdirSync(dir);
if (mode !== 'live') {
content += "<!-- bower:js -->\n\t";
ref = wiredep.js;
for (j = 0, len = ref.length; j < len; j++) {
script = ref[j];
script = script.slice(script.indexOf('bower_components'));
content += "<script src=\"" + script + "\"></script>\n\t";
}
content += "<!-- endbower -->\n\t";
}
for (k = 0, len1 = scripts.length; k < len1; k++) {
script = scripts[k];
if (mode === 'live' && script !== 'vendor_min_js') {
content += "<script src=\"http://" + templateData.year + ".igem.org/Template:" + templateData.teamName + "/js/" + script + "?action=raw&ctype=text/javascript\"></script>\n\t";
} else {
if (script !== 'vendor_min_js') {
content += "<script src=\"js/" + script + "\"></script>\n\t";
}
}
}
for (l = 0, len2 = scripts.length; l < len2; l++) {
script = scripts[l];
if (script === 'vendor_min_js') {
content = ("<script src=\"http://" + templateData.year + ".igem.org/Template:" + templateData.teamName + "/js/" + script + "?action=raw&ctype=text/javascript\"></script>\n\t") + content;
}
}
return new hbs.SafeString(content);
};
Helpers.prototype.cssInject = function(mode) {
var content, dir, j, k, l, len, len1, len2, ref, styles, stylesheet;
content = new String();
if (mode === 'live') {
dir = './build-live/css';
} else {
dir = './build-dev/css';
}
styles = fs.readdirSync(dir);
if (mode !== 'live') {
content += "<!-- bower:css -->\n\t";
ref = wiredep.css;
for (j = 0, len = ref.length; j < len; j++) {
stylesheet = ref[j];
stylesheet = stylesheet.slice(stylesheet.indexOf('bower_components'));
content += "<link rel=\"stylesheet\" href=\"" + stylesheet + "\" type=\"text/css\" />\n\t";
}
content += "<!-- endbower -->\n\t";
}
if (mode === 'live') {
content += '<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">\n\t';
}
for (k = 0, len1 = styles.length; k < len1; k++) {
stylesheet = styles[k];
if (mode === 'live' && stylesheet !== 'vendor_min_css') {
content += "<link rel=\"stylesheet\" href=\"http://" + templateData.year + ".igem.org/Template:" + templateData.teamName + "/css/" + stylesheet + "?action=raw&ctype=text/css\" type=\"text/css\" />\n\t";
} else if (stylesheet !== 'vendor_min_css') {
content += "<link rel=\"stylesheet\" href=\"styles/" + stylesheet + "\" type=\"text/css\" />\n\t";
}
}
for (l = 0, len2 = styles.length; l < len2; l++) {
stylesheet = styles[l];
if (stylesheet === 'vendor_min_css') {
content = ("<link rel=\"stylesheet\" href=\"http://" + templateData.year + ".igem.org/Template:" + templateData.teamName + "/css/" + stylesheet + "?action=raw&ctype=text/css\" type=\"text/css\" />\n\t") + content;
}
}
return new hbs.SafeString(content);
};
Helpers.prototype.image = function(img, format, mode) {
var content, fmt, imageStores;
if (mode === 'live') {
if (format === 'directlink') {
if (fs.readdirSync(__dirname).indexOf('images.json') !== -1) {
imageStores = JSON.parse(fs.readFileSync('images.json'));
content = imageStores[img];
} else {
content = '';
}
} else {
if (format === 'file') {
fmt = 'File';
} else if (format === 'media') {
fmt = 'Media';
}
content = "</html> [[File:" + templateData.teamName + "_" + templateData.year + "_" + img + "]] <html>";
}
} else {
if (format !== 'directlink') {
content = "<img src=\"images/" + img + "\" />";
} else {
content = "images/" + img;
}
}
return new hbs.SafeString(content);
};
link = function(linkName, mode) {
if (mode === 'live') {
if (linkName === 'index') {
return "http://" + templateData.year + ".igem.org/Team:" + templateData.teamName;
} else {
return "http://" + templateData.year + ".igem.org/Team:" + templateData.teamName + "/" + templateData.links[linkName];
}
} else {
if (linkName === 'index') {
return 'index.html';
} else {
return linkName + ".html";
}
}
};
formatNavItem = function(name) {
var newName;
newName = name.split('_');
newName = newName.map(function(piece) {
return piece.charAt(0).toUpperCase() + piece.slice(1);
});
return newName.join(' ');
};
navigation = function(field, mode, active1, active2, recursed) {
var active, actives, arg, content, i, isActive, item, j, k, l, len, len1, newItem, ref, value;
if (recursed) {
content = "<ul class=\"dropdown-menu\">\n";
} else {
content = "<ul class=\"navbar-nav mr-auto\">\n";
}
actives = new Array();
for (i = j = 0, len = arguments.length; j < len; i = ++j) {
arg = arguments[i];
if (i >= 2) {
actives.push(arg);
}
}
for (item in field) {
value = field[item];
isActive = false;
for (k = 0, len1 = actives.length; k < len1; k++) {
active = actives[k];
if (item === active) {
isActive = true;
}
}
if (item[0] === '_') {
newItem = '';
for (i = l = 1, ref = item.length - 1; 1 <= ref ? l <= ref : l >= ref; i = 1 <= ref ? ++l : --l) {
newItem += item[i];
}
item = newItem;
}
if (typeof value === 'object') {
item = formatNavItem(item);
if (isActive) {
content += "<li class=\"active dropdown nav-item\"><a href=\"#\" class=\"nav-link dropdown-toggle nav-link\" data-toggle=\"dropdown\" role=\"button\" aria-haspopup=\"true\" aria-expanded=\"false\">" + item + "<span class=\"caret\"></span></a>\n";
} else {
content += "<li class=\"dropdown nav-item\"><a href=\"#\" class=\"nav-link dropdown-toggle nav-link\" data-toggle=\"dropdown\" role=\"button\" aria-haspopup=\"true\" aria-expanded=\"false\">" + item + "<span class=\"caret\"></span></a>\n";
}
content += navigation(value, mode, active1, active2, true);
} else {
value = formatNavItem(value);
if (isActive) {
if (recursed) {
content += "<li class=\"active\"><a class=\"dropdown-item\" href=\"" + (link(item, mode)) + "\"><span>" + value + "</span></a></li>\n";
} else {
content += "<li class=\"active nav-item\"><a class=\"nav-link\" href=\"" + (link(item, mode)) + "\"><span>" + value + "</span></a></li>\n";
}
} else {
if (recursed) {
content += "<li class=\"\"><a class=\"dropdown-item\" href=\"" + (link(item, mode)) + "\"><span>" + value + "</span></a></li>\n";
} else {
content += "<li class=\"nav-item\"><a class=\"nav-link\" href=\"" + (link(item, mode)) + "\"><span>" + value + "</span></a></li>\n";
}
}
content += "</li>\n";
}
}
content += "</ul>\n";
return content;
};
Helpers.prototype.navigationWrapper = function(mode, active1, active2) {
var content;
content = "<nav class=\"navbar navbar-toggleable-md navbar-inverse bg-primary fixed-top\">";
content += "<a class=\"navbar-brand\" href=\"" + (link('index', mode)) + "\">Waterloo</a>";
content += "<button class=\"navbar-toggler navbar-toggler-right\" type=\"button\" data-toggle=\"collapse\" data-target=\"#navbarCollapse\" aria-controls=\"navbarCollapse\" aria-expanded=\"false\" aria-label=\"Toggle navigation\">\n<span class=\"navbar-toggler-icon\"></span>\n</button>";
content += "<div class=\"collapse navbar-collapse\" id=\"navbarCollapse\">";
content += navigation(templateData.navigation, mode, active1, active2);
content += "</div>";
content += "</nav>";
return new hbs.SafeString(content);
};
Helpers.prototype.template = function(templateName, mode) {
var template;
template = hbs.compile(fs.readFileSync(__dirname + "/src/templates/" + templateName + ".hbs", 'utf8'));
if (mode === 'live') {
if (templateName === 'preamble') {
return new hbs.SafeString('');
}
return new hbs.SafeString("{{" + templateData.teamName + "/" + templateName + "}}");
} else {
return new hbs.SafeString(template(templateData));
}
};
Helpers.prototype.markdownHere = function(string, options) {
var handlebarsedMarkdown, markedHtml;
marked.setOptions({
highlight: function(code) {
return highlighter.highlightAuto(code).value;
}
});
handlebarsedMarkdown = hbs.compile(string)(templateData);
markedHtml = marked(handlebarsedMarkdown);
return new hbs.SafeString(markedHtml);
};
Helpers.prototype.markdown = function(file) {
var content, handlebarsedMarkdown, markdownFile;
marked.setOptions({
highlight: function(code) {
return highlighter.highlightAuto(code).value;
}
});
markdownFile = fs.readFileSync(__dirname + "/src/markdown/" + file + ".md").toString();
handlebarsedMarkdown = hbs.compile(markdownFile)(templateData);
content = '<div class="row">';
content += '<div class="col"><div class="content-main">' + marked(handlebarsedMarkdown) + '</div></div>';
content += "</div>";
return new hbs.SafeString(content);
};
return Helpers;
})();
module.exports = Helpers;
}).call(this);