diff --git a/js/App.js b/js/App.js index ce27ed1e0..e5bfca6c5 100644 --- a/js/App.js +++ b/js/App.js @@ -14,6 +14,7 @@ var WikiStore = require("./WikiStore.js").WikiStore, tiddlerInput = require("./TiddlerInput.js"), tiddlerOutput = require("./TiddlerOutput.js"), WikiTextParser = require("./WikiTextParser.js").WikiTextParser, + JSONParser = require("./JSONParser.js").JSONParser, JavaScriptParser = require("./JavaScriptParser.js").JavaScriptParser, ImageParser = require("./ImageParser.js").ImageParser, Navigators = require("./Navigators.js").Navigators, @@ -25,10 +26,13 @@ var App = function() { this.isBrowser = typeof window !== "undefined"; // Create the main store this.store = new WikiStore(); - // Register the wikitext parser and the SVG parser + // Register the wikitext parser and the image parser this.store.registerParser("text/x-tiddlywiki",new WikiTextParser({ store: this.store })); + this.store.registerParser("application/json",new JSONParser({ + store: this.store + })); var imageParser = new ImageParser(); this.store.registerParser("image/svg+xml",imageParser); this.store.registerParser("image/jpg",imageParser); diff --git a/js/JSONParser.js b/js/JSONParser.js new file mode 100644 index 000000000..c4186dcef --- /dev/null +++ b/js/JSONParser.js @@ -0,0 +1,56 @@ +/*\ +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 utils = require("./Utils.js"); + +var JSONRenderer = function(handlerCode) { + /*jslint evil: true */ + this.handler = eval(handlerCode); +}; + +JSONRenderer.prototype.render = function(tiddler,store) { + return this.handler(tiddler,store,utils); +}; + +JSONRenderer.prototype.toString = function(type) { + var output = []; + utils.renderObject(output,type,this.handler.toString(),[]); + return output.join(""); +}; + +// The parse tree is degenerate +var JSONParseTree = function(tree) { + this.tree = tree; + this.dependencies = []; +}; + +JSONParseTree.prototype.compile = function(type) { +console.log("(function (tiddler,store,utils) {var output=[]; utils.renderObject(output,'" + type + "'," + JSON.stringify(this.tree) + ",[]);return output.join('');})"); + return new JSONRenderer("(function (tiddler,store,utils) {var output=[]; utils.renderObject(output,'" + type + "'," + JSON.stringify(this.tree) + ",[]);return output.join('');})"); +}; + +JSONParseTree.prototype.toString = function(type) { + var output = []; + utils.renderObject(output,type,this.tree,[]); + return output.join(""); +}; + +var JSONParser = function() { +}; + +JSONParser.prototype.parse = function(type,text) { + return new JSONParseTree(JSON.parse(text)); +}; + + +exports.JSONParser = JSONParser; + +})(); diff --git a/js/Utils.js b/js/Utils.js index a5aea606e..2c3432835 100755 --- a/js/Utils.js +++ b/js/Utils.js @@ -363,6 +363,11 @@ utils.renderObject = function(output,type,node,customTemplates) { renderNodeHtml = function(output,node) { if(node instanceof Array) { renderArrayHtml(output,node); + } else if (typeof node === "string") { + output.push(utils.stitchElement("span",null,{ + classNames: ["treeNode","label"], + content: utils.htmlEncode(node) + })); } else { var custom = false; for(var t=0; t