forked from XuluWarrior/jsonresume-theme-orbit-original
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (129 loc) · 3.42 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
var fs = require("fs");
var path = require('path');
var Handlebars = require("handlebars");
var utils = require('handlebars-utils');
var marked = require('marked').marked;
Handlebars.registerHelper('markdown', function(str, locals, options) {
if (typeof str !== 'string') {
options = locals;
locals = str;
str = true;
}
if (utils.isOptions(locals)) {
options = locals;
locals = {};
}
var ctx = utils.context(this, locals, options);
var val = utils.value(str, ctx, options);
var markup = marked(val);
// If we end up with a string wrapped in one <p> block, remove it so we don't create a new text block
var startEndMatch = markup.match(/^<p>(.*)<\/p>\n$/);
return startEndMatch && startEndMatch[1].indexOf("<p>") === -1 ?
startEndMatch[1] :
markup;
});
Handlebars.registerHelper('displayUrl', function(str) {
return str.replace(/https?:\/\//, "");
});
Handlebars.registerHelper('toLowerCase', function(str) {
return str.toLowerCase();
});
Handlebars.registerHelper('year', function(str) {
if (str) {
var d = new Date(str);
return d.getFullYear();
} else {
return "Present"
}
});
Handlebars.registerHelper('award', function(str) {
switch (str.toLowerCase()) {
case "bachelor":
case "master":
return str + "s";
default:
return str;
}
});
Handlebars.registerHelper('skillLevel', function(str) {
switch (str.toLowerCase()) {
case "beginner":
return "25%";
case "intermediate":
return "50%";
case "advanced":
return "75%";
case "master":
return "100%";
default:
return parseInt(str) + "%"
}
});
// Resume.json used to have website property in some entries. This has been renamed to url.
// However the demo data still uses the website property so we will also support the "wrong" property name.
// Fix the resume object to use url property
function fixResume(resume) {
if (resume.basics.website) {
resume.basics.url = resume.basics.website;
delete resume.basics.website
}
fixAllEntries(resume.work);
fixAllEntries(resume.volunteer);
fixAllEntries(resume.publications);
fixAllEntries(resume.projects);
fixWork(resume.work);
}
function fixAllEntries(entries) {
if (entries) {
for (var i=0; i < entries.length; i++) {
var entry = entries[i];
if (entry.website) {
entry.url = entry.website;
delete entry.website;
}
}
}
}
// work.company has been renamed as work.name in v1.0.0
function fixWork(work) {
if (work) {
for (var i=0; i < work.length; i++) {
var entry = work[i];
if (entry.company) {
entry.name = entry.company;
delete entry.website;
}
}
}
}
function render(resume) {
var css = fs.readFileSync(__dirname + "/assets/css/styles.css", "utf-8");
var js = fs.readFileSync(__dirname + "/assets/js/main.js", "utf-8");
var tpl = fs.readFileSync(__dirname + "/resume.hbs", "utf-8");
fixResume(resume);
var partialsDir = path.join(__dirname, 'partials');
var filenames = fs.readdirSync(partialsDir);
filenames.forEach(function (filename) {
var matches = /^([^.]+).hbs$/.exec(filename);
if (!matches) {
return;
}
var name = matches[1];
var filepath = path.join(partialsDir, filename);
var template = fs.readFileSync(filepath, 'utf8');
Handlebars.registerPartial(name, template);
});
const packageJSON = require("./package");
return Handlebars.compile(tpl)({
css: css,
js: js,
resume: resume,
meta: {
packageName: packageJSON.name,
version: packageJSON.version
}
});
}
module.exports = {
render: render
};