-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (95 loc) · 2.77 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
const fs = require("fs");
const path = require("path");
const handlebars = require("handlebars");
const helpers = require("handlebars-helpers")({
handlebars: handlebars
});
function getTranslations() {
let translations = {};
const i18nPath = path.join(__dirname, "/src/i18n")
const files = fs.readdirSync(i18nPath)
.filter(file => path.extname(file) === ".json")
files.forEach(file => {
const data = fs.readFileSync(path.join(i18nPath, file));
const language = path.parse(file).name;
translations[language] = JSON.parse(data);
})
return translations;
}
function skillLevelToPercentHelper(level) {
return Math.min(Math.max(level, 0), 100)
}
function locationToStringHelper(loc) {
return [
loc.address || "",
loc.postalCode || "",
loc.city || "",
loc.region || "",
loc.countryCode || ""
].filter(item => item).join(", ")
}
function languageFromLocaleHelper(locale) {
const languages = {
'en': 'English',
'fr': 'Français'
};
if (!languages.hasOwnProperty(locale))
throw new Error("Unknown locale ${locale} !");
return languages[locale];
}
function registerHelpers() {
handlebars.registerHelper("skillLevelToPercent", skillLevelToPercentHelper)
handlebars.registerHelper("locationToString", locationToStringHelper)
handlebars.registerHelper("languageFromLocale", languageFromLocaleHelper)
}
function registerPartials(partialsPath) {
const filenames = fs.readdirSync(partialsPath);
filenames.forEach(function (filename) {
const filenames = fs.readdirSync(partialsPath);
const matches = /^([^.]+).hbs$/.exec(filename);
if (!matches) {
return;
}
const name = matches[1];
const filepath = path.join(partialsPath, filename)
const partial = fs.readFileSync(filepath, "utf-8");
handlebars.registerPartial(name, partial);
});
}
function render(resume) {
const env = process.env.ENV || "prod";
const colorscheme = resume.meta.colorscheme;
const css =
fs.readFileSync(
path.join(__dirname, "public", env, "assets", "css", colorscheme + ".css"),
"utf-8"
);
const viewsPath = path.join(__dirname, "src", "views");
const template = fs.readFileSync(path.join(viewsPath, "index.hbs"), "utf-8");
registerPartials(path.join(viewsPath, "partials"));
registerPartials(path.join(viewsPath, "components"));
registerHelpers();
return handlebars.compile(template)({
resume: resume,
style: css,
scripts: resume.meta.scripts || [],
language: resume.meta.language || "en",
...getTranslations()
});
}
module.exports = {
render: render,
pdfRenderOptions: {
mediaType: "print",
format: "A4",
landscape: false,
margin: {
top: 0,
right: 0,
bottom: 0,
left: 0
},
printBackground: true,
scale: 0.7
}
};