-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuilder.js
More file actions
70 lines (60 loc) · 2.29 KB
/
builder.js
File metadata and controls
70 lines (60 loc) · 2.29 KB
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
(function () {
var fs = require("fs");
function expandScripts (inputString, directory) {
startLog("Expanding all script tags..");
inputString = expandFileReferences({
inputString: inputString,
regex: /<script.*?src="([a-zA-Z0-9\.\/]*)".*?><\/script>/g,
addStart: '\n<script type="text/javascript">\n\n',
addEnd: '\n</script>',
directory: directory
});
endLog();
return inputString;
}
function expandCSS (inputString, directory) {
startLog("Expanding linked CSS..");
inputString = expandFileReferences({
inputString: inputString,
regex: /<link href="([a-zA-Z0-9\.\/]*)" rel="stylesheet" type="text\/css">/g,
addStart: '\n<style type="text/css">\n\n',
addEnd: '\n</style>',
directory: directory
});
endLog();
return inputString;
}
function expandFileReferences (options) {
options = options || {};
var inputString = options.inputString,
regex = options.regex,
addStart = options.addStart,
addEnd = options.addEnd,
directory = options.directory;
var scriptTag, matchArray = [];
while (scriptTag = regex.exec(inputString)) {
var tag = scriptTag[0];
var src = directory + "/" + scriptTag[1];
matchArray.push({
pos: inputString.indexOf(tag),
tag: tag,
src: src
});
}
for (var i = matchArray.length-1; i >= 0; i--) {
var match = matchArray[i];
log(match.src);
var content = fs.readFileSync(match.src);
inputString = inputString.substr(0, match.pos) + addStart + content + addEnd + inputString.substr(match.pos + match.tag.length);
}
return inputString;
}
exports.processSample = function (filename, directory) {
var path = directory + "/" + filename;
log("Writing sample " + path + "..");
var fileContents = fs.readFileSync(path, "utf-8");
fileContents = expandScripts(fileContents, directory);
fileContents = expandCSS(fileContents, directory);
fs.writeFileSync(path, fileContents, "utf-8");
};
})();