1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 20:10:03 +00: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:
Jeremy Ruston 2012-03-02 11:31:39 +00:00
parent 1102c9babb
commit 488562bd95
3 changed files with 55 additions and 43 deletions

View File

@ -16,6 +16,7 @@ var WikiStore = require("./WikiStore.js").WikiStore,
Renderer = require("./Renderer.js").Renderer, Renderer = require("./Renderer.js").Renderer,
WikiTextParser = require("./WikiTextParser.js").WikiTextParser, WikiTextParser = require("./WikiTextParser.js").WikiTextParser,
JavaScriptParser = require("./JavaScriptParser.js").JavaScriptParser, JavaScriptParser = require("./JavaScriptParser.js").JavaScriptParser,
JSONParser = require("./JSONParser.js").JSONParser,
ImageParser = require("./ImageParser.js").ImageParser; ImageParser = require("./ImageParser.js").ImageParser;
var App = function() { var App = function() {
@ -27,7 +28,8 @@ var App = function() {
// Register the parsers // Register the parsers
this.store.registerParser("text/x-tiddlywiki",new WikiTextParser({store: this.store})); this.store.registerParser("text/x-tiddlywiki",new WikiTextParser({store: this.store}));
this.store.registerParser(["image/svg+xml","image/jpg","image/jpeg","image/png","image/gif"],new ImageParser({store: this.store})); this.store.registerParser(["image/svg+xml","image/jpg","image/jpeg","image/png","image/gif"],new ImageParser({store: this.store}));
this.store.registerParser(["application/json","application/javascript"],new JavaScriptParser({store: this.store})); this.store.registerParser("application/javascript",new JavaScriptParser({store: this.store}));
this.store.registerParser("application/json",new JSONParser({store: this.store}));
// Register the standard tiddler serializers and deserializers // Register the standard tiddler serializers and deserializers
tiddlerInput.register(this.store); tiddlerInput.register(this.store);
tiddlerOutput.register(this.store); tiddlerOutput.register(this.store);

52
js/JSONParser.js Normal file
View File

@ -0,0 +1,52 @@
/*\
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,
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 WikiTextParseTree([renderObject(JSON.parse(text))],new Dependencies(),this.store);
};
exports.JSONParser = JSONParser;
})();

View File

@ -14,31 +14,6 @@ var WikiTextParseTree = require("./WikiTextParseTree.js").WikiTextParseTree,
Dependencies = require("./Dependencies.js").Dependencies, Dependencies = require("./Dependencies.js").Dependencies,
esprima = require("esprima"); 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 // Initialise the parser
var JavaScriptParser = function(options) { var JavaScriptParser = function(options) {
this.store = options.store; 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 // Parse a string of JavaScript code or JSON and return the parse tree as a wikitext parse tree
JavaScriptParser.prototype.parse = function(type,code) { 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 // Get the parse tree
var parseTree = esprima.parse(code,{ var parseTree = esprima.parse(code,{
tokens: true, tokens: true,
@ -81,15 +48,6 @@ JavaScriptParser.prototype.parseJavaScript = function(code) {
],new Dependencies(),this.store); ],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; exports.JavaScriptParser = JavaScriptParser;
})(); })();