This repository has been archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (64 loc) · 2.47 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
var fs = require("fs");
var Handlebars = require("handlebars");
function render(resume) {
// Load css and template
var css = fs.readFileSync(__dirname + "/css/style.css", "utf-8");
var template = fs.readFileSync(__dirname + "/resume.template", "utf-8");
// Load print-specific css
var print = fs.readFileSync(__dirname + "/css/print.css", "utf-8");
var image = fs.readFileSync(__dirname + "/images/picture.png", "base64");
// Register custom handlebars extensions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// foreach loops //
// http://stackoverflow.com/a/12002281/1263876
Handlebars.registerHelper("foreach",function(arr,options) {
if(options.inverse && !arr.length)
return options.inverse(this);
return arr.map(function(item,index) {
item.$index = index;
item.$first = index === 0;
item.$notfirst = index !== 0;
item.$last = index === arr.length-1;
return options.fn(item);
}).join('');
});
// Logic operators //
// http://stackoverflow.com/a/16315366
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
switch (operator) {
case '==':
return (v1 == v2) ? options.fn(this) : options.inverse(this);
case '===':
return (v1 === v2) ? options.fn(this) : options.inverse(this);
case '<':
return (v1 < v2) ? options.fn(this) : options.inverse(this);
case '<=':
return (v1 <= v2) ? options.fn(this) : options.inverse(this);
case '>':
return (v1 > v2) ? options.fn(this) : options.inverse(this);
case '>=':
return (v1 >= v2) ? options.fn(this) : options.inverse(this);
case '&&':
return (v1 && v2) ? options.fn(this) : options.inverse(this);
case '||':
return (v1 || v2) ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}
});
// comma separated lists //
// http://stackoverflow.com/a/18831911
Handlebars.registerHelper('commalist', function(items, options) {
return options.fn(items.join(', '));
});
console.log("image [",image,"]");
// Compile
return Handlebars.compile(template)({
css: css,
print: print,
resume: resume,
image: image
});
}
module.exports = {
render: render
};