-
Notifications
You must be signed in to change notification settings - Fork 4
/
apply_template.js
112 lines (92 loc) · 3.13 KB
/
apply_template.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
// Overwrites navigation and footer for all pages
var path = require("path");
var fs = require("fs");
var cheerio = require("cheerio");
// Change these constants to suit your needs
const HTML_FOLDER = path.join(__dirname, 'public');; // folder with your HTML files
const EXCLUDE_FILES = ["google1b139a0c8d361fbe.html"];
const TEMPLATE = "templates/template.html"; // template file
function isHtml(filename) {
lower = filename.toLowerCase();
return (lower.endsWith(".htm") || lower.endsWith(".html"));
}
function findHtml(folder) {
if (!fs.existsSync(folder)) {
console.log("Could not find folder: ", folder);
return;
}
var files = fs.readdirSync(folder);
var htmls = [];
for (var i = 0; i < files.length; i++) {
var filename = path.join(folder, files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
var recursed = findHtml(filename);
for (var j = 0; j < recursed.length; j++) {
recursed[j] = path.join(files[i], recursed[j]).replace(/\\/g, "/");
}
htmls.push.apply(htmls, recursed);
} else if (isHtml(filename) && !EXCLUDE_FILES.includes(files[i])) {
htmls.push(files[i]);
};
};
return htmls;
};
// Splits html around common header area
function splitForHead(htmlTxt) {
const START_COMMENT = "<!-- Start Common Head -->";
const END_COMMENT = "<!-- End Common Head -->";
var stop1 = htmlTxt.indexOf(START_COMMENT) + START_COMMENT.length;
var stop2 = htmlTxt.indexOf(END_COMMENT);
var beforeIt = htmlTxt.slice(0, stop1);
var it = htmlTxt.slice(stop1, stop2);
var afterIt = htmlTxt.slice(stop2);
return [beforeIt, it, afterIt];
}
function readTemplate() {
var filename = path.join(__dirname, TEMPLATE);
var txt = fs.readFileSync(filename).toString();
var $ = cheerio.load(txt);
var header = $("body").find("header").eq(0);
var footer = $("body").find("footer").eq(0);
var end_of_body = $("#before-end-of-body");
var template = {
"head": splitForHead(txt)[1],
"header": header,
"footer": footer,
"end_of_body": end_of_body
}
return template;
}
function updateHead(htmlTxt, template) {
parts = splitForHead(htmlTxt);
return parts[0] + template.head + parts[2];
}
function updateHtml(root, file, template) {
var filename = path.join(root, file);
var txt = fs.readFileSync(filename).toString();
txt = updateHead(txt, template);
var $ = cheerio.load(txt);
var header = $("body").find("header").eq(0);
var footer = $("body").find("footer").eq(0);
var end_of_body = $("#before-end-of-body");
header.replaceWith(template.header);
footer.replaceWith(template.footer);
end_of_body.replaceWith(template.end_of_body);
fs.writeFile(filename, $.html(), function (err) {
if (err) {
return console.log(err);
}
console.log("Saved " + filename);
});
}
function main() {
files = findHtml(HTML_FOLDER);
template = readTemplate();
console.log("Updating these files:");
for (var i = 0; i < files.length; i++) {
console.log(" " + files[i]);
updateHtml(HTML_FOLDER, files[i], template);
}
}
main();