-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.js
62 lines (51 loc) · 1.91 KB
/
build.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
const fs = require("fs")
const path = require("path")
const {ncp} = require("ncp")
const del = require("del")
function injectTemplates(filename) {
let html = fs.readFileSync(filename).toString();
//Inject templates
html = html.replace(/{{{.*}}}/g, (match, x, y) => {
return fs.readFileSync(`templates/${match.replace(/[{}]/g, "")}`).toString()
})
let photoItemSrc = fs.readFileSync("templates/photoitem.html").toString();
//Inject images
html = html.replace(/{img}(.|\n|\r)*?{img}/g, (match, x, y) => {
match = match.replace("{img}", "{");
match = match.replace("{img}", "}");
json = JSON.parse(match);
output = photoItemSrc.replace("[IMG_SRC]", json.src);
output = output.replace("[IMG_SRC]", json.src);
output = output.replace("[CAPTION]", json.caption);
output = output.replace("[SIZE]", json.size);
output = output.replace("[DIV_ATTRS]", json.div_attr == undefined ? "": json.div_attr);
output = output.replace("[IMG_ATTRS]", json.img_attr == undefined ? "": json.img_attr);
return output
})
fs.writeFileSync(filename, html)
console.log("---parsed ", filename)
}
function fromDir(startPath, filter) {
let htmlFiles = []
const files = fs.readdirSync(startPath)
for (let i = 0; i < files.length; i++) {
const file = files[i]
const filename = path.join(startPath, file)
const stat = fs.lstatSync(filename)
if (stat.isDirectory()) {
fromDir(filename, filter)
} else if (filename.indexOf(filter) >= 0) {
htmlFiles.push(filename)
}
}
return htmlFiles
}
// Delete out the previous build
del.sync(["docs/*"])
// Copy all of the files from the src directory to the docs directory
ncp("./src", "./docs", () => {
const htmlFiles = fromDir("./docs", ".html")
htmlFiles.forEach(file => {
injectTemplates(file)
})
})