mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-02-20 04:50:03 +00:00
Passed "store" to the other parser constructors
This commit is contained in:
parent
45663f6f16
commit
017be8f4ff
@ -26,8 +26,8 @@ var App = function() {
|
|||||||
this.store = new WikiStore();
|
this.store = new WikiStore();
|
||||||
// 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("application/json",new JSONParser());
|
this.store.registerParser("application/json",new JSONParser({store: this.store}));
|
||||||
var imageParser = new ImageParser();
|
var imageParser = new ImageParser({store: this.store});
|
||||||
this.store.registerParser("image/svg+xml",imageParser);
|
this.store.registerParser("image/svg+xml",imageParser);
|
||||||
this.store.registerParser("image/jpg",imageParser);
|
this.store.registerParser("image/jpg",imageParser);
|
||||||
this.store.registerParser("image/jpeg",imageParser);
|
this.store.registerParser("image/jpeg",imageParser);
|
||||||
|
13
js/HTML.js
13
js/HTML.js
@ -42,7 +42,7 @@ var utils = require("./Utils.js");
|
|||||||
Constructs an HTMLParseTree from a tree of nodes. A single node or an array of nodes can be passed.
|
Constructs an HTMLParseTree from a tree of nodes. A single node or an array of nodes can be passed.
|
||||||
|
|
||||||
As a shortcut, the constructor can be called as an ordinary function without the new keyword, in which case
|
As a shortcut, the constructor can be called as an ordinary function without the new keyword, in which case
|
||||||
it automatically returns the `text/html` rendering of the tree.
|
it by default returns the `text/html` rendering of the tree.
|
||||||
*/
|
*/
|
||||||
var HTML = function(tree,type) {
|
var HTML = function(tree,type) {
|
||||||
if(this instanceof HTML) {
|
if(this instanceof HTML) {
|
||||||
@ -110,6 +110,17 @@ HTML.macro = function(name,params,children,dependencies) {
|
|||||||
return m;
|
return m;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Static method to construct a label
|
||||||
|
*/
|
||||||
|
HTML.label = function(type,value,classes) {
|
||||||
|
classes = (classes || []).slice(0);
|
||||||
|
classes.push("label");
|
||||||
|
return HTML.elem("span",{
|
||||||
|
"class": classes
|
||||||
|
},value);
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Static method to construct a split label
|
Static method to construct a split label
|
||||||
*/
|
*/
|
||||||
|
@ -9,41 +9,22 @@ Compiles images into JavaScript functions that render them in HTML
|
|||||||
/*jslint node: true */
|
/*jslint node: true */
|
||||||
"use strict";
|
"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) {
|
var ImageParser = function(options) {
|
||||||
/*jslint evil: true */
|
this.store = options.store;
|
||||||
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() {
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ImageParser.prototype.parse = function(type,text) {
|
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;
|
exports.ImageParser = ImageParser;
|
||||||
|
@ -9,47 +9,43 @@ Compiles JSON objects into JavaScript functions that render them in HTML and pla
|
|||||||
/*jslint node: true */
|
/*jslint node: true */
|
||||||
"use strict";
|
"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) {
|
var renderObject = function(obj) {
|
||||||
/*jslint evil: true */
|
var children = [],t;
|
||||||
this.handler = eval(handlerCode);
|
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) {
|
var JSONParser = function(options) {
|
||||||
return this.handler(tiddler,store,utils);
|
this.store = options.store;
|
||||||
};
|
|
||||||
|
|
||||||
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() {
|
|
||||||
};
|
};
|
||||||
|
|
||||||
JSONParser.prototype.parse = function(type,text) {
|
JSONParser.prototype.parse = function(type,text) {
|
||||||
return new JSONParseTree(JSON.parse(text));
|
return new WikiTextParseTree([renderObject(JSON.parse(text))],{},this.store);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
exports.JSONParser = JSONParser;
|
exports.JSONParser = JSONParser;
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
@ -33,6 +33,7 @@ exports.macro = {
|
|||||||
} else {
|
} else {
|
||||||
return store.renderText("text/x-tiddlywiki",v,type,tiddler.title);
|
return store.renderText("text/x-tiddlywiki",v,type,tiddler.title);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
case "date":
|
case "date":
|
||||||
var template = params.template || "DD MMM YYYY";
|
var template = params.template || "DD MMM YYYY";
|
||||||
return encoder(utils.formatDateString(v,template));
|
return encoder(utils.formatDateString(v,template));
|
||||||
|
@ -13,6 +13,7 @@ Technical documentation includes:
|
|||||||
* [[Testing]] regimen
|
* [[Testing]] regimen
|
||||||
* Details of the CommandLineInterface
|
* Details of the CommandLineInterface
|
||||||
* Overview of TiddlyWikiArchitecture
|
* Overview of TiddlyWikiArchitecture
|
||||||
|
** TiddlyWikiInternals
|
||||||
** MacroInternals
|
** MacroInternals
|
||||||
* Information about TiddlerFiles and RecipeFiles
|
* Information about TiddlerFiles and RecipeFiles
|
||||||
* NewWikiTextFeatures
|
* NewWikiTextFeatures
|
||||||
|
Loading…
x
Reference in New Issue
Block a user