-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.js
66 lines (55 loc) · 2.01 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
63
64
65
66
var fs = require("fs"),
mustache = require("mustache"),
_ = require("underscore"),
wrench = require("wrench"),
util = require("util");
var CLIENT_LANGUAGES = ["python", "node.js", "cli"];
var SERVER_LANGUAGES = ["python", "node.js"];
var EXAMPLES = ["Hello World", "Streaming Responses", "First Class Exceptions"];
var HLJS_LANGUAGE_MAP = {
"python": "python",
"node.js": "javascript",
"cli": "no-highlight"
};
function getFile() {
var args = ["."].concat(Array.prototype.slice.call(arguments));
var path = Array.prototype.join.call(args, "/");
return fs.readFileSync(path, "utf8");
}
function getSource(exampleId, type, language) {
return getFile("examples", exampleId, type + "-" + language)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/\n/g, "<br/>");
}
var exampleTemplate = fs.readFileSync("./templates/example_template.html", "utf8");
var examples = _.map(EXAMPLES, function(title) {
var exampleId = title.toLowerCase().replace(/ /ig, "-");
var createLanguagesData = function(exampleId, type, source) {
return _.map(source, function(language) {
return {
language: language,
hljsClass: HLJS_LANGUAGE_MAP[language] || "",
source: getSource(exampleId, type, language),
tabId: [exampleId, type, language.replace(/\./ig, "")].join("-")
};
});
};
return mustache.render(exampleTemplate, {
title: title,
exampleId: exampleId,
types: [{
type: "Server",
languages: createLanguagesData(exampleId, "server", SERVER_LANGUAGES),
},{
type: "Client",
languages: createLanguagesData(exampleId, "client", CLIENT_LANGUAGES)
}]
});
});
try {
wrench.rmdirSyncRecursive("./bin");
} catch(e) {}
var source = fs.readFileSync("./src/index.html", "utf8").replace("__EXAMPLES__", examples.join("\n"));
fs.writeFileSync("./index.html", source);