1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-08 04:54:23 +00:00

Refactored the image and JSON parsers

Now they reuse the WikiTextParseTree
This commit is contained in:
Jeremy Ruston 2012-02-11 20:12:25 +00:00
parent 9ececdf406
commit 4ce479f693
2 changed files with 41 additions and 64 deletions

View File

@ -9,41 +9,22 @@ Compiles images into JavaScript functions that render them in HTML
/*jslint node: true */
"use strict";
var utils = require("./Utils.js");
var WikiTextParseTree = require("./WikiTextParseTree.js").WikiTextParseTree,
HTML = require("./HTML.js").HTML,
utils = require("./Utils.js");
var ImageRenderer = function(handlerCode) {
/*jslint evil: true */
this.handler = eval(handlerCode);
};
ImageRenderer.prototype.render = function(tiddler,store) {
return this.handler(tiddler,store,utils);
};
// The parse tree is degenerate
var ImageParseTree = function(type,text) {
this.type = type;
this.text = text;
this.dependencies = {};
};
ImageParseTree.prototype.compile = function(type) {
if(type === "text/html") {
if(this.type === "image/svg+xml") {
return new ImageRenderer("(function (tiddler,store,utils) {return '<img src=\"data:" + this.type + "," + utils.stringify(encodeURIComponent(this.text)) + "\">';})");
} else {
return new ImageRenderer("(function (tiddler,store,utils) {return '<img src=\"data:" + this.type + ";base64," + this.text + "\">';})");
}
} else {
return null;
}
};
var ImageParser = function() {
var ImageParser = function(options) {
this.store = options.store;
};
ImageParser.prototype.parse = function(type,text) {
return new ImageParseTree(type,text);
var src;
if(this.type === "image/svg+xml") {
src = "data:" + type + "," + encodeURIComponent(text);
} else {
src = "data:" + type + ";base64," + text;
}
return new WikiTextParseTree([HTML.elem("img",{src: src})],{},this.store);
};
exports.ImageParser = ImageParser;

View File

@ -9,47 +9,43 @@ Compiles JSON objects into JavaScript functions that render them in HTML and pla
/*jslint node: true */
"use strict";
var utils = require("./Utils.js");
var WikiTextParseTree = require("./WikiTextParseTree.js").WikiTextParseTree,
HTML = require("./HTML.js").HTML,
utils = require("./Utils.js");
var JSONRenderer = function(handlerCode) {
/*jslint evil: true */
this.handler = eval(handlerCode);
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"]);
}
};
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) {
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() {
var JSONParser = function(options) {
this.store = options.store;
};
JSONParser.prototype.parse = function(type,text) {
return new JSONParseTree(JSON.parse(text));
return new WikiTextParseTree([renderObject(JSON.parse(text))],{},this.store);
};
exports.JSONParser = JSONParser;
})();