/
item.js
60 lines (50 loc) · 1.37 KB
/
item.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
var fs = require("./_fs");
var sys = require("sys");
var path = require("_path");
var kiwi = require("kiwi");
var yaml = kiwi.require("yaml");
var Item = function (origin, filePath, namespace) {
this.origin = origin;
this.filePath = filePath;
this.relativePath = path.relativePath(origin, filePath);
this.outputPath = this.relativePath;
this.namespace = namespace;
this.preProcess(this.filePath);
};
Item.prototype.read = function (filePath) {
return fs.readFileSync(filePath);
};
Item.prototype.parserHeaders = function (data) {
var parsed = { "headers": {}
, "body": ""
};
if (data) {
var splits = data.split("\n\n");
var header = splits.shift();
if (header) {
try {
parsed.headers = yaml.eval(header);
parsed.body = splits.join("\n\n");
} catch (e) {
parsed.body = data;
}
}
}
return parsed;
};
Item.prototype.preProcess = function (filePath) {
var parsed = this.parserHeaders(this.read(filePath));
this.headers = parsed.headers;
this.body = parsed.body;
};
Item.prototype.context = function () {
var context = this.headers;
context.body = this.body;
return context;
};
Item.prototype.write = function (output) {
var outfile = path.join(output, this.outputPath);
fs.mkdirsSync(path.dirname(outfile), 0755);
fs.writeFile(outfile, this.body);
};
exports.Item = Item;