-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (49 loc) · 1.58 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
const fs = require("fs");
const yaml = require("js-yaml");
const ejs = require("ejs");
const config = yaml.load(fs.readFileSync("_config.yml"));
const readSPARQL = (config) => {
let stringData = [];
const commentRe = /#\+.*/g;
const dirPath = config["queries_directory"];
fs.readdirSync(dirPath).forEach((file) => {
// Exclude LICENSE file
if (file === 'LICENSE') {
return;
}
const fileContent = fs.readFileSync(`${dirPath}${file}`).toString();
const commentMatches = fileContent.match(commentRe);
const grlcDecorators = commentMatches
? yaml.load(
commentMatches
.map((el) => el.replace(/#\+\s?/, ""))
.join("\n")
)
: {};
stringData.push({
filename: file.split(".")[0],
query: fileContent,
encodedQuery: encodeURIComponent(fileContent),
title: grlcDecorators["summary"],
main_title: config["title"],
});
});
return stringData;
};
const renderAll = (config) => {
const queriesArr = readSPARQL(config);
const allData = {
queryFiles: queriesArr,
...config,
};
if (!fs.existsSync(config["output_directory"])) {
fs.mkdirSync(config["output_directory"]);
}
ejs.renderFile(allData["index_template"], allData, {}, (err, str) => {
if (err) throw err;
fs.writeFile(`${config["output_directory"]}/index.html`, str, (err) => {
if (err) throw err;
});
});
};
renderAll(config);