2012-01-25 22:35:52 +00:00
|
|
|
/*\
|
|
|
|
title: js/JSONParser.js
|
|
|
|
|
|
|
|
Compiles JSON objects into JavaScript functions that render them in HTML and plain text
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true */
|
|
|
|
"use strict";
|
|
|
|
|
2012-02-11 20:12:25 +00:00
|
|
|
var WikiTextParseTree = require("./WikiTextParseTree.js").WikiTextParseTree,
|
|
|
|
HTML = require("./HTML.js").HTML,
|
|
|
|
utils = require("./Utils.js");
|
|
|
|
|
|
|
|
var renderObject = function(obj) {
|
|
|
|
var children = [],t;
|
|
|
|
if(obj instanceof Array) {
|
|
|
|
for(t=0; t<obj.length; t++) {
|
|
|
|
children.push(HTML.elem("li",{
|
|
|
|
"class": ["jsonArrayMember"]
|
|
|
|
},[renderObject(obj[t])]));
|
|
|
|
}
|
|
|
|
return HTML.elem("ul",{
|
|
|
|
"class": ["jsonArray"]
|
|
|
|
},children);
|
|
|
|
} else if(typeof obj === "object") {
|
|
|
|
for(t in obj) {
|
|
|
|
children.push(HTML.elem("li",{
|
|
|
|
"class": ["jsonObjectMember"]
|
|
|
|
},[HTML.splitLabel("JSON",[HTML.text(t)],[renderObject(obj[t])])]));
|
|
|
|
}
|
|
|
|
return HTML.elem("ul",{
|
|
|
|
"class": ["jsonObject"]
|
|
|
|
},children);
|
|
|
|
} else {
|
|
|
|
return HTML.label("JSON" + (typeof obj),[HTML.text(JSON.stringify(obj))],["jsonValue"]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var JSONParser = function(options) {
|
|
|
|
this.store = options.store;
|
2012-01-25 22:35:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
JSONParser.prototype.parse = function(type,text) {
|
2012-02-11 20:12:25 +00:00
|
|
|
return new WikiTextParseTree([renderObject(JSON.parse(text))],{},this.store);
|
2012-01-25 22:35:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.JSONParser = JSONParser;
|
|
|
|
|
|
|
|
})();
|