1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-24 17:10:29 +00:00

Added parsing and rendering for JSON tiddlers

This commit is contained in:
Jeremy Ruston 2012-01-25 22:35:52 +00:00
parent b7fd566148
commit 3e62698efb
4 changed files with 91 additions and 1 deletions

View File

@ -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);

56
js/JSONParser.js Normal file
View File

@ -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;
})();

View File

@ -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<customTemplates.length; t++) {

View File

@ -0,0 +1,25 @@
title: SampleData
type: application/json
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}