1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-02 22:42:46 +00:00
TiddlyWiki5/js/JSONParser.js
Jeremy Ruston 4ce479f693 Refactored the image and JSON parsers
Now they reuse the WikiTextParseTree
2012-02-11 20:12:25 +00:00

52 lines
1.3 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 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;
};
JSONParser.prototype.parse = function(type,text) {
return new WikiTextParseTree([renderObject(JSON.parse(text))],{},this.store);
};
exports.JSONParser = JSONParser;
})();