52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
var yaml = {};
|
|
|
|
yaml.tojson = function (yaml) {
|
|
// Replace key value pairs that are strings with quotation marks around them
|
|
yaml = yaml.replace(/(\w+):/g, '"$1":');
|
|
yaml = yaml.replace(/: ([\w\.\/]+)/g, ': "$1"'); // TODO: make this more general
|
|
|
|
yaml = yaml.split("\n");
|
|
var cont = {};
|
|
var cur = 0;
|
|
for (var i = 0; i < yaml.length; i++) {
|
|
var line = yaml[i];
|
|
var indent = line.search(/\S/);
|
|
|
|
if (indent > cur) {
|
|
if (line[indent] == "-") {
|
|
cont[indent] = "array";
|
|
yaml[i] = line.sub(indent, "[");
|
|
} else {
|
|
cont[indent] = "obj";
|
|
yaml[i] = line.sub(indent - 1, "{");
|
|
}
|
|
}
|
|
|
|
if (indent < cur) {
|
|
while (cur > indent) {
|
|
if (cont[cur] === "obj") yaml[i - 1] = yaml[i - 1] + "}";
|
|
else if (cont[cur] === "array") yaml[i - 1] = yaml[i - 1] + "]";
|
|
|
|
delete cont[cur];
|
|
cur--;
|
|
}
|
|
}
|
|
|
|
if (indent === cur) {
|
|
if (yaml[i][indent] === "-") yaml[i] = yaml[i].sub(indent, ",");
|
|
else yaml[i - 1] = yaml[i - 1] + ",";
|
|
}
|
|
|
|
cur = indent;
|
|
}
|
|
yaml = "{" + yaml.join("\n") + "}";
|
|
yaml = yaml.replace(/\s/g, "");
|
|
yaml = yaml.replace(/,}/g, "}");
|
|
yaml = yaml.replace(/,]/g, "]");
|
|
yaml = yaml.replace(/,"[^"]+"\:,/g, ",");
|
|
yaml = yaml.replace(/,"[^"]+"\:}/g, "}");
|
|
return yaml;
|
|
};
|
|
|
|
return yaml
|