-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmlRenderer.js
66 lines (55 loc) · 2.44 KB
/
htmlRenderer.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
const path = require("path");
const fs = require("fs");
const templatesDir = path.resolve(__dirname, "../templates");
const render = employees => {
const html = [];
html.push(...employees
.filter(employee => employee.getRole() === "Manager")
.map(manager => renderManager(manager))
);
html.push(...employees
.filter(employee => employee.getRole() === "Engineer")
.map(engineer => renderEngineer(engineer))
);
html.push(...employees
.filter(employee => employee.getRole() === "Intern")
.map(intern => renderIntern(intern))
);
return renderMain(html.join(""));
};
const renderManager = manager => {
let template = fs.readFileSync(path.resolve(templatesDir, "manager.html"), "utf8");
template = replacePlaceholders(template, "name", manager.getName());
template = replacePlaceholders(template, "role", manager.getRole());
template = replacePlaceholders(template, "email", manager.getEmail());
template = replacePlaceholders(template, "id", manager.getId());
template = replacePlaceholders(template, "officeNumber", manager.getOfficeNumber());
return template;
};
const renderEngineer = engineer => {
let template = fs.readFileSync(path.resolve(templatesDir, "engineer.html"), "utf8");
template = replacePlaceholders(template, "name", engineer.getName());
template = replacePlaceholders(template, "role", engineer.getRole());
template = replacePlaceholders(template, "email", engineer.getEmail());
template = replacePlaceholders(template, "id", engineer.getId());
template = replacePlaceholders(template, "github", engineer.getGithub());
return template;
};
const renderIntern = intern => {
let template = fs.readFileSync(path.resolve(templatesDir, "intern.html"), "utf8");
template = replacePlaceholders(template, "name", intern.getName());
template = replacePlaceholders(template, "role", intern.getRole());
template = replacePlaceholders(template, "email", intern.getEmail());
template = replacePlaceholders(template, "id", intern.getId());
template = replacePlaceholders(template, "school", intern.getSchool());
return template;
};
const renderMain = html => {
const template = fs.readFileSync(path.resolve(templatesDir, "main.html"), "utf8");
return replacePlaceholders(template, "team", html);
};
const replacePlaceholders = (template, placeholder, value) => {
const pattern = new RegExp("{{ " + placeholder + " }}", "gm");
return template.replace(pattern, value);
};
module.exports = render;