1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-17 23:34:50 +00:00
TiddlyWiki5/js/JSONParser.js
Jeremy Ruston 7f136dd898 Get rid of the old WikiTextParseTree class
And replace it with a new root Renderer() class
2012-03-03 18:33:16 +00:00

52 lines
1.4 KiB
JavaScript

/*\
title: js/JSONParser.js
Compiles JSON objects into JavaScript functions that render them in HTML and plain text
\*/
(function(){
/*jslint node: true */
"use strict";
var Renderer = require("./Renderer.js").Renderer,
Dependencies = require("./Dependencies.js").Dependencies,
utils = require("./Utils.js");
var renderObject = function(obj) {
var children = [],t;
if(obj instanceof Array) {
for(t=0; t<obj.length; t++) {
children.push(Renderer.ElementNode("li",{
"class": ["jsonArrayMember"]
},[renderObject(obj[t])]));
}
return Renderer.ElementNode("ul",{
"class": ["jsonArray"]
},children);
} else if(typeof obj === "object") {
for(t in obj) {
children.push(Renderer.ElementNode("li",{
"class": ["jsonObjectMember"]
},[Renderer.SplitLabelNode("JSON",[Renderer.TextNode(t)],[renderObject(obj[t])])]));
}
return Renderer.ElementNode("ul",{
"class": ["jsonObject"]
},children);
} else {
return Renderer.LabelNode("JSON" + (typeof obj),[Renderer.TextNode(JSON.stringify(obj))],["jsonValue"]);
}
};
var JSONParser = function(options) {
this.store = options.store;
};
JSONParser.prototype.parse = function(type,text) {
return new Renderer([renderObject(JSON.parse(text))],new Dependencies(),this.store);
};
exports.JSONParser = JSONParser;
})();