mirror of
https://github.com/Jermolene/TiddlyWiki5.git
synced 2026-01-01 14:52:08 -08:00
Restored the JSONParser as a separate entity
It doesn't really have much in common with the JavaScript parser
This commit is contained in:
parent
1102c9babb
commit
488562bd95
3 changed files with 55 additions and 43 deletions
|
|
@ -14,31 +14,6 @@ var WikiTextParseTree = require("./WikiTextParseTree.js").WikiTextParseTree,
|
|||
Dependencies = require("./Dependencies.js").Dependencies,
|
||||
esprima = require("esprima");
|
||||
|
||||
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"]);
|
||||
}
|
||||
};
|
||||
|
||||
// Initialise the parser
|
||||
var JavaScriptParser = function(options) {
|
||||
this.store = options.store;
|
||||
|
|
@ -46,14 +21,6 @@ var JavaScriptParser = function(options) {
|
|||
|
||||
// Parse a string of JavaScript code or JSON and return the parse tree as a wikitext parse tree
|
||||
JavaScriptParser.prototype.parse = function(type,code) {
|
||||
if(type === "application/javascript") {
|
||||
return this.parseJavaScript(code);
|
||||
} else {
|
||||
return this.parseJSON(code);
|
||||
}
|
||||
}
|
||||
|
||||
JavaScriptParser.prototype.parseJavaScript = function(code) {
|
||||
// Get the parse tree
|
||||
var parseTree = esprima.parse(code,{
|
||||
tokens: true,
|
||||
|
|
@ -81,15 +48,6 @@ JavaScriptParser.prototype.parseJavaScript = function(code) {
|
|||
],new Dependencies(),this.store);
|
||||
};
|
||||
|
||||
JavaScriptParser.prototype.parseJSON = function(code) {
|
||||
// Wrap it in parenthesis to make it a program
|
||||
code = "(" + code + ")";
|
||||
// Get the parse tree
|
||||
return new WikiTextParseTree([
|
||||
renderObject(esprima.parse(code))
|
||||
],new Dependencies(),this.store);
|
||||
};
|
||||
|
||||
exports.JavaScriptParser = JavaScriptParser;
|
||||
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue